The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM. It use swap space to move inactive pages from RAM.

Swap space can be created into Linux system in two ways, one we can create a dedicated partition for swap or another one is created a swap file and use it as swap space.

Usually, when we are creating VPS with any provider like Digital Ocean, Linode, etc., we don’t have the swap partition into the server, in this case, we need to create swap file to use as swap space in server.

In this tutorial, we have explained all necessary steps to create a swap file on CentOS 7 machine.

Prerequisites

Before beginning with this tutorial, make sure you have Debian 9 machine and a user to login into the system with sudo privileges.

Before creating a swap file, it is good to know swap file or swap partition is available into your system or not.

To check the swap space, you can use below command:

If the output of the above command is empty, it means your system doesn’t have swap space.

If your system has already configured swap space you will get output like below:

It is also possible to configure more than one swap space.

Swap File Creation

In this tutorial, we will add 1GB of swap space, if you want to create bigger swap space you can create by replacing 1G with any other size what you want.

To create a swap file, follow the below steps:

Step 1 – Create a file for swap

To create a file for swap space, use the “fallocate” command. Here we are creating 1GB swap file at /swapfile file. As shown below:

If in your system fallocate is not installed or you get a message like “fallocate failed: Operation not supported,” you can use below command to create swap file:

Step 2 – Change Permission

The swap file should only access by the root user; you can execute the below command to configure correct permission for “/swapfile.”

Step 3 – Create swap space

To create swap space, use the “mkswap” command, as shown below:

Step 4 – Activate swap space

To activate swap file use the “swapon” command, as shown below:

To activate swap space permanent by configuring file “/etc/fstab”:

Add following line into the file “/etc/fstab”:

Step 5 – Verify Swap space

To check the swap space is active or not, you can use “swapon” or “free” command, as shown below:

Configure Swappiness Value

Swappiness is the essential swap properties of Linux kernel, which is defined when the system will use swap space. Swappiness can have the value between 0 to 100, which is represent the percentage of RAM utilization, for example, if you set the value 20 for swappiness, it means the Linux kernel will use swap space when RAM is only 20 % available.

So, we can say the lower value of swappiness is to try to avoid the use of swap space.

The default value of swappiness is 30, which you can check by using the following command:

The default value (30) of swappiness is ok for the desktop system, but when you run the production system, try to set the lower value for production servers.

You can set the swappiness value by using “sysctl” command, for example, if you want to set the value of swappiness is 20 you should execute below command:

To change the value of swappiness permanently, you should change the parameter “vm.swappiness” into the “/etc/sysctl.conf” file and reboot the system.

Remove Swap File

To deactivate and remove the active swap space, follow the below steps:

Step 1 – Deactivate Swap Space

To deactivate swap space, you can use the “swapoff” command, as shown below:

Step 2 – Remove from /etc/fstab

Edit the “/etc/fstab” file and remove the line “/swapfile swap swap defaults 0 0” from the file and save

Step 3 – Delete swapfile file

You can delete the “/swapfile” file by using the rm command, as shown below:

Conclusion

Now you have learned how to create a swap space into the CentOS 7 machine if it not configured or you want to create another swap space.

If You Like What We Do Here On LinuxConcept, You Should Consider:

Stay Connected to: Twitter | Facebook

Subscribe to our email updates: Sign Up Now

 

We are thankful for your support.

Follow me

SATISH KUMAR

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.
Follow me