A swap partition or file is useful for extending RAM with disk space or for being able to hibernate the device.

Table of Contents

Reserve disk space

You can create a swap partition or a swap file with at least the same size as your RAM (if you want to hibernate your system). It can be less if you are not going to hibernate your device.

Create a partition

Create a partition (with fdisk for example).

Create a file

dd if=/dev/zero of=<file path> bs=1M count=<size in megabytes>
chmod 600 <file path>

Create swap

mkswap <partition path>
# e.g.: mkswap /dev/sda5
# or
mkswap <swap file path>
# e.g.: mkswap /swap/swapfile

Enable swap

swapon <partition path>
# or
swapon <swap file path>

Editing fstab file

Append this line to the /etc/fstab file:

<swap file path> none swap defaults 0 0
# or
UUID=<swap UUID> none swap defaults 0 0
  • You can find swap partition UUID by running sudo blkid or lsblk -f.


Disable swap

swapoff <swap partition or swap file>
# or
swapoff -a

If you have any suggestion, feel free to contact me via social media or email.