progressive pride flag

Adding SWAP to a Debian Machine

So for SWAP, we usually have two choices. A swapfile, and a swap partition.

A swapfile is a lot more flexible since it lives on your filesystem. However, it does have an added overhead of the filesystem.

Another reason why you may want to use a swap partition is that in case you have more than one linux distro installed, they can share the same swap partition.

People also encrypts their swap partition

  1. First we create the swapfile with 4G
    fallocate -l 4G /swapfile

  2. Then, we set the correct permission for the swapfile
    chmod 600 /swapfile

  3. We now run this to setup the swapfile with the correct headers.
    mkswap /swapfile

  4. We will now run this to enable the swap partition, please note that this is a transient operation and will not persist if you reboot the system.
    swapon /swapfile

  5. Lastly, we will add the swapfile to our fstab so it will automatically enable the swapfile on boot.
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

There are also two sysctl options that you can tweak, either via the sysctl command (transient), or /etc/sysctl.conf (persistent).

vm.swappiness
vm.vfs_cache_pressure

Further reading: kernel Memory Management docs

Adapted from the DigitalOcean SWAP space guide


If you have any questions, want to change my mind, or literally anything else, please reach out!