Linux Linux Commands

How To Add Swap Space On CentOS 8

How To Add Swap Space On CentOS 8
Written by itsubuntu

Tutorial to add swap space on CentOS 8

Previously we covered an article on adding swap space on Ubuntu 20.04 LTS. In this article, we are going to do the same thing but with CentOS 8 Linux.

When RAM is full, It takes inactive pages from the RAM. Swap space is used when RAM is full in your system.

How To Define Swap Space In CentOS 8

  • Swap space 3 times of the RAM if your system has less than 2 GB RAM
  • Swap space same size of the RAM if your system has systems has 2 to 8 GB RAM
  • Systems with more than 8 GB RAM then your Swap space will be at least 4 GB of Swap

How To Add Swap Space On CentOS 8

At first, we need to create a swap file that will serve as swap space in your CentOS 8. We are creating a Swap file with 2 G.

sudo fallocate -l 2G /swapfile

Now, use the dd command to create the swap file:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152

We are setting permission right to 600.

sudo chmod 600 /swapfile

Create a Linux swap area on the file:

sudo mkswap /swapfile

Run the following command to activate the swap file.

sudo swapon /swapfile

Verify that the swap is active.

sudo swapon --show

Or,

sudo free -h

Now, You can make the change permanent by adding a swap entry in the /etc/fstab file:

sudo nano /etc/fstab

and paste the following line:

/swapfile swap swap defaults 0 0

Swappiness Value In CentOS 8

Swappiness is a method that defines how often your Linux system will use the swap space.  Default swappiness value is30 in CentOS 8. You can check the current value with the following command.

cat /proc/sys/vm/swappiness

Note: It can have a value between 0 and 100. A lower value is better as it will make the kernel try to avoid swapping whenever possible and at the same when you have a higher value, the kernel will try to use Swap Space like never before.

Meanwhile, if you want to set the swappiness value, then you need to open the /etc/sysctl.conf file and add the following line.

vm.swappiness=20

Run the following command to delete or deactivate the Swap value in CentOS 8

sudo swapoff -v /swapfile

Now, you can remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Run the following command to remove the actual swapfile file with the following command:

sudo rm /swapfile

About the author

itsubuntu

Leave a Comment