How to add swap space on Ubuntu 20.04 Operating System

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 or add swap space on Ubuntu 20.04 machine.

Prerequisites

Before beginning with this tutorial, make sure you have Ubuntu 20.04 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:

$ sudo swapon --show

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:

$ sudo fallocate -l 1G /swapfile

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:

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

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.

$ sudo chmod 600 /swapfile

Step 3 – Create swap space To create swap space, use the mkswap command, as shown below:

$ sudo mkswap /swapfile

Step 4 – Activate swap space To activate swap file use the swapon command, as shown below:

$ sudo swapon /swapfile

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

$ sudo vim /etc/fstab

Add following line into the file /etc/fstab:

/swapfile  	swap 	swap 	defaults 	0 	0

Step 5 – Verify Swap space To check the swap space is active or not, you can use swapon or free command, as shown below:

$ sudo swapon --show
$ sudo free –h

Configure Swappiness Value

Swappiness is the essential swap properties of Linux kernel, which is defined when a system will use swap spaceSwappiness 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 60, which you can check by using the following command:

$ cat /proc/sys/vm/swappiness

The default value (60) 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:

$ sudo sysctl vm.swappiness=20

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

vm.swappiness=10

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:

$ sudo swapoff –v /swapfile

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:

$ sudo rm /swapfile

Conclusion

Now you have learned how to create a swap space into the Ubuntu 20.04 machine. If it not configured or you want to create another swap space.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles