When the physical RAM memory is full and Linux system runs out of RAM then it uses some area of hard disk space to store data that area is called Swap space. When the operating system no longer can put data on RAM then inactive pages are moved from the RAM to the swap space. In this tutorial, we will learn how to add swap space on CentOS 8 system.
Before you start, you must have login with root or user account with sudo privileges.
After that, you should check either swap space is already enabled or not on you system by below command:
sudo swapon --show
It will show as below:
NAME TYPE SIZE USED PRIO /dev/sda7 partition 2.3G 0B -2
If it will not display any output in terminal means swap space is disabled on your system. Following are the steps for adding a swap file on CentOS 8 systems.
Step 1 : Create a Swap File
In this example, we will create swap file with size of 512 MB to the system. You can add more space just by replacing 512 MB with another number. Run the following command:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
Step 2 : Set Permission
Next, set the file permissions to allow only the root user to read and write the swap file:
sudo chmod 600 /swapfile
Step 3 : Set up swap space
Run below command to set up swap space on your system using mkswap:
sudo mkswap /swapfile
Step 4 : Activate the swap
Activate the created swap file as swap space using the following command:
sudo swapon /swapfile
Step 5 : Verify Swap Space
To confirm and verify swap space run below command:
sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 512M 10.9M -1
You can also verify using free command, as shown below:
sudo free -h
total used free shared buff/cache available Mem: 2048M 657M 283M 102.3M 246M 1010M Swap: 512G 100M 256M
Make the swap file permanent
When your system will reboot the server will not remember the above changes permanently. To make the above changes permanent add a swap entry in /etc/fstab file:
sudo nano /etc/fstab
Paste the following line:
/swapfile swap swap defaults 0 0
Change Swappiness Value
Swappiness is a Linux kernel property that used to configure how often the swap space should be used. Value of swappiness can have between 0 and 100. System will avoid using swap space if value is set to lower while bigger value use swap space much often. The default swappiness value on the CentOS system is 30. To check swapiness value run following command:
The swappiness value of 30 is OK for desktop and development machines, You should change for production servers, you may need to set a lower value.
For example, to set the swappiness value to 15, type:
sudo systemctl vm.swappiness=15
The above swappiness value will not persist after a reboot. So to make the changes permanent edit /etc/systemctl.conf file:
sudo nano /etc/systemctl.conf
Then add the following line to the file:
Remove a Swap File
Perform the following steps to remove swap file.
Run below command to deactivate the swap space:
sudo swapoff -v /swapfile
Remove the swap entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
Now delete the physical swap file:
sudo rm /swapfile
You successfully learned how to create a swap file and activate and configure swap space on your CentOS 8 system.
If you have any queries about swap file, leave comment below.