You Are Here: Home » How-To » Linux

How To Create A Swap File In Linux

By Ricky on February 7th, 2011 

The swap space is an area of the hard disk that is used when the RAM runs out of memory. If your system has enough RAM it is generally not necessary to have swap space. However, if you have installed Linux without creating any swap partition and later want some swap space, you can easily do so. You can either create a swap partition or create a swap file.

The swap file approach is simpler and safer if you want to add swap space post OS installation. Beside, swap file and swap partition have the same performance now. I carried out the instructions I am giving here on Ubuntu 10.10, but it should work on any other distributions as well.

The first thing we need to do is create an empty file of the required size. To do this open the Terminal and execute the command given below. In this example I am creating a 3GB swap file. You will not probably need that much. So, enter your own value.

$ cd /

$ sudo dd if=/dev/zero of=swapfile bs=1M count=3000

To set it as 1GB, change the count value (3000 in the example above) to 1000, 1500 for 1.5GB etc.

Now change the file created to a swap file with the command below.

$ sudo mkswap swapfile

Turn on the swap file with the command,

$ sudo swapon swapfile

To ensure that the swap file is turned on automatically at system startup, open fstab.

$ sudo nano etc/fstab

And add the line given below. Save and close.

/swapfile none swap sw 0 0

That is it. You can check if the system is using the swap file you created with the command

$ cat /proc/meminfo



How To Create A Swap File In Linux was originally published on on February 6, 2011 - 10:36 pm (Indian Standard Time)