Contents
What is Swap VPS?
Swap is virtual memory used on Linux operating system.
When the Linux server works, if it runs out of RAM, the server will use a part of the hard drive to make memory for normal operating processes.
In case the server has no swap, if you run out of RAM you will get the error Establishing a Database Connection. Cause when running out of RAM MySQL service will stop.
In this article, I will guide you to create Swap in case the server has not been allocated.
If you have created a swap and still running out of RAM is common, it’s time to upgrade more RAM.
Note: VPS uses OpenVZ virtualization technology, you may not be able to create swap because the system has already activated it.
In this article, I will run commands with a non- root user, so many commands need to start with sudo to have root privileges. For security reasons, I recommend not using the root user. Refer to Security for Linux Servers .
Instructions for creating Swap on Linux server
First you need to check if your server has swap with the following command:
swapon -s
If no information is returned as above, it means that your server does not have a swap.
You can check more with the command

Next, check the available hard drive space with the following command: ( df -h )

The memory rule allocated for swap is twice the RAM. For example, if your RAM is 512MB, you will take 1GB of the hard drive as swap.
Next create swap with the command below. For example below, I create swap 1GB (count=1024k). You adjust it to suit your situation.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
Next, create a swap partition with the following command:
sudo mkswap /swapfile
Next enable swap with the following command:
sudo swapon /swapfileCheck the swap again and you will see the following:
swapon -s
Swap you create as above will be lost if the server is restarted. To maintain you need to edit the /etc/fstab file with the nano editor:
sudo nano /etc/fstabCopy and paste the following line at the end of the file. Then press Ctrl + O and then Enter to save the file, then press Ctrl + X to exit.
/swapfile swap swap defaults 0 0Increase the security of swap with the following command:
sudo chown root:root /swapfile
sudo chmod 0600 /swapfileConfigure swappiness
Swappiness is the priority to use swap when the percentage of RAM remaining equals the value of swappiness. Swappiness has a value in the range of 0-100.
- swappiness = 100: swap will be prioritized as RAM
- swappiness = 60: swap is used when RAM is 60%
- swappiness = 60 swap used when RAM is 10%
- swappiness = 0: swap is only used when running out of RAM.
Swap speed cannot be compared with RAM. So you set the swappiness price close to zero to take advantage of RAM. The recommended value is 10.
Check your server swappiness value with the following command:
cat /proc/sys/vm/swappinessReset the swappiness price with the following command
sudo sysctl vm.swappiness=10Now you use the above swappiness check command to check again.
To keep the swappiness parameter on server reboots, you need to edit vm.swappiness at the end of /etc/sysctl.conf .
sudo nano /etc/sysctl.confIf not, add this line vm.swappiness = 10 at the end of the file.
Now restart the VPS. Check swap and swappiness.
sudo swapon -s
sudo cat /proc/sys/vm/swappinessCentos 7.2 has a tune profile process so it overwrites vm.swappiness every reboot. You need to check that the profile is being overwritten to reconfigure.
grep vm.swappiness /usr/lib/tuned/*/tuned.confThe following results:

At this point, open the file /usr/lib/tuned/virtual-guest/tuned.conf and change vm.swappiness = 30 to vm.swappiness = 10.
sudo nano /usr/lib/tuned/virtual-guest/tuned.confChange swap size
If you followed the instructions above to create a swap file, you can follow these steps to resize the swap.
Turn off swap
sudo swapoff /swapfileDelete the swap file
sudo rm -f /swapfileCreate a new swap file with the size you want for example create 2GB
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048kCreate a swap partition:
sudo mkswap /swapfileEnable swaps:
swapon /swapfileSecure swap with the following 2 commands:
sudo chown root:root /swapfile
sudo chmod 0600 /swapfileCheck if the swap is correct:
swapon -sThe swap configuration section when restarting the server and setting the swappiness you don’t need to change.
Now restart the server and check again.
If you have problems with swap creation, leave a comment below.

