Skip to content

How to enable Swap for a Linux VPS Without Interruption

This guide explains how to add Swap memory to a VPS that is currently under load and lacks any Swap. Follow these steps carefully to avoid disruptions.


Step 1: Check Current Swap Status

First, check the current Swap status:

sudo swapon --show

If the output is empty, it means the server has no active Swap.


Step 2: Check Disk Space

Ensure there is enough free space on the disk for creating a Swap file. Use the following command:

df -h

Step 3: Create a Swap File

Create a new Swap file. In this example, we’ll create a 2GB Swap file:

sudo fallocate -l 2G /swapfile

If the above command is not supported, use:

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

Step 4: Set File Permissions

Secure the Swap file by restricting access:

sudo chmod 600 /swapfile

Step 5: Configure the File as Swap

Set the newly created file as a Swap file:

sudo mkswap /swapfile

Step 6: Enable Swap

Activate the Swap file:

sudo swapon /swapfile

Now verify the Swap status again to ensure it is active:

sudo swapon --show

Step 7: Make Swap Permanent

To ensure Swap remains active after a reboot, add the Swap file to the fstab file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Final Notes

  • Appropriate Swap Size: Choose the Swap size based on your server’s needs and available resources.
  • Monitor Swap Usage: After creating the Swap, monitor its usage using tools like htop or free.

We hope this guide helps you efficiently manage your server! 😊


Last update: December 28, 2024

Comments