Print

How to add swap space on Linux?


First, it is a good idea to check how much swap space is already available with the command:
free -m
The -m switch tells free to report the space in megabytes.

To increase the amount of swap space you can either allocate 1) a dedicated swap partition on disk, or 2) a swap file. Both procedures must be executed from the root account.

1) Dedicated swap partition
For this you need a dedicated free partition, for instance on a new disk you are going to install. This partition can be at most 2 GB in size. Suppose this partition is /dev/hdg7 then initialize the swapspace with:
mkswap /dev/hdg7

Edit the file /etc/fstab and add a new entry for this swap partition:
/dev/hdg7 swap swap defaults 0 0
Enable the new swap partition with:
swapon -a


2) Adding a new swap file
This procedure is more complex, but the advantage is that you don't need to repartition or add disks for it. First locate which of your currently mounted partitions has enough free disk space available by using the df command. Suppose you find that the filesystem which is mounted as the /mydata directory has sufficient free disk space available to hold a 2 GB swap file. Use the following commands to create it:
mkdir /mydata/swap
chmod 700 /mydata/swap

on one line:
dd if=/dev/zero of=/mydata/swap/swapfile1 bs=1024 count=2097150
chmod 600 /mydata/swap/swapfile1
mkswap -f /mydata/swap/swapfile1 2097150


The dd command may take some time to complete. After the mkswap command has completed edit the file /etc/fstab and a new single line entry for the swap file:
/mydata/swap/swapfile1 swap swap defaults 0 0

Lastly enable the new swap file with the swapon command:
swapon -a




Keywords: swap Linux partition
Categories: Faq Installation, Faq Platforms, Huygens Faq, Imported Faqs
Platforms: Linux
Related products: Hu Ess Hu Pro Hu Script