Adding Swap on Linux Without a Reinstall (the 4-Command Version)

A 2–4GB swap file takes four commands and gives your server a memory cushion. Here's when swap genuinely helps, when it hides a problem, and the exact settings to use.

What you'll see

Root causes

No swap configured at all

Cloud images ship without swap. Linux can then only shed pressure by killing — there's no slow path. swapon --show confirms.

Swap is expected to substitute for RAM

Swap on spinning disks (or heavily throttled cloud disks) can make things worse — it's a cushion, not capacity. Keep swappiness low so swap absorbs spikes, not steady load.

Fix it

  1. Create and enable the swap file
    sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
  2. Persist across reboots
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  3. Tune swappiness: swap for spikes, not steady state
    sudo sysctl vm.swappiness=10 && echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
  4. Verify
    free -h && swapon --show && cat /proc/sys/vm/swappiness

Field note

Size guidance: 2GB is usually plenty as a cushion on 4–8GB servers; match RAM only on desktops/hibernation setups. Containers don't hold swap config — set it on the host; some container runtimes report swap as 0 inside the namespace even when the host has it.

Common questions

Does swap hurt performance?

Active steady-state swapping does (disk is ~1000x slower than RAM). A small, low-swappiness swap used only during spikes costs nothing at rest and prevents OOM kills. Watch si/so columns in vmstat to see if you're swapping constantly.

Can I use a file instead of a partition?

Yes — swap files on modern ext4/xfs work fine and are easier to resize. Prefer fallocate (instant); on filesystems where it's unsupported, use dd if=/dev/zero.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.