Swap is space on a disk that is set aside for use as additional virtual memory. When a Linux server runs out of memory, the kernel has the ability to move inactive processes over to swap space to make room for active processes. Swap space can take the form of either a dedicated swap partition or a swap file, but in the case of most virtual Cloud Servers, a swap partition is not present so the only option is to create a swap file. The performance of a swap file is comparable to the performance of a swap partition, but using a swap file makes it easier to control the swap size without having to repartition the volume.
This article will walk you through the steps you’d need to take in order to create a swap file on a Linux server, and to modify the ‘swappiness’ value.
• Cloud Server running a Linux OS
1) Create the file that you’ll use for swap by running the following command:
# sudo fallocate -l 1G /mnt/1GB.swap
In this case we are created a 1GB swap file, but adjust this level to your own server’s needs. If you receive a message saying ‘fallocate failed: Operation not supported’ try running the following instead:
# sudo dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
2) Format the swap file by running the following command:
# sudo mkswap /mnt/1GB.swap
3) Now you need to add the file to the system as swap, which allows your system to begin utilizing it. Do this with the following command:
# sudo swapon /mnt/1GB.swap
4) In order to make the change permanent, and prevent having to do this after each reboot, open up the /etc/fstab file in your text editor of choice and add the following to the end of the file and save it:
/mnt/1GB.swap none swap sw 0 0
5) To adjust the ‘swappiness’ value, open the file /etc/sysctl.conf in your text editor of choice and add this at the end:
We recommend starting with a lower value, like 10, and increasing it as you believe is necessary. The highest value you can set is 100, and most systems with swap partitions will default to 60. If you set the vm.swappiness to 0, it will only use the swap file if the system runs out of memory entirely, while higher values allow your system to swap idle processes out which may improve overall system performance.
6) Ensure that the swap file was created by entering the following command:
# sudo swapon -s
7) Reboot your server, to ensure the changes you made are still in effect. If it comes back up and the ‘sudo swapon -s’ command returns the same output, it should be working. The final step is to adjust permissions to the file to allow only the root user to access it:
# chmod 600 /mnt/1GB.swap
NOTE: Should you later need to remove the swap file, use the following command:
# sudo swapoff /mnt/1GB.swap
Be sure to remove the entry from the /etc/fstab file, or simply comment out the line you added by inserting a # sign in front of it.
Lastly, remove the actual swap file with this command:
# sudo rm /mnt/1GB.swap
This article has taken you the process of creating a swap file, activating, and configuring swap space on your Linux server.