Adding extra swap space to Linux

This is a quick guide to adding emergency swap space to linux. Which is useful when you know a machine is going to run out memory. Or you need a lot of memory for a single task that cannot be completed without the extra memory.


Login as root


Create a new file of the size that you want to add using the following command. In this case the parameter 512k will make 512 mbytes of space.

dd if=/dev/zero of=/extraswap1 bs=1024 count=512k


Fix the permissions on the file so that only the creator can read / write to it.

chmod 0600 /extraswap1


Turn it into a swap file and tell linux to turn on the extra space

mkswap /extraswap1
swapon /extraswap1


If you want the swap to be enabled after the next reboot of the machine you will also need to add the following line to /etc/fstab

/extraswap1 swap swap defaults 0 0


You can verify that it is working by either using free or by looking directly at the proc filesystem using cat /proc/swaps

Did You find this page useful?

Yes No

Last Modified: 02 May 2017

Releated Posts

2017-09-02 - Raspberry PI - Router Guide
2013-03-01 - Linux - What and how to kill a zombie process
2013-02-14 - Linux - Getting sshfs to work
2012-12-12 - Linux - List / Copy group membership for users
2012-12-08 - Linux - ssh key authentication
2012-12-04 - Linux - sudo without a password
2012-10-13 - Rasberry Pi - Alternative method to play video without omx gstreamer element
2012-10-10 - How to run tcpdump as root
2012-07-13 - Linux - Killing all processes for a specific user
2012-04-05 - Using gdb to debug a core file
2012-01-16 - Linux - Color Coding The Bash Prompt
2012-01-14 - Linux - Automatically set the DISPLAY environment variable in SSH connection
2012-01-06 - Adding extra swap space to linux