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.
Cloud images ship without swap. Linux can then only shed pressure by killing — there's no slow path. swapon --show confirms.
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.
sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo sysctl vm.swappiness=10 && echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
free -h && swapon --show && cat /proc/sys/vm/swappiness
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.
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.
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.
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.