Add Swap partition on an Ubuntu server – 2 different ways explained

It’;s common that many services fail due to memory crunch on the server.

That’;s where the role of swap space come into picture. And, it’;s true that swap partition can improve the performance and stability of the system.

At Bobcares, we help server owners to add or expand swap partition as part of our Server Management Services.

Today, let’;s discuss the 2 different ways to add swap partition on an Ubuntu server.

 

What is Swap and Why it’;s needed?

Before we move on to the steps to add swap partition on Ubuntu, let’;s first understand what is swap and the role of swap space.

Swap space is actually a virtual memory. In other words, swap space is like adding RAM, but not physical memory. It’;s more useful for systems with low memory, but have resource intense applications.

If physical memory is full and the server needs more memory to run applications, it moves the inactive pages from memory to swap space. The system uses this free space for other active programs.

 

Add Swap partition on Ubuntu –; How much is needed?

Ideally, our Server Experts always advise to keep the swap memory equal to the size of physical memory at minimum and double the size of the physical memory at maximum. For example, if the physical memory on the server is 16GB, swap space should be between 16 –; 32 GB.

For Ubuntu systems with RAM less than 1GB, the swap should be atleast the size of RAM and atmost double the RAM. Similarly, for systems with more than 1GB RAM, we recommend swap space should be atleast equal to the square root of RAM size and atmost double the RAM.

Add Swap partition on Ubuntu –; How we do it?

Now, let’;s see how our Server Administrators add swap partition on an Ubuntu server.

Before, we start adding swap on the server, we first confirm whether swap partition is configured on the Ubuntu server. For that, we use the below command.

swapon --show

 

If the system doesn’;t display any output, the swap partition on the system is not enabled. In addition to that, our Support Engineers verify the same using the free command.

 

add swap partition ubuntu

How to verify the Swap information?

 

We check the Swap section here. The 0 value shows that swap on the server is not enabled. The next step is to create swap space on the server.

We can either use a dedicated hard drive partition to add new swap space or create a swap file and use it as swap space. Let’;s see both cases in detail.

 

1) Create a swap partition

If we have an extra hard disk or free space on an existing disk, we can go ahead and create a swap partition. But, be careful here. Any wrong step can result in major data loss. That’;s why, we recommend to have a working backup of the data files before moving ahead.

Our Support Experts use the fdisk utility to create new partitions. For example, we use the following command to create a new partition from /dev/sda.

fdisk /dev/sda

 

Most importantly, we assign the partition value as 82, so that system detects it as a swap partition. Alternatively, customers can use tools such as GParted, cfdisk, etc. for partitioning and formatting.

Further, we convert the new partition as swap area using the below command.

mkswap /dev/sda1

 

And, enable the swap space using the below command.

swapon -s /dev/sda1

 

Finally, to make this swap space available even after reboot, we make some modifications in the /etc/fstab file.

cat /etc/fstab
/dev/sda1 none swap sw 0 0

 

Alternatively, in some systems we add the UUID(Universally Unique Identifier) for the swap partition in the /etc/fstab file.

UUID=615b3be3-719c-5e21-a944-a033114f3cf5 none swap sw 0 0

 

Our Support Engineers get the UUID information of a partition using the blkid command. We always prefer to use the UUID, because it remains constant even if we move the partition around or rename it.

Finally, we verify the new swap space using the below commands.

swapon -s
free -m

 

However, consider scenarios such as Ubuntu running on a virtual machine and there’;s no swap partition or you have no additional space on the server. Then, the only option is to create a swap file and use it as swap space on the server.

 

2) Create a Swap file

Let’;s now see the steps to add swap partition on Ubuntu using swap file. This involves a series of steps. Let’;s discuss them in detail.

a) Select the partition

Firstly our Support Engineers check the current disk usage on the system using the below command.

df -h

 

The output will be like this.

 

add swap partition ubuntu

Output of df -h command

 

This gives us an idea of the disk usage on each partition and the total free space. Based on this, we select a partition that can hold the swap file.

 

b) Create a swap file

Once we’;ve confirmed the available disk space, we create a swap file within the selected filesystem. Our Support Engineers usually use fallocate utility to create swap file.

For example, if we need to create a swap file named “swapfile”; of size 1GB, we use the following command.

fallocate -l 1G /swapfile

 

However, fallocate utility may not be installed on some systems. In such cases, we use dd command to create a swap file.

dd if=/dev/zero of=/swapfile bs=1M count=1024

 

Further, we verify if correct space is allocated to the /swapfile using the below command.

ls -lh /swapfile

 

Most importantly, we ensure that there are strict permissions for this file, so that only users with root privileges can read the contents. That is, we change the permission of swapfile to 600.

chmod 600 /swapfile

 

c) Enable the swap file

Further, we use the mkswap utility to convert the /swapfile as Linux swap space.

mkswap /swapfile

 

add swap partition ubuntu

How to convert swap file as swap space?

 

Later, we activate this swap file so the system can now use it.

swapon /swapfile

 

Once done, we check the swap status on the server using the below command.

#swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -1

 

In addition to that, we double check the same using the free command as well.

 

d) Make the swap permanent

Finally, our Server Experts make changes to the /etc/fstab file to make the swapfile available after every server reboot. For instance, we add the following entry in the /etc/fstab file to make the swap file permanent.

/swapfile none swap sw 0 0

 

You’;re done, reboot the system to use the new swap space.

[Trouble adding swap space on your server? Click here and get one of our Support Experts to look into your issue.]

 

Add Swap partition on Ubuntu –; Post checks to be done

Now we know how to add swap partition on Ubuntu. However, our Dedicated Engineers do certain tweaks to improve system performance while using swap memory.

1) Swappiness

Swappiness is a kernel parameter that defines how often the kernel moves processes from physical memory to swap space. It can have values ranging from 0 to 100. And, the default value in Ubuntu is 60. The higher the value, the more often the system uses swap. We can get the current swappiness value of the system from the /proc/sys/vm/swappiness file.

Our Support Engineers define the swappiness value after analyzing the type of applications, memory usage of the applications, server work load, etc. For instance, to set the swappiness value permanently to 20, we add the following code in /etc/sysctl.conf file.

vm.swappiness=10

2) Cache Pressure settings

Another kernel parameter that affects swap performance is  VFS Cache Pressure settings. This controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. We can get the current value of this parameter from /proc/sys/vm/vfs_cache_pressure.

Our Support Engineers adjust the vfs_cache_pressure to an optimum value without affecting the system performance. For instance, we change this value to 50 by adding the following code in the file /etc/sysctl.conf.

vm.vfs_cache_pressure=50

 

[Need help in tweaking the kernel parameters to improve the system performance? Our Experienced Server Administrators can help your here. We are available 24/7.]

Conclusion

In short, adding swap space on the server helps server owners resolve out of memory problems to a great extent. However, it has a downside as well. Since swap space resides in hard disk, the access time of swap is slower. Today, we’;ve discussed how our Server Experts add swap partition on Ubuntu servers and tweak it’;s parameters to improve system performance.

The post Add Swap partition on an Ubuntu server –; 2 different ways explained appeared first on Bobcares.