Linux add a swap file howto
Q. I need additional swap space to improve my system performance. How do I add a swap file to Linux system?
A. 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[7] as swap partitions, although Red Hat 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
You need to use dd command to create swapfile. Next you need to use mkswap command to set up a Linux swap area on a device or in a file.
a) Login as the root user
b) Type following command to create 512MB swap file (1024 * 512MB = 524288 block size):
# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
c) Set up a Linux swap area:
# mkswap /swapfile1
d) Activate /swapfile1 swap space immediately:
# swapon /swapfile1
e) To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using text editor such as vi:
# vi /etc/fstab
Append following line:
/swapfile1 swap swap defaults 0 0
So next time Linux comes up after reboot, it enables the new swap file for you automatically.
g) How do I verify swap is activated or not?
Simply use free command:
$ free -m
See also:
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- Solaris add a new swap file for database
- Linux Move swap space from one location to another location
- Linux check memory usage
- How do I find out Linux System Memory Utilization?
- Linux performance tools to troubleshoot problem
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: create linux swap file, dd command, free command, linux swap command, linux swap commands, linux swap file, linux swap file partition, linux swap file size, linux swap file system, linux swap filesystem, linux swap partitions, linux swap space, linux swapfile, mkswap command, redhat swap file, swapfile in linux, swapon command




January 17th, 2007 at 10:00 pm
TYPO: vi /etc/fstatb
It should be fstab
January 17th, 2007 at 10:16 pm
anon,
Thanks for heads up!
June 1st, 2007 at 2:35 am
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
October 20th, 2007 at 8:25 am
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.
May 7th, 2008 at 6:29 pm
Thanks, I have been looking for the last step for months heh.