Last Updated: February 25, 2016
· rlankenau

Create a new swap file on Linux

Creating a new swap file on Linux requires a non-sparse file. The usual way is to use dd to copy bytes from /dev/zero to initialize the file, but you can also use hdparm --fallocate.

The first option is as follows:

#dd if=/dev/zero of=/var/newswap bs=8192 count=1048576

This usually takes a while, so I use hdparm

#hdparm --fallocate 8388608 /var/newswap

Once your file is created, set it up as swap

#mkswap -L myswap /var/newswap

Turn it on using swapon

#swapon /var/newswap

To view active swap space, take a look at /proc/swaps

#cat /proc/swaps
Filename      Type      Size     Used Priority
/dev/sda5     partition 4192252  0    -1
/var/newswap  file      8388604  0    -2