centos, linux,

Adding Swap space on CentOS 7 Linux Cloud VMs

Feb 06, 2023 · 3 mins read · Post a comment

It feels like there is no more need of adding a swap practice, especially today, deploying VMs in the cloud, considering the abundance of memory, disk and vCPUs resources that are available on-demand. But, for those out there who wants to trade a “bit” of a disk space for a few extra GBs of memory while running CentOS 7, the steps are described below.


  • CentOS 7
  • sudo permissions


initial checks

Step 1. Check first if swap is already enabled.

swapon -s
cat /proc/swaps

Step 2. Check VM’s overall memory.

free -mh

Example output:

              total        used        free      shared  buff/cache   available
Mem:            15G        410M         13G        1.2M        1.6G         14G
Swap:            0B          0B          0B

Step 3. Check available storage space.

df -h

configuring swap

Step 1. Before going any further, the kicker here is to find how much swap space is enough for a certain memory size. I’ve dug the following recommended practice:

  • Less than 2GB of RAM = 2x RAM
  • Between 2GB and 8GB = 1x RAM
  • Between 8GB and 64GB = Equal or more than 4GB, it depends.
  • More than 64GB = Equal or more than 4GB, it depends.

In my case, with 16GB of RAM, initially I’ll settle with 4GB of swap space.

Step 2. Run the following command to create a swap file.

sudo dd if=/dev/zero of=/swapfile bs=256M count=16

256MBx16 = 4096MB

Another option to consider is the optimal block size (bs), which in my example is set to 256MB. However, it depends on several factors, including: the OS, disks, filesystem, workload type, among others. I believe the default value is 128MB. Anyhow, there are few bash scripts you could find on the Internet to calculate the “optimal” block size.

Step 3. Update the file permissions:

sudo chmod 600 /swapfile

Step 4. Configure a Linux swap area:

sudo mkswap /swapfile

Step 5. Activate the swap at once:

swapon /swapfile

Step 6. Verify:

sudo swapon -s

Step 7. Apply swap changes to be permanent, so you won’t lose the swap configuration after reboot. Open the /etc/fstab file and add the following line:

/swapfile swap swap defaults 0 0

Step 8. Confirm the memory too:

free -mh


Swappiness enumerate how often the system will use the swap space. You can find the default value, by running:

cat /proc/sys/vm/swappiness

If you want to configure a higher or a lower number, update the vm.swappiness parameter found under the /etc/sysctl.conf file.

As with the swap space and block sizes, swappiness value depends on multiple things. You have to test, experiment to find the optimal value. Even so, I would suggest not to put many hours in, just for the swap space optimization itself.

disable and remove a swap space

Step 1. Disable swap:

sudo swapoff -v /swapfile

Step 2. Remove the swap entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Step 3. Finally, delete the swap file:

rm /swapfile


If you have any other great solutions on this topic, or even issues, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.