How to Create a Swap File on a Linux VPS

It is important to create and configure a swap file on the Linux Virtual Private Server (VPS) to enhance the performance and stability of the server. The VPS swap space acts as a virtual RAM, allowing the server to process heavy operations when physical RAM is low. Efficient creation and management of the swap space can help avoid application crashes and system slowdowns even when the RAM is in high demand. 

This tutorial will demonstrate how to create and manage the swap space on your Linux VPS.

How to Create a Swap File on a Linux VPS

Use fallocate /swapfile -l 2G to create swap space for a Linux VPS. Then, set permissions using chmod 600. Then, you can enable it with mkswap and swapon to use the space immediately. Follow the steps below for creating a swap file on a Linux VPS:

Step 1: Check the Swap Information

Check your system for existing swap space before creating a new swap file. For most VPS setups, a single swap file is sufficient. Run the following command to verify current swap usage.

sudo swapon --show
check current swap usage

If the output is blank, there is no swap room available on your system. You can use the command below to verify swap space on your system.

free -h
verify swap space on your system

The swap with 0B indicates that the system does not have swap space.

Step 2: Check Available Space 

Check the available disk space before adding swap space. You can check if your disk space is sufficient to add swap by using the following command.

df -h
Check Available Space 

The system will mount the hard drive as /dev/sda.

Step 3: Creating a Swap File

After you have determined the amount of disk space available, it is time to create your swap file. This will depend on what your system requires. In general, you can create a swap file based on your RAM. You can either keep your swap space equal to the RAM size or double it.

As an example, here is how I create a 1.5GB swap space for a server with 2GB RAM using the fallocate command.

sudo fallocate -l 1.5G /swap_file_name.img
Creating a Swap File

Note: You can change the swap_file_name and swap_space according to your preference.

Step 4: Verify the Swap File is Created

You can verify the swap file using the following command once you have created it.

ls -lh /swap_file_name.img
Verify the Swap File is Created

How to Adjust Swappiness Value on a Linux VPS

Use commands such as swapon, free -h, and swapoff to manage swap space in a Linux Virtual Private Server. You can adjust performance by changing the “vm.swappiness” setting in the system configuration.

Step 1: Enable the Swap File on Linux VPS

You must now convert the swap file that you created to the swap space in order for your system to detect it. Before you proceed, you should lock down the permissions of the swap files so that only root users can read their content. You can set the swap file permissions on a Linux VPS using the following command.

sudo chmod 600 /swap.img
set swap file permissions

You can mark the swap file on a Linux VPS as a swap area by using the command below.

sudo mkswap /swap.img
mark swap file

After this, you can then enable swap space on your system by using the following command.

sudo swapon /swap.img
Enable the Swap File on Linux VPS

Step 2: Make a Permanent Swap Space

The system applies the changes you made above to enable swap space only to the current session. You will lose the changes if you restart the system. However, you can make them permanent by adding the file swap to the fstab. The command below will open the fstab on a Linux VPS.

sudo nano /etc/fstab

Add the following line to the file and save it using CTRL+X. Then add Y, and press Enter.

/swap.img none swap sw 0 0
Make a Permanent Swap Space

Step 3: Verify Swap Space Information

The following command will allow you to check swap information for a Linux VPS.

sudo swapon --show
Verify Swap Space Information

You can also check the swap space information by using the free command with the h flag.

free -h
check the swap space information

Step 4: Adjust Swappiness Value

The swappiness control parameter determines how often the system uses swap files (disk space). The swappiness value is between 0 and 100. A value near 0 makes the system avoid using the disk unless necessary. If the value is close to 100, the system swaps programs to the disk immediately. You can check the default swappiness value of 60 for Linux VPS systems by running the following command.

cat /proc/sys/vm/swappiness
Adjust Swappiness Value

The swappiness close to 60 value is a good choice for a Linux VPS desktop. For a server, you can use the following command to change the value so that it is close to zero.

sudo sysctl vm.swappiness=20
change Swap Value for server

When you use the sysctl command, the system applies the swappiness value only to the current session. After you reboot the system, it removes these changes. You can set the swappinness permanently by opening the “sysctl.conf” on Linux VPS using the below command.

sudo nano /etc/sysctl.conf

Add the swappiness to the file at the end and save it.

vm.swappiness=20
Add swappiness to the file

Step 5: Adjust Cache Pressure Value

Cache pressure is a key factor in determining how quickly the system Kernel can release cached memory to other processes on your system. You can change the default cache pressure value from 100 to 50-200. Use the following command to check the cache pressure on Linux VPS.

cat /proc/sys/vm/vfs_cache_pressure
Adjust Cache Pressure Value

Use the following command to change the cache value.

sudo sysctl vm.vfs_cache_pressure=120
change the cache value

Note: To set the cache-pressure value permanently on the system, add the value in the systl.conf and save it. For better performance and stability, you can manage and create it with commands such as fallocate and mkswap.

How to Remove a Swap File on Linux VPS

To remove the swap file on a Linux VPS, first deactivate the space using the following command.

sudo swapoff -v /swap.img
deactivate the space

Remove the entry for the swap file from the file /etc/fstab. You can now remove the swap files from your system using the following command.

sudo rm /swap.img
 remove the swap file

Conclusion

The swap space of a Linux virtual private server supports RAM by acting as virtual memory when RAM is heavily used. To create a swap file on a Linux VPS, start by checking if swap already exists using the command swapon --show or free -h. If none is available, create a swap file by running sudo fallocate -l 2G /swapfile (replace “2G” with the desired size), then secure it with sudo chmod 600 /swapfile. Next, format it as swap using sudo mkswap /swapfile and enable it with sudo swapon /swapfile.

To make the change permanent, add the line /swapfile none swap sw 0 0 to /etc/fstab. You can verify it with swapon --show or free -h. To adjust swap usage, modify the swappiness value using sudo sysctl vm.swappiness=10 or by adding vm.swappiness=10 to “/etc/sysctl.conf”. Properly managing swap ensures stable performance by preventing memory exhaustion on your VPS.

FAQs

What does it mean to use a swap file in Linux?
Why do I have a swap file on a Linux VPS?
What is the difference between a swap file and a swap partition?
How much swap space should we create?
Will adding a swap slow down my VPS? 
How can I check if swap is active on my Linux VPS? 
Can I resize or remove a swap file later? 

Related Post

How to Fix Unable to Locate Package in Kali L...

Kali Linux is widely known as an operating system used ...

How to Install Deb Files / Packages on Ubuntu

The .deb packages are the standard format for installin...

Unlocking the power of Linux Pipe Command

The pipe command symbolized as the bar (“|”) enable...

How to Check CentOS Version

Understanding which version of CentOS you're running is...

Exploring the at Command in Linux

The at command in Linux is used to schedule tasks to ru...

How to Install GCC Compiler on Linux

GCC (GNU Compiler Collection) is a widely used compiler...

Leave a Comment