How to Create and Add Swap Space on Linux (CentOS, Ubuntu, CWP, and VestaCP)

How to Create and Add Swap Space on Linux (CentOS, Ubuntu, CWP, and VestaCP)


This guide explains how to add swap space to Linux-based operating systems such as CentOS, Ubuntu, and RHEL, specifically for systems managed with CentOS Web Panel (CWP) or VestaCP. Adding swap space is essential for preventing memory-related issues like crashes and instability, particularly when a server runs out of physical memory (RAM).

What is Swap Space?

Swap space in Linux is used when the system’s physical memory (RAM) is fully utilized. When this happens, inactive memory pages are moved to the swap space on the hard disk. Although swap space provides a temporary memory buffer, it is significantly slower than RAM due to the hard disk’s lower access speed. While swap can improve system stability and performance on systems with limited RAM, it is not a substitute for upgrading physical memory.

Why Add Swap Space?

  • Prevent Out-of-Memory (OOM) Errors: Swap helps prevent crashes and memory-related instability.
  • Increase Memory Availability: It allows the system to run more processes concurrently.
  • Enhance System Stability: Swap reduces the likelihood of server hangs or non-responsiveness.

This guide applies only to KVM-based virtual servers and dedicated servers. It is not compatible with OpenVZ or other container-based servers.

How to Add Swap Space

Step 1: Preliminary Commands

Run the following commands one by one to prepare your server for swap space creation:

cd /var
touch swap.img
chmod 600 swap.img

Step 2: Create a Swap File

In this step, we will create a swap file of 1GB (1024MB). You can adjust the size by modifying the count value.

For 1GB Swap:

dd if=/dev/zero of=/var/swap.img bs=1024k count=1000

For 2GB Swap:

dd if=/dev/zero of=/var/swap.img bs=1024k count=2000

For 3GB Swap:

dd if=/dev/zero of=/var/swap.img bs=1024k count=3000

Once executed, the result will indicate the successful creation of the swap file:

[root@srv1 var]# dd if=/dev/zero of=/var/swap.img bs=1024k count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 3.30777 s, 317 MB/s

Step 3: Configure the Swap File

Next, we need to configure the swap file. Run the following commands to set up the swap:

mkswap /var/swap.img

The system will confirm that the swap space is ready for use:

[root@srv1 var]# mkswap /var/swap.img
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=5813e8e7-1034-4700-84c2-c06905e26535

Step 4: Enable Swap

To activate the swap, execute:

swapon /var/swap.img

You can verify that the swap is active by running:

free -h

The result will look similar to this:

free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        260M        144M         27M        1.6G        1.5G
Swap:          999M          0B        999M

Step 5: Enable Swap on Boot

To ensure the swap space is available after a reboot, add an entry to the /etc/fstab file. Run the following command:

echo "/var/swap.img    none    swap    sw    0    0" >> /etc/fstab

That’s it! Your swap space is now enabled and will persist across reboots.

Managing Swap Space

To modify or remove swap space, use the following steps:

1. Disable the Existing Swap:

swapoff -a

2. Delete the Swap File:

rm -rf /var/swap.img

3. Recreate Swap Space:

Follow the steps outlined above. If you already added the /etc/fstab entry, you can skip Step 5.

Summary

Adding swap space is a practical solution to enhance the stability and performance of Linux servers, especially those with limited physical memory. Swap acts as an overflow for RAM, helping to prevent out-of-memory errors, system crashes, and server hangs.

While it is not a substitute for upgrading hardware, it provides a cost-effective way to handle memory-intensive workloads. By following the steps outlined in this guide, you can efficiently create, enable, and manage swap space on your KVM-based or dedicated server. Remember to monitor your system’s memory usage to ensure optimal performance and make adjustments as needed.