Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to add swap to a KVM VPS  (Read 12189 times)

0 Members and 1 Guest are viewing this topic.

jominj

  • Guest
How to add swap to a KVM VPS
« on: April 20, 2014, 06:57:22 am »
Swap is very needy when the memory is limited. swap allows to move the inactive codes to hard disk. Thus swap helps to overcome the memory deficiency.
The first thing we need to do is check to see if any swap files have already ben enabled. So login to shell as the root user and run:
Code: [Select]
# swapon -s
If it comes back blank, then you do not have a swap file enabled and can proceed.
Now, create the swap file using the dd command :
Code: [Select]
# dd if=/dev/zero of=/swapfile bs=1024 count=1024k“of=/swapfile” designates the file’s name. In this case the name is swapfile.

Now, prepare the swap file by creating a linux swap area:
Code: [Select]
# mkswap /swapfile
Then finally, the last step would be to activate the swap file:
Code: [Select]
# swapon /swapfile
Now when you check your memory, you will now see the swap memory available. You will also see the swap file when checking the summary:
 
Code: [Select]
# swapon -s
To make sure these new settings stick after a machine reboot, we modify the fstab file.
Open up the file:
Code: [Select]
# nano /etc/fstab
Paste in the following line:
Code: [Select]
/swapfile swap swap defaults 0 0
Save the file. And now, set up the correct permissions for the file
Code: [Select]
chown root:root /swapfile
chmod 0600 /swapfile