No KYC · seven Linux VPS plans, one price in six cities · Pay in BTC, USDT, USDC, ETH, SOL, LTC, XMR
Language: English
Configure a VPS

Guide · memory · checked 16 November 2026

How do I add swap on a Linux VPS?

Last updated: · 4 min read

Quick answer

How do I add swap on a Linux VPS?

To add swap on a Linux VPS, create a file with fallocate -l 2G /swapfile, set it to mode 600, format it with mkswap, turn it on with swapon and add it to /etc/fstab so it survives a reboot. On a SaltVPS KVM server, swap works like on any Linux machine, from Grain (2 GB) upward.

In brief
  • What swap does: gives the kernel disk space to park idle memory pages when RAM runs short.
  • Why on a VPS: it stops the out-of-memory killer from stopping a process during a short peak.
  • What it is not: a replacement for RAM; disk is far slower than memory, even NVMe.
  • Typical size: 1 to 2 GB on a small server; more only for a reason.
  • KVM: each server has its own kernel, so a swap file is allowed.

How do you add a swap file on a Linux VPS?

Run as root. The same commands work on Debian, Ubuntu, AlmaLinux and Rocky Linux with ext4 or XFS.

  1. Check for existing swap

    Run swapon --show and free -h. No output from swapon means no swap is active.

  2. Create the file

    Run fallocate -l 2G /swapfile. If fallocate is refused, use dd if=/dev/zero of=/swapfile bs=1M count=2048.

  3. Restrict access

    Run chmod 600 /swapfile so only root can read it. swapon warns about wider permissions.

  4. Format and enable

    Run mkswap /swapfile, then swapon /swapfile. Check again with swapon --show.

  5. Keep it after reboot

    Add the line /swapfile none swap sw 0 0 to /etc/fstab. Run findmnt --verify to catch typos.

  6. Tune swappiness

    Run sysctl vm.swappiness=10, and put vm.swappiness=10 in /etc/sysctl.d/99-swap.conf to keep it.

How much swap fits each server size?

These are starting points, not rules: watch real use with free -h and adjust.

Suggested starting swap file size by SaltVPS plan RAM and disk
PlanRAMDiskStarting swap
Grain2 GB40 GB NVMe2 GB
Flake4 GB80 GB NVMe2 GB
Crystal6 GB120 GB NVMe2 GB
Halite8 GB160 GB NVMe1 to 2 GB
Rock to Dome16 GB to 32 GB240 GB NVMe to 640 GB NVMe1 GB, or none

Does swap make a small VPS faster?

No: swap prevents crashes, it does not add speed. When RAM fills, the kernel either moves idle pages to swap or kills a process. A little swap turns a crash into a short slowdown. Constant swapping, though, means the server needs more memory.

SaltVPS checked its plan list on 16 November 2026: RAM is reserved for each server, while vCPU are shared with neighbours on the same host. So adding swap never takes memory from anyone else. If free -h shows swap in heavy use day after day, move up a plan; the pricing page lists RAM per plan, and choosing a VPS explains how to size one.

What does vm.swappiness change?

It sets how eagerly the kernel moves memory to swap, on a scale from 0 to 200. Most distributions default to 60. A value of 10 keeps data in RAM longer and uses swap mainly under pressure, which suits servers with databases or containers.

Check the current value with cat /proc/sys/vm/swappiness. Changes made with sysctl apply at once; the file in /etc/sysctl.d/ reapplies them after a reboot.

When is a swap file on a VPS most useful?

During short peaks: package upgrades, image builds, a backup or a burst of visitors. Building Docker images and running apt upgrade on a 2 GB server are typical cases. The Docker guide shows how to check disk use, since swap files share the same disk. Because the server runs on KVM, no host setting blocks a swap file.

Swap on a VPS: questions

How do I add swap on Ubuntu 24.04?

The steps above work as they are: fallocate, chmod 600, mkswap, swapon and a line in /etc/fstab.

How do I remove a swap file?

Run swapoff /swapfile, delete its line from /etc/fstab, then rm /swapfile.

Can I resize swap later?

Yes. Turn it off with swapoff, recreate the file at the new size, run mkswap and swapon again.

Is zram better than a swap file?

zram compresses memory in RAM instead of writing to disk. It helps on very small servers but uses CPU; a swap file is simpler.

What is the best swap size for a 2 GB VPS?

2 GB is a common start. Increase it only if free -h shows swap filling up during normal peaks.

Verdict
  • Adding swap on a Linux VPS takes five commands and one fstab line.
  • Use it as a safety net; heavy daily swapping means the plan needs more RAM.
Sources (3)
  1. SwapFaq — Ubuntu community help wiki (2026-11-16)
  2. mkswap(8) manual page — man7.org (2026-11-16)
  3. Documentation for /proc/sys/vm (swappiness) — Linux kernel (2026-11-16)

More RAM instead of more swap

Flake doubles Grain's memory to 4 GB, for $4.50 a month on 12 months.

From $2.25/mo on 12 months · no KYC

Configure