Skip to main content
To add swap on Ubuntu (and Debian, AlmaLinux or Rocky Linux), create a swap file and turn it on: sudo fallocate -l 2G /swapfile, sudo chmod 600 /swapfile, sudo mkswap /swapfile, sudo swapon /swapfile. Then add /swapfile none swap defaults 0 0 to /etc/fstab so it is used after every reboot. Check it with swapon --show.

Before you start

  • Connect with SSH. Commands use sudo; root users can leave it out.
  • Swap is disk space Linux uses as overflow when RAM is full. It stops programs being killed when memory runs out, but it is much slower than RAM. If your server swaps all the time, it needs more memory. See check memory usage.
  • Make sure you have room: df -h /. Leave plenty of free space after adding the swap file.

How to check if you already have swap

If swapon --show prints nothing and free -h shows Swap: 0B, there is no swap.

How much swap do I need?

Red Hat’s guidance for servers without hibernation: These are starting points. For a small VPS, 1–2 GB of swap is a common safety net.

How to create a swap file

1

Create the file

If fallocate fails, use dd instead (2048 blocks of 1 MiB = 2 GiB):
2

Lock down permissions

Only root may read swap, because it can contain passwords and other data from memory.
3

Format it as swap

4

Turn it on

5

Keep it after reboot

Back up /etc/fstab, then add the swap line:
A typo in /etc/fstab can stop your server booting normally. Use tee -a (append) exactly as shown, never > which would overwrite the whole file. Check the file with sudo findmnt --verify before you reboot. If the server does not start, use the VNC console or rescue mode on a VPS to restore /etc/fstab.bak.

Swap on Btrfs (Fedora)

On a Btrfs file system the steps above do not work, because a swap file must not be copy-on-write. With btrfs-progs 6.1 or newer, create it in one step:
Check your file system type with df -T /.

How to check it worked

free -h now shows the swap total on the Swap: line.

How to change swappiness

Swappiness (0–100) controls how readily Linux moves memory to swap. The default is 60. Many servers use a lower value, such as 10, so swap is used mainly when RAM is nearly full.

How to make swap bigger or remove it

To resize, turn the swap file off, recreate it at the new size and turn it on again:
mkswap warns wiping old swap signature; that is expected. To remove swap: run sudo swapoff /swapfile, delete the /swapfile line from /etc/fstab, then sudo rm /swapfile.
swapoff moves everything in swap back into RAM. If RAM is nearly full, stop some services first or it can fail.

Frequently asked questions

Does adding swap give my VPS more RAM?

No. Swap lets the server survive short memory peaks without killing processes, but disk is far slower than RAM. If free -h shows swap in use all the time, upgrade your plan or reduce memory use.

What if fallocate fails?

Some file systems do not support it. Use the dd command shown above instead. On Btrfs, use btrfs filesystem mkswapfile.

Should I use a swap file or a swap partition?

On a server you already run, a swap file is easier: you can add, resize or remove it without repartitioning the disk.

Why are my processes still killed when I have swap?

If both RAM and swap fill up, the kernel’s out-of-memory killer still stops processes. See fix out-of-memory killed processes.

VPS and dedicated server guides

Every guide in this section.

Check memory usage

See how much RAM is in use.

Fix out-of-memory kills

When processes are killed for memory.

Check disk space

Make sure there is room for swap.

Monitor VPS resources

Memory graphs in the VPS Panel.

Checked against the official Red Hat Enterprise Linux 9 swap documentation, the Ubuntu SwapFaq and the Btrfs swapfile documentation on 4 November 2026; fallocate, mkswap, swapon, swapoff and swapon --show tested on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026