How to Add Swap Space on CentOS 7

Table of Contents
Add Swap Space on CentOS 7
When operating system no longer can put data on RAM then it uses some area of hard disk space to store data that area is called Swap
space. When the system goes on low memory mode then it uses Swap
space to store data. Basically, it swaps the data on RAM of an ideal program to swap area. In this tutorial, we are going to learn how to add swap space on CentOS 7.
Prerequisites
Before you start to add swap space on CentOS 7. You must have the root user account credentials of your system.
Then you should check if on your system swap
space is already enabled or not running below command:
sudo swapon --show
The output should look like:
NAME TYPE SIZE USED PRIO /dev/sda7 partition 2.3G 0B -2
If you get no output means swap space is not enabled on your system. Check following steps to add swap space on your system.
Create Swap File
Here in this tutorial, we will add 512 MB of swap space to the system. To add more space simply replace 512 with another number.
Now run following command to create a swap file of 512 MB.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
Set the permissions for the swap file running below command:
sudo chmod 600 /swapfile
Run below command to setup swap space on your system using mkswap
:
sudo mkswap /swapfile
Activate the created swap file as swap space using the following command:
sudo swapon /swapfile
Make the swap file permanent
When the system reboots the server will not save above changes permanently. To make above changes permanent use following steps
First backup /etc/fstab
file to /etc/fstab.bak
:
sudo cp /etc/fstab /etc/fstab.bak
Now run below command to make the swap file permanent:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify Swap Space
To confirm and verify swap space run below command:
sudo swapon --show
The output should look like:
NAME TYPE SIZE USED PRIO /swapfile file 512M 10.9M -1
You can also check the space by running the following command:
free -h
The output should look like:
total used free shared buff/cache available Mem: 2048M 657M 283M 102.3M 246M 1010M Swap: 512G 100M 256M
Changing Swappiness Parameter
The swappiness parameter is used to configure how often the swap space should be used. The swappiness
parameter value lies between 0 to 100. As the value is set to smaller the system will avoid using swap space and if the value is bigger then the system will use swap space much often. The default swappiness value on the CentOS system is 30. The value 30 is ok for desktop machine while you should change it for the production system.
However to check swapiness value run following command:
cat /proc/sys/vm/swappiness
To set the swappiness value to 15, run below command:
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:
vm.swappiness=10
Deactivate swap file
Use following instructions to deactivate swap space.
To deactivate swap
space run following command:
sudo swapoff -v /swapfile
Edit /etc/fstab
file and remove /swapfile swap swap defaults 0 0
line from it:
sudo nano /etc/fstab
Now delete the swap file:
sudo rm /swapfile
Conclusion
You have successfully learned how to add Swap Space on CentOS 7. If you have any queries regarding this then please don’t forget to comment below.
LATEST POSTS
-
How to Install Redis on Debian 9
-
How to Install Ruby on CentOS 7
-
How to Install IntelliJ IDEA IDE on Linux Mint 19
-
How to Install Opera Browser on CentOS 7
-
Generate Random Unique String in Laravel
-
How to Install Go on Debian 9
-
How to Install Adobe Flash Player on Debian
-
How to Install Laravel PHP Framework With Nginx on CentOS 7
-
How to Install Docker Compose on CentOS 7
-
How to Install and Use Docker on Ubuntu 18.04
-
How to Rename Local and Remote Branches in Git
-
How to Copy, Cut, Paste in Vim or Vi