Swap space is a space on a disk that is used when the amount of physical memory (RAM) is full. When the RAM is full, inactive processes are moved to the swap space. By default, most of the servers do not have a Swap file.
Without a swap file, a server that runs out of memory will start killing the running processes to free up the memory of RAM. It may cause server downtime and loss of unsaved data. Some applications require swap files to ensure data reliability. So we need to create a swap file on the server. Swap can be created in two forms, Swap partition and swap file.
In today’s tutorial, we will learn how we can add a Swap file on the Ubuntu 20.04 server. You can also use the same tutorial to add swap on other operating systems like CentOS etc.
- Server or Local system with Ubuntu 20.04 operating system.
- Access of Root user or another user with Sudo privileges.
Check the System for Swap
Before we start, let’s check if the server or the local system already has an existing swap space. It is possible to have multiple swap files or swap partitions, but one swap file or swap partition is enough. Run the following command to check the Swap file on the server.
sudo swapon --show
If the above command did not show any output, then it means the server or the local system does not have any existing swap space. You can also use the free command with the -h attribute to check both RAM and Swap on the server.
sudo free -h
Create the Swap file
Create the swap file as per the server’s RAM because the swap file is always twice the size of RAM. Set the swap file size as shown in the following table.
|RAM||SWAP FILE SIZE|
|1 GB||2 GB|
|2 GB||4 GB|
|4 GB||4 GB|
For this tutorial, we are using a server with 4 GB of RAM. According to the above table, for 4 GB of RAM, we need to create a swap file of 4 GB on the server.
- Allocate the free space for the swap file with the following command.
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1M
Change the value of the count as per your server’s RAM.
- Change the Ownership of the swapfile with the chown command
sudo chown root:root /swapfile
- Change the permission of the swap file with the chmod command. So, except root user, nobody can access and modify the swap file.
sudo chmod 0600 /swapfile
- Now we need to tell the system to enable the swap with the following command.
sudo mkswap /swapfile
- Swap is ready to use, run the following command to activate the Swap file.
sudo swapon /swapfile
Make the Swap File Permanent
Currently, the swap file is enabled, but it will get automatically removed after the server reboot. To make the swap file permanent, we need to mount the swap file by modifying the /etc/fstab file.
Open the /etc/fstab file in edit mode with the following command.
sudo vim /etc/fstab
Add the following data at the end of the /etc/fstab file to mount the swap file permanently.
/swapfile swap swap defaults 0 0
Now check the configuration which you have made with the above command is valid and error-free with the following command. fstab file must be properly formatted according to syntax. Otherwise, you can have a boot problem on your next reboot.
Run the below command to check if everything is okay with the above changes.
sudo mount -a
Adjust the Swappiness Value
The swappiness value specifies how often the swap space should be used by the Linux kernel. As the swappiness value varies between 0 and 100, 0 indicate the low priority, and a higher number increases the swap usage priority. By default, the swappiness value will be 60 which is okay for the normal server environment. For the production environment server, the swappiness value must be low and set to 10.
Run the following command to change the swappiness value.
sudo sysctl vm.swappiness=10
The swappiness value will automatically be set to default after the reboot. To make the swappiness value permanent, we need to modify the /etc/sysctl.conf file. Open the /etc/sysctl.conf file in edit mode with the following command.
sudo vim /etc/sysctl.conf
Add the following data at the end of the /etc/sysctl.conf file to set the swappiness value permanently.
Removing a Swap File
If you want to remove the swap file from the server, then follow the below steps –
- First, disable the swap file with the following command.
sudo swapoff -v /swapfile
- Delete the line “/swapfile swap swap defaults 0 0” from the /etc/fstab file which we have added earlier.
sudo vim /etc/fstab
Find and delete the “/swapfile swap swap defaults 0 0” from the /etc/fstab file.
- Finally, remove the /swapfile with the following command.
sudo rm /swapfile
In today’s tutorial, we have learned about the swap and its importance, we have also learned how we can setup the swap file on the Ubuntu 20.04 with some simple steps.
If you guys have any issues or queries related to this tutorial, let me know in the comment section.