How to create a swap file for Ubuntu or CentOS 7


Here are bare minimal instructions on how to set up a swap file for Ubuntu Linux and CentOS 7. The basic difference between Ubuntu and CentOS is that with Ubuntu fallocate seems to always work and on CentOS dd seems to work better.

In the beginning and in the end swapon command is used to test for the availability of the swap file. In the middle the vi text editor is used to append the requisite swap entries to fstab. If you’re working with AWS EC2, also see the reference pointing to their site.

It’s possible to use this entire procedure for CentOS 7 but you can’t do fallocate and you have to use dd instead.

Ubuntu Instructions

Below if you have 2GB RAM and you want to create the maximum size swapfile as per the table at the bottom of this article. On 4GB you could add a maximum of 8GB.

Go in as sudo -i

swapon --show
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

vi /etc/fstab

Append or comment existing swap entry and then append this:

 /swapfile swap swap defaults 0 0

Check your work:

swapon --show
free -h

CentOS 7

fallocate doesn’t work well with CentOS so use dd as per below.

Example is for 4 GB, but for 1 GB count = 1048576 so use a calculator and multiply by your desired size.

To assess the situation I like to use free -m or swapon --show

dd if=/dev/zero of=/swapfile bs=1024 count=4194304
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
vi /etc/fstab

Add this to fstab:

/swapfile swap swap defaults 0 0

Comment out the old swap file line.

Note about CentOS and dedicated swap partitions

If you use vi to edit the /etc/fstab file, and you encounter the line below:

/dev/mapper/cl-swap swap swap defaults 0 0

That basically means the installation engineer created a seperate swap partition for your installation. Although this is technically a more performant way of using swap, it completely breaks the ability to easily upgrade swap space. The reason for this is you would have to go and find the backend technology for the disk (e.g. the VM or physical host type) and then fiddle with complicated commands to enlare it. Also, if you just comment out that line and use the hard disk type swap option as per our advice, you will end up with an orphaned partition of wasted disk space.

The solution is too complicated to document here, as there are too many different types of disk backends, but you could use this document as a primer.

Note about existing swap file

There is a command swapoff

but the problem is you shouldn’t just use it unless you have a lot of time. See references why this is complicated, but in a nutshell once you run this command it’s going to transfer what’s in the current swap file to memory, and that’s a complicated process. It might just crash your machine or slow it down to FUBAR.

So it’s better just to create a new swap file, and then remove the other swap file later. Follow the above steps but use /swapfile2 as the name.

How Much Swap Is Needed*

*Adapted from the Ubuntu reference, removed hibernation column as Servers don’t hibernate

RAM       Recommended    Maximum
1GB          1GB          2GB
2GB          1GB          4GB
3GB          2GB          6GB
4GB          2GB          8GB
5GB          2GB          10GB
6GB          2GB          12GB
8GB          3GB          16GB
12GB         3GB          24GB
16GB         4GB          32GB
24GB         5GB          48GB
32GB         6GB          64GB
64GB         8GB          128GB
128GB        11GB         256GB
256GB        16GB         512GB
512GB        23GB         1TB
1TB          32GB         2TB
2TB          46GB         4TB
4TB          64GB         8TB
8TB          91GB         16TB


Share this article

Leave a Reply

Your email address will not be published.

Scroll to Top