System administrationWritten by GLWS9 min readUbuntu 24.04 LTS / AlmaLinux 9
Commands run on your server unless a section says otherwise. Replace example usernames, addresses and paths with your own values. Keep a working session open when changing remote access.

What swap is for

Swap is disk space the kernel uses when memory is full. It is far slower than RAM, so it is a safety net and not a substitute for memory. On a small VPS it can stop a package upgrade or a database from being killed during a short spike. If a server swaps constantly, it needs a bigger plan or a lighter workload.

Many images have no swap at all. Check before you add any, because a second swap area only wastes disk.

On the VPS: check current memory, swap and disk space
free -h
swapon --show
df -h /

Choose a size

A common starting point for small servers is 1 to 2 GB, or the same as RAM on a 1 GB plan. Do not use more than you are willing to wait for: a process that has to run from swap is slow. Make sure the filesystem has enough free space, because the file is allocated immediately.

Create the swap file

These commands create a 2 GB file owned by root and readable only by root, format it as swap and turn it on. The dd command writes the file in full, which works on every filesystem including XFS on AlmaLinux. Change count to adjust the size in megabytes.

Never overwrite an existing /swapfile. If the image already provides swap, inspect it before adding more. Choose a separate unused path if an additional file is justified.

Both systems
if sudo test -e /swapfile; then
  echo '/swapfile already exists; inspect swapon --show and choose a different unused path.'
else
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
free -h
fi

Make it permanent

Add a line to /etc/fstab so the file is enabled at boot. Back the file up first, and append the line only once. A mistake in fstab can stop a server from booting normally, so verify with the final command before you reboot.

Both systems
sudo cp /etc/fstab /etc/fstab.bak
sudo grep -qE '^/swapfile[[:space:]]' /etc/fstab || echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verify --fstab
Optional: confirm after a reboot
sudo reboot
# after logging in again:
swapon --show

Tune swappiness

The vm.swappiness setting controls how readily the kernel moves idle memory to swap. The default is 60. A lower value, such as 10, keeps more in RAM and uses swap mainly under pressure, which suits most servers. Put it in a file under /etc/sysctl.d/ so it survives reboots.

Both systems
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system | grep swappiness
cat /proc/sys/vm/swappiness
RECORDED LAB OUTPUT · Ubuntu 24.04 LTS
NAME                      TYPE SIZE USED PRIO
/swapfile                 file   8G   0B   -2
/swapfile.glws-validation file   2G   0B   -3
vm.swappiness = 10
NAME      TYPE SIZE USED PRIO
/swapfile file   8G   0B   -2
Recorded separate 2 GB swap-file test alongside the VM’s existing swap. The extra file was removed afterward and the original swap stayed active.

Check whether the server is under memory pressure

Free memory is not the best measure, because Linux uses spare memory for caching. Watch the available column and the si and so columns of vmstat, which show swap-in and swap-out. Occasional use is normal. Constant swapping, or out-of-memory messages in the kernel log, means the workload does not fit.

Both systems
vmstat 5 5
sudo journalctl -k --since "1 day ago" | grep -i "out of memory"

Remove the swap file

Turn the file off, delete its fstab line and remove the file. Use these steps if you upgrade the plan and no longer need it.

Both systems
sudo swapoff /swapfile
sudo sed -i '\|^/swapfile |d' /etc/fstab
sudo rm /swapfile
swapon --show

Official documentation

These upstream references provide more detail on the commands and workflows in this guide.