Creating a swap file

From time to time you might have the need to create some extra swap space. Instead of creating a separate partition (maybe there is no more free space on the disk) you can create a swap file. This can also easily be removed after it is not needed anymore.

In this example we will add a swap file of 64 MB which 65536 * 1024k blocks.

dd if=/dev/zero of=/swapfile bs=1024 count=65536

Initialise the swap file with

mkswap /swapfile

Afterwards you can enable it with

swapon /swapfile

To enable it at boot time, edit /etc/fstab and add the following stanza:

/swapfile swap swap defaults 0 0

Otherwise the swap file will not be activated when the system is restarted.