Create a Swap Partition on CentOS | VPS Setup Guide for Beginners | Ep. 2

YouTube video

Swap memory or swap partition is a reserved part of your system storage that can be used as a memory when the physical memory or RAM is full. When a Linux system runs out of the physical memory, it moves inactive data chunk from the memory to the swap space allowing the system to free up some RAM space for the active processes.

Even if your system has sufficient memory, it’s always a good idea to have a swap partition. Having the swap space allows the system to keep running when a memory hungry process consumes all the physical memory space.

However, it is not good to allow the system to allow frequently swapping as it reduces system performance.

If your system is running out memory very frequently, you may need to upgrade your system with bigger RAM, or you may want to overhaul your code to find where it is leaking the memory. If you need to upgrade your VPS, I recommend Contabo as they provide the cheapest virtual machines with an exemplary configuration and service.

Install Nano Text Editor

In this tutorial, we will need to edit the files using a text editor. Nano editor makes the text editing very easy. Read how to install nano editor if your CentOS system already doesn’t have Nano installed.

How to create a swap partition on CentOs?

At first, you need to check if your system already has a swap space. Access your server with an SSH client and write the following command.

free -m

If the value of ‘Swap:’ in the output shows zero, that means there is no swap space created.

              total        used        free      shared  buff/cache   available
Mem:           7820         112        7643           8          65        7537
Swap:             0           0           0

Now, let’s create the swap file. I’m going to create a 4GB file. However, you may change it depending on your needs. The common practice is that the swap file size should be half to the physical memory. But, it should not be more than 4GB.

sudo dd if=/dev/zero of=/swap count=4096 bs=1MiB

Now change the edit permissions:

sudo chmod 600 /swap

and make the swap file usable:

sudo mkswap /swap

Now enable the swap partition:

sudo swapon /swap

Now, we can test if the swap partition was created and running correctly. Run the memory checking command again:

free -m

It should return a result like this:

              total        used        free      shared  buff/cache   available
Mem:           7820         112        7643           8          65        7537
Swap:          4095           0        4095

The swap partition is created, and it’s running correctly. But, the partition will not automatically be activated if you reboot the system.

Activate the swap partition permanently

To make the swap partition automatically mounted after each reboot, execute the following command to add it to your system’s fstab config.

sudo echo "/swap swap swap sw 0 0" >> /etc/fstab

You can test if the swap partition is working correctly after rebooting the system, reboot the system using the following command.

sudo reboot

Your SSH client’s connection to the server is now disconnected. Close the SSH client and reconnect to the server and execute the memory command.

free -m

If the swap value is not 0, you’ve permanently created the swap partition to your Linux system.

Change the swappiness configuration on CentOS

Swappiness is a Linux kernel property that defines how often the kernel swaps the pages from physical memory to the swap space. We can set a value between 0-100. Higher the value is, the kernel will be more aggressive to move the pages from RAM to the swap partition.

We know that swap partition increases the system stability in stressful conditions, but in exchange, it reduces the system performance.

So, having a higher value for swappiness is not expected in an optimum system environment. We will allow the system to swap the pages only when it’s deadly needed.

Check the current swappiness value

We can check the value using the following command:

cat /proc/sys/vm/swappiness

The default value for your CentOS system should be 30, and the output should look like this:

30

We can check the swappiness value with another command, and that is:

sysctl vm.swappiness

The output should be the following:

vm.swappiness = 30

Changing the swappiness value

Now, run the following command to change the swappiness value to 1. We will change it to 1, so the system swaps only when it’s in extreme condition.

sudo sysctl vm.swappiness=1

Now, lets check the swappiness value again:

sysctl vm.swappiness

The output should be the following:

vm.swappiness = 1

You’ve changed the swappiness value. But, the change is not permanent yet. It will change the swappiness value to the default one in the next system reboot.

To make the value permanent, edit the sysctl.conf file and change the swappiness value to 1 there.

To do that, open /etc/sysctl.conf file with Nano text editor:

sudo nano /etc/sysctl.conf

Look for vm.swappiness=30 and change the value to 1. If you don’t find that, append the file with the following line on Nano editor:

vm.swappiness=1

Now, rebooting the system shouldn’t change the value to the default one that was 30.

If you have any confusion or questions regarding this tutorial or are delighted, leave a comment.

Was the tutorial helpful? Click to rate it!
9 vote(s), avg: 5 out of 5

Leave a Comment