Enable swap on linux

Enable swap on linux
Photo by Harrison Broadbent / Unsplash

Since I keep forgetting how to enable swap (the ability to use your harddrive as extra ram).

Instructions

Log in using SSH

fallocate -l 2G /swapfile1
chown root:root /swapfile1
chmod 0600 /swapfile1
mkswap /swapfile1
swapon /swapfile1
vi /etc/fstab # See content below (you can also use nano or another editor)
sudo reboot

Content of /etc/fstab

We only need the last line

LABEL=cloudimg-rootfs   /        ext4   defaults        0 1
LABEL=UEFI      /boot/efi       vfat    umask=0077      0 1
/swapfile1 none swap sw 0 0

Checks after reboot

root@host:~# free -h
              total        used        free      shared  buff/cache   available
Mem:          1.9Gi       1.2Gi       208Mi       127Mi       578Mi       498Mi
Swap:         2.0Gi        55Mi       1.9Gi