Linux > Virtualization

How to add swap to a KVM VPS

(1/1)

jominj:
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: ---# swapon -s
--- End code ---

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: ---# dd if=/dev/zero of=/swapfile bs=1024 count=1024k
--- End code ---
“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: ---# mkswap /swapfile
--- End code ---

Then finally, the last step would be to activate the swap file:

--- Code: ---# swapon /swapfile
--- End code ---

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: ---# swapon -s
--- End code ---

To make sure these new settings stick after a machine reboot, we modify the fstab file.
Open up the file:

--- Code: ---# nano /etc/fstab
--- End code ---

Paste in the following line:

--- Code: ---/swapfile swap swap defaults 0 0
--- End code ---

Save the file. And now, set up the correct permissions for the file

--- Code: ---chown root:root /swapfile
chmod 0600 /swapfile
--- End code ---

Navigation

[0] Message Index

Go to full version