Linux Swap Partition

In many cases you may need to either increase or decrease the Linux swap size if you add or remove the ram. Swap partions are created while installing the OS usually with a size of 2X of your RAM installed. If you are more concern about increasing the swap partition size you need to use the tools like parted to increase or decrease the partition size. Insted of touching the drive partions another easy and safe way is to use the swap files.

Creating swap file

A swap file can be created and mounted using the dd command. Say for example you need are planning to add 1 GB extra space for your swap memory.

  • # dd if=/dev/zero of=/usr/local/extraswap bs=1M count=1024

Once create you will see the output similar to the following screen-caps.


After that you need to use the mkswap to format the file to swap file sytem.

  • # mkswap /usr/local/extraswap

Output should look similar to the sceen-caps below.


Then use the swapon command to enable the new swap file.

  • # swapon /usr/local/extraswap

Check the swap memory using free -m command and confirm it with swapon -s


To load the swap file on the next boot. Just edit the /etc/fstab and add the following lines.

  • /usr/local/extraswap none   swap   defaults  0   0

Save and exit, make sure you have made the backup of /etc/fstab before adding the lines.