August 17, 2012

Beginner

How To Add Swap on CentOS 6

Tagged In: Cent Os, Linux Basics

About Linux Swapping


Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a “linux swap” can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.

However, swapping does have disadvantages. Because hard disks have a much slower memory than RAM, server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.

Check for Swap Space


Before we proceed to set up a swap file, we need to check if any swap files have been enabled by looking at the summary of swap usage.
swapon -s

If nothing is returned, the summary is empty and no swap file exists.

Check the File System


After we know that we do not have a swap file enabled, we can check how much space we have on the server with the df command. The swap file will take 512MB— since we are only using up about 7% of the /dev/hda, we can proceed.
df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda              20642428   1347968  18245884   7% /

Create and Enable the Swap File


Now it’s time to create the swap file itself using the dd command :
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

“of=/swapfile” designates the file’s name. In this case the name is swapfile.

Subsequently we are going to prepare the swap file by creating a linux swap area:
sudo mkswap /swapfile

The results display:
Setting up swapspace version 1, size = 536866 kB

Finish up by activating the swap file:
sudo swapon /swapfile

You will then be able to see the new swap file when you view the swap summary.
 swapon -s
Filename				Type		Size	Used	Priority
/swapfile                               file		524280	0	-1

This file will last on the server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.

Open up the file:
sudo nano /etc/fstab

Paste in the following line:
/swapfile          swap            swap    defaults        0 0


To prevent the file from being world-readable, you should set up the correct permissions on the swap file:
chown root:root /swapfile 
chmod 0600 /swapfile

How To Configure Swappiness


The operating system kernel can adjust how often it relies on swap through a configuration parameter known as swappiness. To find the current swappiness settings, type:
cat /proc/sys/vm/swappiness
60

Swapiness can be a value from 0 to 100. Swappiness near 100 means that the operating system will swap often and usually, too soon. Although swap provides extra resources, RAM is much faster than swap space. Any time something is moved from RAM to swap, it slows down.

A swappiness value of 0 means that the operating will only rely on swap when it absolutely needs to. We can adjust the swappiness with the sysctl command:
sysctl vm.swappiness=10
vm.swappiness=10

If we check the system swappiness again, we can confirm that the setting was applied:
cat /proc/sys/vm/swappiness
10

To make your VPS automatically apply this setting every time it boots up, you can add the setting to the /etc/sysctl.conf file:
sudo nano /etc/sysctl.conf
# Search for the vm.swappiness setting.  Uncomment and change it as necessary.
    vm.swappiness=10


By Etel Sverdlov

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

26 Comments

Write Tutorial
  • Gravatar Oliver Gasser about 1 year

    chown and chmod need sudo, as dd also used sudo to create them. Actually, chown can be skipped as the file is already owned by root:root.

  • Gravatar David Levy about 1 year

    thank you for this

  • Gravatar Fernando Vieira about 1 year

    Thank you! Nice!

  • Gravatar shovtyuk 12 months

    Thank you!

  • Gravatar raj 11 months

    very useful on basic droplets with 512MB ram. i was getting clamav error due and adding swap space fixed it. thank you

  • Gravatar Nathan 10 months

    This may seem like a silly question, but which parameter should I change in the "dd" command if I want to make a 1024 Mb swap file? Thank you!

  • Gravatar Kamal Nasser 10 months

    @Nathan: Edit the count=512k argument with the size of your choice, in your case it would be count=1024k

  • Gravatar pensivedog 7 months

    I have read that, as a rule, swap space should be 2x ram. I will be using a 512 ram or 1g ram droplet to host 1 or 2 drupal sites, plus a vpn with a few users. Should I follow the 2x rule? A couple more quetions: Does this tutorial create a partition, or just a swap file? Is it important to do this upon droplet creation, or is it also ok to do it at any time? Thanks for the excellent tutorial and info!

  • Gravatar Kamal Nasser 7 months

    @pensivedog: 512MB should be enough to prevent a droplet from crashing. We don't recommend creating swap files larger than 512MB. Does this tutorial create a partition, or just a swap file? Swap file. Is it important to do this upon droplet creation, or is it also ok to do it at any time? It doesn't matter -- you can add swap whenever you want.

  • Gravatar Catalin 7 months

    Very nice :> i've made myself a little swamp..

  • Gravatar fonemasta 4 months

    >>> # Search for the vm.swappiness setting. Uncomment and change it as necessary. vm.swappiness=10 >>> For some reason my /etc/sysctl.conf does not have the vm.swapiness setting. Should I add the line at the bottom of this file? Any idea why it's missing? I am running CentOS 6.5 64bit

  • Gravatar fonemasta 4 months

    UPDATE: Adding vm.swappiness=10 to the bottom of seemed to do the trick.

  • Gravatar agus.halim 3 months

    i got warning message [root@us1 pusatweb]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda 20642428 1888684 17705168 10% / tmpfs 251128 0 251128 0% /dev/shm [root@us1 pusatweb]# sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k 524288+0 records in 524288+0 records out 536870912 bytes (537 MB) copied, 6.46376 s, 83.1 MB/s [root@us1 pusatweb]# sudo mkswap /swapfile mkswap: /swapfile: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 524284 KiB no label, UUID=179dbe18-5ac2-412d-840e-dd3e79443be0

  • Gravatar Kamal Nasser 3 months

    @agus.halim: You can safely ignore that warning.

  • Gravatar agus.halim 3 months

    i see thanks. another problems is i cannot start my clamd and i check in the log [root@us1 amavisd]# tail -f /var/log/clamav/clamd.log Mon Jan 27 00:58:53 2014 -> LOCAL: Setting connection queue length to 30 Mon Jan 27 00:58:53 2014 -> ERROR: daemonize() failed: Cannot allocate memory Mon Jan 27 00:58:53 2014 -> Socket file removed. how to increase from 512 Mb swap to 1.5 GB swap ? sorry i'm still basic in here appreciate if can give the step. thanks

  • Gravatar agus.halim 3 months

    Hi after i set sysctl vm.swappiness=10 and do restart the server. the service of clamd can work.

  • Gravatar Ricardo Parraga 2 months

    You need to execute `sudo` when using `sysctl` on this step: ~~~~ sudo sysctl vm.swappiness=10 ~~~~ @fonemasta In CentOS 6.5 x64 I was missing the entry for swappiness so added it manually: ~~~~ # Manual entry for swappiness vm.swappiness=10 ~~~~ Tip: I use `dh -h` for a friendlier human readable format when checking the file system.

  • Gravatar jmiller about 1 month

    Are there any recommended values for swampiness (other than the default)? What might suggest that a change from the default would be a good idea?

  • Gravatar Kamal Nasser about 1 month

    @jmiller: A swappiness value of 10 is recommended.

  • Gravatar daniellekwint about 1 month

    You will then be able to see the new swap file when you view the swap summary. I don't see anything at all

  • Gravatar nguyenan87 24 days

    why i can not run 2 command: "sudo nano /etc/fstab" "sudo nano /etc/sysctl.conf" allways:command not found!plz help me

  • Gravatar nguyenan87 24 days

    my vps using:centos 6.5 64bit

  • Gravatar Kamal Nasser 23 days

    @nguyenan87: Try installing nano:

    sudo yum install nano

  • Gravatar l33.adrian 14 days

    My swap got lost after rebooting the vps. was that normal? Do I have to redo the steps again? and one question, my fstab looks like this... LABEL=DOROOT / ext4 defaults 1 1 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 Do I have to paste the one you mentioned /swapfile swap swap defaults 0 0 or should it be in this format? swap /swapfile swap defaults 0 0

  • Gravatar Igor 10 days

    Mine looks similar

  • Gravatar Kamal Nasser 9 days

    Your fstab file looks normal -- just add the swap file line as mentioned in the article.

Leave a Comment

Create an account or login:
Ajax-loader