How to create swap in linux

Linux OS Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to create swap in linux

Post by Saman » Sun Aug 12, 2012 7:05 am

These are the steps to add swap space to your Linux system through the use of swap files. Be sure to login as root user.
  1. Determine the amount of existing swap space

    Code: Select all

    more /proc/swaps
    This will list any swap partitions and swap files currently in use. Your swap space should be at least twice the amount of physical RAM installed for best results. For example, a system with 512MB of physical RAM should have at least 1GB of swap space.
    To configure 2GB of swap, we can make one 2GB swap file or two 1GB swap files. We'll do two 1GB swap files because one 2GB swap file might not work depending on Linux kernel support for large files (file over 2GB).
  2. Make swap files named swapfile1 and swapfile2 in /var/tmp

    Code: Select all

    cd /var/tmp
    dd if=/dev/zero of=swapfile1 bs=1024 count=1048576
    dd if=/dev/zero of=swapfile2 bs=1024 count=1048576
    This creates the files /var/tmp/swapfile1 and /var/tmp/swapfile2, each 1GB in size. Using the "dd" command ensures that the files have no holes.
  3. Turn each swap file into a swap area

    Code: Select all

    /sbin/mkswap -c -v1 /var/tmp/swapfile1
    /sbin/mkswap -c -v1 /var/tmp/swapfile2
  4. Turn on swap files

    Code: Select all

    # swapon /var/tmp/swapfile1
    # swapon /var/tmp/swapfile2
    If you getting the error message, swapon: swapfile: Operation not permitted. then you must be in a VPS without rights to create swap files. Make sure you are logged in as root as well.
  5. Verify that the system is using the swap file

    Code: Select all

    more /proc/swaps
    You should see entries for /var/tmp/swapfile1 and swapfile2.
  6. Set up your system to load the swap space at startup

    In /etc/rc.d/rc.sysinit, look for a line that contains:

    Code: Select all

    swapon -a
    Append this command to enable your swap files at startup:

    Code: Select all

    swapon /var/tmp/swapfile1
    swapon /var/tmp/swapfile2
    The line should now look something like this:

    Code: Select all

    action "Activating swap partitions" swapon -a swapon /var/tmp/swapfile1 swapon /var/tmp/swapfile2
Post Reply

Return to “Linux”