Tutorial

None of the VPS systems that I have ever worked on came with a swapfile by default. One might if your provider happens to use a custom system image, or in rare cases they manually create one for you.

When your system runs low on memory it can use swap. It's slower than real ram/memory, but it's better than running out or crashing your server. Also, pages that are not used very often can be swapped out to give more memory to another application or process that needs it immediately.

The drawback besides being a little slower is that it will consume physical space. If you create a 1 GB swapfile, then you would lose 1 GB of available hard drive space.

Usually the recommended swapfile size is 2x ram/memory. For example, if you have a system with 1 GB, then you would create one that is 2 GBs. However, you can use any size you want, and if your available hard drive space is limited 1x ram/memory would be sufficient.


The following explains how to create a simple swapfile.

1. Login to SSH as root.



2. Check to see if you have a swapfile first with this:
swapon -s

Check for Swapfile



You can also use the top command to check the swap total/used/free section.

Check Swapfile with Top command



3. If one doesn't exist yet decide what size you want to create.

If you want a 512 MB swapfile enter:
dd if=/dev/zero of=/swapfile bs=1024 count=512k

If you want a 1 GB swapfile enter:
dd if=/dev/zero of=/swapfile bs=1024 count=1024k

If you want a 2 GB swapfile enter:
dd if=/dev/zero of=/swapfile bs=1024 count=2048k

*Wait for it to finish. It may take several seconds depending on the size you choose to create.



4. Make, enable, set ownership, permissions:

mkswap /swapfile

swapon /swapfile

chown root:root /swapfile

chmod 0600 /swapfile


It should exist now. You can recheck again with:
swapon -s

And, with the top command as well.



5. Enable it at boot/restart.

Set your system to remember and use your /swapfile at boot/restart:

Edit and add to /etc/fstab using the vi editor with:
vi /etc/fstab

Press a (to enter insert/edit mode).


For CentOS 6x+ add/paste the following at the bottom of the file:
/swapfile               swap                    swap    defaults        0 0

Save the changes with:
Press Esc (to exit insert/edit mode).
Press :wq
Press Enter


For Ubuntu add/paste the following at the bottom of the file:
/swapfile               none                    swap    sw              0 0

Save the changes with:
Press Esc (to exit insert/edit mode).
Press :wq
Press Enter

And you are done.



Change swappiness value.

By default CentOS and Ubuntu will usually have a swappiness value of 60. A larger number will use swap more often, and a smaller number less often.

The standard 60 value is sufficient in most cases. However, if you would like to change this value follow the instructions below.

To check your current swappiness value enter:
cat /proc/sys/vm/swappiness

If you want to change it for example enter:

For swappiness of 10:
sysctl vm.swappiness=10

For 20:
sysctl vm.swappiness=20

For 30:
sysctl vm.swappiness=30

Note:
Only enter one, not all of these.


Verify that it changed by entering this again:
cat /proc/sys/vm/swappiness

To make your system automatically use this value again if you reboot/restart:

Add the following to bottom of /etc/sysctl.conf

If you set a swappiness value of 10 above then:
vm.swappiness=10

For 20:
vm.swappiness=20

For 30:
vm.swappiness=30

To do so:
vi /etc/sysctl.conf

Press a (to enter insert/edit mode.)

Type or paste the line of code at the bottom of the file.

Press Esc (to exit insert/edit mode.)
Press :wq
Press Enter
(To save the changes.)

And done.



Example Creating a 1 GB swapfile with a swappiness value of 20 on CentOS 6.5

I know one doesn't exist on this system yet as swapon -s returns nothing.

Check for Swapfile



And, nothing when checking with the top command.

Check Swapfile with Top command



So from the SSH command line I enter:
dd if=/dev/zero of=/swapfile bs=1024 count=1024k

Then, I enter the following one at a time:
mkswap /swapfile

swapon /swapfile

chown root:root /swapfile

chmod 0600 /swapfile

Make Swapfile



I confirm that it exists now with:
swapon -s

Swapon Check



I enter this in SSH:
vi /etc/fstab

I press a (to enter insert/edit mode).

And, I add/paste the following at the bottom of the file:
/swapfile               swap                    swap    defaults        0 0

I save the changes:
Press Esc (to exit insert/edit mode).
Press :wq
Press Enter

Add swapfile to fstab



I know the swappiness will be 60 by default with this system, but I check with:
cat /proc/sys/vm/swappiness

Now I change it from 60 to 20 by entering:
sysctl vm.swappiness=20

Then, I confirm it has changed with:
cat /proc/sys/vm/swappiness

Change Swappiness



Finally, I make sure the system uses the 20 setting in the event of a reboot by entering:
vi /etc/sysctl.conf

I press a (to enter insert/edit mode.)

I type or paste the following line of code at the bottom of the file:
vm.swappiness=20

I save the changes:
Press Esc (to exit insert/edit mode).
Press :wq
Press Enter

Add Swappiness to sysctl



I can then verify again with:
swapon -s

Verify Swap



And, when I use the top command I can see it as well.

Verify Swap with Top command



3 to 5 minutes later I have a 1 GB swapfile with a swappiness setting of 20 on CentOS 6.5.

 
Written By
Tutorial by: Jeremy LeSarge (AKA Ray)

I am the owner and administrator of DialMe.com. I write Tutorials for Boonex Dolphin as well as tips and resources surrounding website programming and development. I enjoy working with WordPress, SEO, and Web Hosting / Servers. I also maintain a WordPress Blog here on this site where you will find a variety of technology and webmaster resources.

Actions
Sponsored Links
Recommend