Linux Add a Swap File – Howto

by Vivek Gite on May 18, 2006 · 19 comments

I need additional swap space to improve my system performance. How do I add a swap file to Linux system using command line options?

In Linux, as in most other Unix-like operating systems, it is common to use a whole partition of a hard disk for swapping. However, with the 2.6 Linux kernel, swap files are just as fast as swap partitions, although I recommends using a swap partition. The administrative flexibility of swap files outweighs that of partitions; since modern high capacity hard drives can remap physical sectors, no partition is guaranteed to be contiguous. You can add swap file as a dedicated partition or use following instructions to create a swap file.

Procedure To Add a Swap File Under Linux

You need to use the dd command to create swap file. The mkswap command is used to set up a Linux swap area on a device or in a file.

Step #1: Login as the Root User

Open a terminal window (select Applications > Accessories > Terminal) or login to remote server using the ssh client. Switch to the root user by typing su - and entering the root password, when prompted

Step #2: Create Storage File

Type the following command to create 512MB swap file (1024 * 512MB = 524288 block size):
# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
Where,

  1. if=/dev/zero : Read from /dev/zero file. /dev/zero is a special file in that provides as many null characters to build storage file called /swapfile1.
  2. of=/swapfile1 : Read from /dev/zero write stoage file to /swapfile1.
  3. bs=1024 : Read and write 1024 BYTES bytes at a time.
  4. count=524288 : Copy only 523288 BLOCKS input blocks.

Step #3: Set Up a Linux Swap Area

Type the following command to set up a Linux swap area in a file:
# mkswap /swapfile1
Setup correct file permission for security reasons, enter:
# chown root:root /swapfile1
# chmod 0600 /swapfile1

A world-readable swap file is a huge local vulnerability. The above command make sure only root user can read/write to the file. Finally, activate /swapfile1 swap space immediately, enter:
# swapon /swapfile1

To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using a text editor such as vi:
# vi /etc/fstab

Append the following line:
/swapfile1 swap swap defaults 0 0
Save and close the file. Next time Linux comes up after reboot, it enables the new swap file for you automatically.

How do I Verify Swap is Activated or Not?

Simply use the free command:
$ free -m

See also:

Page last updated at 2:39 PM, January 5, 2012.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 19 comments… read them below or add one }

1 anon January 17, 2007

TYPO: vi /etc/fstatb
It should be fstab

Reply

2 nixcraft January 17, 2007

anon,

Thanks for heads up!

Reply

3 some_guy June 1, 2007

If you don’t want to reboot the machine to enable the new swapfile, after step f) you can issue these commands:

swapoff -a
swapon -a

to first stop and then start all swaps in /etc/fstab

Reply

4 asdffdsa October 20, 2007

Thanks for the help. But how would I go about deleting this swapfile? Also, how does creating a swapfile this way differ from creating a separate partition for one? I only ask because I would like to have multiple installs of linux on my system, and I would like them to share the same swapfile.

Recently, I’ve been trying to create a swapfile partition and it’s not being recognized, so I’ve been having trouble.

Reply

5 Mosab May 7, 2008

Thanks, I have been looking for the last step for months heh.

Reply

6 Alex April 10, 2009

Thank you for the guide; I’d like to add another detail. In this line one may choose to use other units to make things more simple:

# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288

You can turn it into:

# dd if=/dev/zero of=/swapfile1 bs=1M count=512

This means that the block size is 1 MB, so count=512 means “I need 512 megs”, there is no need to do any other calculations.

Reply

7 acidtoi November 10, 2009

hi,
i’m about to install ubuntu karmic on a new computer with thre sata hard drives.

I usually allocate a little more of current ram in swap partition (5 gigas) so I can safely hibernate, but on a post I read in FreeBSD forums there’s a link to FreeBSD manual where states there should be a swap partition for every disk, not just one swap for the whole system.

So, I know Linux is not *BSD but I wonder if the same is applicable here because installing a swap of 5 gigs in all three sata seems a waste of space to me!

cheers!

Reply

8 Gen2ly December 5, 2009

Set swap priority:

• Swapiness is the priority of input/output for swap. To look the current value:
cat /proc/sys/vm/swappiness

To change the swap priority (lower value means less swapping):
sysctl vm.swappiness=10

To have this value set at boot add it to /etc/sysctl.conf
vm.swappiness=0

Reply

9 Primoz January 12, 2010

Hi!
First I must say thanks for a great how to.
And secondly, I’m referencing it in Arch Linux wiki how to create swap file.
Hope you agree with it.
If not please contact me.
Link: http://wiki.archlinux.org/index.php/HOWTO_Create_swap_file

Reply

10 luckyrams March 3, 2010

Thanks! I’m newbie to Linux, It helped me in time.

Reply

11 Thomas May 3, 2010

Great howto!

After creating the swap file its permissions should be set so that only root can access the file:

chmod 600 /swapfile1

Reply

12 Vlad June 29, 2011

great post!

Reply

13 Andrew July 20, 2011

+1 to Thomas suggestion.

You MUST chmod 600 /swapfile1. Otherwise your box will get owned!

http://www.juniper.net/security/auto/vulnerabilities/vuln2678.html

Reply

14 Adam August 4, 2011

So, after following these steps I received the notification that my root partition is full. And then, after rebooting, I cannot log into gnome. It will start gdm but will not go into gnome from there, it will only bring me back to gdm. startx does not work as well. So how do I do the oposite of this?

Reply

15 John M August 29, 2011

You just have to:
-make sure the swap file is not active
swapon -s
-if it is active
swapoff /swapfile (or whatever you called it)
-comment out the line in your /etc/fstab file (Place a hashmark # in front of it or just delete the line)
-remove the swapfile:
rm /swapfile (or whatever you called it)

Reboot and you should be away unless you want to create a smaller swap file in which case you should just make sure it isn’t in use, delete the file and recreate it using a smaller size.

Reply

16 Ron October 9, 2011

Since kernel 2.6.31, you can use the util fallocate instead of dd on btrfs, ext4, ocfs2, and xfs filesystems. It’s *much* faster than dd on really big swap files.

Reply

17 Simon McNair October 27, 2011

Hi Vivek,
5 years on and the page is still useful. Thanks :-)

Reply

18 ravinder January 4, 2012

dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
pls tell me the meaning and function of this command..why we are using if=/dev/zero and of=/swapfile..why this command started with dd..

Reply

19 John Mills January 5, 2012

dd is a command that does a low level copy of data from a to b. ‘if’ is the input file for the copy. /dev/zero is a device that generates as many null characters as it is asked to create and passes them on. ‘of’ is your output file or what will become your new (or additional) swapfile. the ‘/swapfile’ could be any name that you want it to be as long as you don’t forget what it is for the further manipulations. ‘bs’ is the block size of the file and ‘count’ is the number of bytes in the file. So, what the command does is takes 524288 null bytes from /dev/zero and puts them into /swapfile thus initially populating the file.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 12 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.




Previous post:

Next post: