How to Add Swap Space on CentOS 7

How to Add Swap Space on CentOS

Swap Memory is part of a Physical Disk Either it’s HDD or SSD. It is being used when Physical RAM (Random Access Memory) gets full. When Linux based Server/System runs out of physical RAM, It moves all inactive pages to Swap Space. In This article, we will see how to add swap space on CentOS 7 server/systems.

Swap Memory is part of Physical Disk Either it’s HDD or SSD. It is being used when Physical RAM (Random Access Memory) gets full. When Linux based Server/System runs out of physical RAM, It moves all inactive pages to Swap Space.

Swap space can be created either as a separate partition or it can be in form of a file. Mostly on Linux Virtual Machines, mostly partition is not possible to the only thing left to create a swap file.

Read Also: How to install Fastpanel on CentOS 7

Before You Begin

Before adding Swap Space on CentOS, Just check if the system already has swap enabled. To check use the below command:

sudo swapon --show

If you see the out blank that means the system doesn’t have any enabled swap space.

If it has already been enabled you will see output something like below:

NAME      TYPE       SIZE USED PRIO
/dev/sda2 partition 1023M 768K   -2

It’s quite rare if you find more than one swap space on a single server.

Creating a Swap File

To create swap memory you should have logged in as either root user or Sudo privileged user. In this article, we will see adding 2G as swap space. if you want to add more than 2G just change it in the below command according to your requirement.

Below are the steps that we need to follow to add the swap space on CentOS 7.

Step 1 is to create the file that we will use as swap space. when the file will be created it will occupy Disk space, Run the below command to do create a swap space file:

sudo dd if=/dev/zero of=/swapfile bs=2GiB count=1

Step 2 As per the security aspect, we need to make sure only root users have only read-write permission. so just set the correct permission with the below command:

sudo chmod 600 /swapfile

Step 3 Now, convert that file into swap space by below command:

sudo mkswap /swapfile

Step 4 Run the below command to activate the swap:

sudo swapon /swapfile

Now we need to make these below changes in /etc/fstab file to open the file run below command:

sudo vim /etc/fstab

and then add the below line in /etc/fstab at the very bottom.

/swapfile swap swap defaults 0 0

Now check if /etc/fstab file is proper and according to syntax if something went wrong server can have a boot problem on the next reboot. so to check if all is good with the above changes, Run the below command:

sudo mount -a

Step 5 Now we have done all the changes that requires activating swap space. so just to verify if swap has been activated run either swapon --show or free -h as below:

sudo swapon --show
NAME      TYPE  SIZE   USED PRIO
/swapfile file   2G 425.8M   -2

You will get output as above this with swapon --show.

sudo free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        1.5G        1.4G         17M        867M        2.0G
Swap:          2.0G        425M        1.6G

Adjusting the Swappiness Value

Swappiness Value refers to the Linux Kernel parameter that handles how often the server should use the swap space. Swappiness value should be between 0 to 100 in which 0 indicates very low priority and higher values will increase the swap usages priority whenever possible. the default swappiness value is 30 in centos 7.

If you want to increase/decrease the swappiness default value in centos 7. we need to follow below steps with the following commands:

cat /proc/sys/vm/swappiness
30

As the above output shows the default value now the default value is okay for desktop machine and developer environment but the production environment server, we should keep this value lower. so we are going to set this 10:

To set the value to 10 run these below commands and make a few changes in the file:

sudo sysctl vm.swappiness=10

The above command changes will be there until we don’t restart the server so to make this value permanent we need to edit a system file /etc/sysctl.conf and append the line.

To open the file run the below command:

sudo vim /etc/sysctl.conf
vm.swappiness=10

Mostly value this value will depend on your server memory usages and workload. You should change this value in part to find the proper value for your system.

Removing a Swap File

If you want to delete and deactivate the swap file, you should follow the below steps:

Step 1. First, deactivate the swap memory by the below command:

sudo swapoff -v /swapfile

Step 2. Then, delete the line /swapfile swap swap defaults 0 0 we added in /etc/fstab file:

sudo vim /etc/fstab

Find and Remove the line /swapfile swap swap defaults 0 0.

Step 3. At very least remove the file by the below command:

sudo rm /swapfile

Conclusion

The above article explained the steps, how to configure and activate Swap space on the CentOS server.

If you hit a snag, leave a comment below.