Skip to content

How to add Swap File

Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G with 2G.

1. Create swap file

Create a file that will be used for swap:

sudo fallocate -l 1G /swapfile

"Note: dynamic space allocation such as using fallocate is not supported, as it causes problems with some file systems such as F2FS[1] and will likely fail to activate at boot time with error "swapon: swapfile has holes" as of kernel 5.7. Hence, contiguous allocation, such as dd, is the only reliable way to allocate a swap file.[2]" (https://wiki.archlinux.org/index.php/Swap#Swap_file).

If fallocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=3145728

2. Change ownership

Only the root user should be able to write and read the swap file. To set the correct permissions type:

sudo chmod 600 /swapfile

3. Set file as swap area

Use the mkswap utility to set up the file as Linux swap area:

sudo mkswap /swapfile

4. Enable swap

Enable the swap with the following command:

sudo swapon /swapfile

5. Set swap in fstab

To make the change permanent open the /etc/fstab file and append the following line:

/swapfile swap swap defaults 0 0

6. Verify

To verify that the swap is active, use either the swapon or the free command as shown below:

swapon:

sudo swapon --show
NAME      TYPE  SIZE   USED PRIO
/swapfile file 1024M 507.4M   -1

free:

sudo free -h
total        used        free      shared  buff/cache   available
Mem:           488M        158M         83M        2.3M        246M        217M
Swap:          1.0G        506M        517M

7. Adjust swappiness value

How to adjust the swappiness value

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.

The default swappiness value is 60. You can check the current swappiness value by typing the following command:

cat /proc/sys/vm/swappiness
60

While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.

For example, to set the swappiness value to 10, you would run the following sysctl command:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:

vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

8. Remove Swap File

If for any reason you want to deactivate and remove the swap file, follow these steps:

First, deactivate the swap by typing:

sudo swapoff -v /swapfile

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

Finally, delete the actual swapfile file using the rm command:

sudo rm /swapfile