How to Enable Swap on a VPS?

Swap is a special space on the hard drive that the operating system uses when it runs out of RAM. Virtual Private Servers (VPS) often require swap to ensure stable application performance. In this article, we will show you how to enable swap on a VPS and configure it correctly.

1. sudo fallocate -l 1G /swapfile

First, we create a swap file of 1 GB using the fallocate command.

2. sudo chmod 600 /swapfile

VPS хостинг

Виртуальные серверы с гарантированными ресурсами

Выбрать VPS

Then, we set the correct permissions on the swap file so that only the owner has access to it.

3. sudo mkswap /swapfile

After that, we initialize swap in the /swapfile file.

4. sudo swapon /swapfile

Now we activate swap so that the operating system starts using it.

5. echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

Finally, we add swap information to the /etc/fstab file so that it is automatically mounted when the system boots.

You should now have swap enabled on your VPS. Don’t forget to check its functionality with the command sudo swapon --show.

We hope this article helped you understand how to enable swap on a VPS. Good luck!