Enable a Swap File in Linux

You can use a Swap File instead of a Swap Partition with the linux kernel 2.6. Yeah, old news, but I’ve still always used a partition out of habit. Time to change that habit!

1. Log in as root, or use sudo
2. Type following command to create 512MB swap file
sudo dd if=/dev/zero of=/swap.img bs=1024k count=512
could also do it this way (1024 * 512MB = 524288 block size)
sudo dd if=/dev/zero of=/swap.img bs=1024 count=524288
3. Format it as swap.
sudo mkswap /swap.img
4. Activate your swap file immediately
sudo swapon /swap.img
5. Optional, but I recommend activating the swap file on boot. Edit your /etc/fstab file and add the following:
/swap.img none swap sw 0 0
6. To verify that the swap is working type:
free -m

Links this information was taken from:
http://blog.mypapit.net/2007/07/how-to-add-linux-swap-file-if-you-dont-have-swap-partition.html
http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/

Leave a comment