Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Check for Existing Swap

swapon --show
free -h

If swapon --show prints nothing and the Swap line in free shows 0, the server has no swap. If swap already exists, decide whether you need more before adding a second file. Check that the disk has room for the file:

df -h /

Step 1: Create the File

The fastest way reserves the space in one step. This creates a 2 GiB file:

sudo fallocate -l 2G /swapfile

The swapon manual warns that, depending on the file system, a file made with fallocate may be treated as a file with holes, and swap files must not have holes. XFS accepts it since Linux 4.18. If fallocate is not supported, or swapon in Step 4 refuses the file, delete it and write every block with dd instead. The manual calls dd the most portable method; it is slower:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Step 2: Restrict Access

Swap can hold any data that was in memory, including passwords. Only root may read it:

sudo chmod 600 /swapfile
ls -lh /swapfile

Step 3: Format the File as Swap

sudo mkswap /swapfile

Step 4: Turn It On

sudo swapon /swapfile
swapon --show

The file now appears in the list and in the Swap line of free -h.

Step 5: Keep It After a Reboot

A mistake in /etc/fstab can stop the server from booting, so make a copy first, then add the line:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verify

findmnt --verify checks the file for errors without rebooting. Before you reboot, make sure you can open the server console in your provider's control panel. The guide to rebooting a Linux server lists the other checks.

Tune Swappiness

vm.swappiness sets how willing the kernel is to move memory to swap rather than drop file cache. It ranges from 0 to 200, and the kernel default is 60. On a server, a lower value keeps active programs in RAM longer:

cat /proc/sys/vm/swappiness
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system

The file in /etc/sysctl.d/ applies the value at every boot. 0 does not turn swap off; it only makes the kernel avoid it until memory is very low.

How Big Should It Be?

RAMStarting point for swap
Up to 2 GBThe same as RAM
2 to 8 GB2 to 4 GB
More than 8 GB4 GB is usually enough

These are rules of thumb, not requirements. Servers do not hibernate, so swap does not need to match RAM. If the swap you add fills up in normal use, the server needs more memory, not a bigger swap file: see how to check memory usage.

Btrfs and Other Caveats

  • Btrfs: a swap file must be NOCOW and uncompressed, the file system must sit on a single device, and the subvolume that holds it cannot be snapshotted while swap is active. Instead of Step 1 to Step 3, create it with sudo btrfs filesystem mkswapfile --size 2G /swapfile (Linux 6.1 and later), then continue with Step 4.
  • Containers: some container-based virtual servers do not allow swap at all; servers on KVM virtualization do.
  • Removing swap: before sudo swapoff /swapfile, check with free -h that the memory in use plus the swap in use fits in RAM. Then remove the fstab line and delete the file.
Was this answer helpful?
Loading...