Instructions for creating Swap for Linux server

Tutorials 0 lượt xem

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
how to create swap file for Linux server 1

If no information is returned as above, it means that your server does not have a swap. 

You can check more with the command

how to create swap file for Linux server 2

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

how to create swap file for Linux 3 . server

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
how to create swap file for Linux server 4

Next, create a swap partition with the following command: ​

sudo mkswap /swapfile
how to create swap file for Linux server 5

Next enable swap with the following command:

sudo swapon /swapfile

Check the swap again and you will see the following: 

swapon -s
how to create swap file for Linux server 6

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/fstab

Copy 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 0

Increase the security of swap with the following command: 

sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

Configure 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/swappiness

Reset the swappiness price with the following command 

sudo sysctl vm.swappiness=10

Now 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.conf

If 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/swappiness

Centos 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.conf

The following results: 

how to create swap file for Linux 7 . server

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.conf

Change 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 /swapfile

Delete the swap file

sudo rm -f /swapfile

Create a new swap file with the size you want for example create 2GB

sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048k

Create a swap partition: 

sudo mkswap /swapfile

Enable swaps: 

swapon /swapfile

Secure swap with the following 2 commands: 

sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

Check if the swap is correct: 

swapon -s

The 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. ​

Bài viết liên quan