Skip to content
Log In Sign Up info@muvcloud.com

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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations