Creating a swap file on vps

Even if you have sufficient memory in your server, it’s still a good idea to have a swap file.  In order to reduce disk IO.

Step #1: Login as the Root User

Firstly use the following command to check if you have any swap space already

free -m

The following output shows zero swap space

     total  used        free      shared  buff/cache   available
Mem: 7822   1390         506         492        5924        5569
Swap: 0      0          0

Now we need to create the swap file .Use below command to create 4 GB swap file ,adjust the value according to your need

Step #2: Create Swap File

sudo dd if=/dev/zero of=/swap count=4096 bs=1MiB

Step #3: Secure swap file by setting permission

sudo chmod 600 /swap

Step #4: Set up a Linux swap area by formating the swap file:

sudo mkswap /swap

Step #5: Enabling the swap file

sudo swapon /swap

Step #6: Update /etc/fstab file

To make the swap be automatically mounted after reboot, the following command will add it to your fstab

sudo echo "/swap swap swap sw 0 0" >> /etc/fstab

To verify swap is activated or not

Simply use the free command:

free -m
give following output
      total   used     free      shared  buff/cache   available
Mem: 7822     1390     506         492        5924        5569
Swap:4095     0        4095

Now you are all set.

Was this article helpful?
YesNo

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *