Adding Swap to your KVM VPS

This guide is applicable only for KVM based VPSes and dedicated servers, If you wish to increase swap on your OpenVZ based VPS please contact us via a Support ticket from the client area.

Once you've done this, we suggest limiting the use of SWAP as using a lot of SWAP affects the Disk I/O, we suggest using the maximum amount of RAM as possible.

Please click here to know how to limit the SWAP usage and maximize the use of RAM.

Creating the swap file

The following command creates a 512 MB swap file,

dd if=/dev/zero of=/swapfile1 bs=1024 count=524288

If you're running low on disk space or need a small swap file, create a 128 MB swap file with the following command,

dd if=/dev/zero of=/swapfile1 bs=1024 count=131072

or a 64 MB swap file with the following,

dd if=/dev/zero of=/swapfile1 bs=1024 count=65536

Now setup the swap space

mkswap /swapfile1

Setting up permissions for the swap file

chown root:root /swapfile1
chmod 0600 /swapfile1

Activating the swap file

swapon /swapfile1

Verify that the swap file is activated

free -m

Make the change permanent by editing the /etc/fstab file

We will now use the nano file editor to make changes in the fstab file which will make the swapfile permanent and doesn't turn off after a reboot.

nano /etc/fstab

Add the following line at the end of the file.

/swapfile1  swap    swap    sw  0   0

Save and exit the file.

You can reboot the server to confirm if the Swap is enabled.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS