Linux VPS

Create, resize and tune a swap file on a Linux VPS

Add a swap file to a Linux VPS, make it permanent in fstab, choose a sensible size and swappiness, and resize or remove swap safely.

BeginnerLinux VPS2 min read

Swap is disk space the kernel uses when RAM runs out. It is much slower than memory, but a small swap file stops a short memory spike from triggering the OOM killer and crashing MySQL or your app. Here is how to create a swap file on a Linux VPS and keep it sensible.

Before you start

  • Root or sudo access.
  • Free disk space for the swap file (df -h /).
  • Check whether swap already exists: swapon --show and free -h.

Choose a swap size

VPS RAMSuggested swap
1–2 GB1–2 GB
4–8 GB2 GB
16 GB+2–4 GB (safety net only)

Swap is a cushion, not extra RAM. If the server swaps constantly, it needs more memory or less load.

Create the swap file

  1. Allocate the file (2 GB here):
    fallocate -l 2G /swapfile
    If fallocate fails (some filesystems), use dd if=/dev/zero of=/swapfile bs=1M count=2048.
  2. Restrict permissions so other users cannot read memory contents:
    chmod 600 /swapfile
  3. Format and enable it:
    mkswap /swapfile && swapon /swapfile
  4. Confirm with swapon --show.

Make swap permanent

Add a line to /etc/fstab so it comes back after reboot:

/swapfile none swap sw 0 0

Test the fstab file without rebooting: swapoff /swapfile && swapon -a && swapon --show.

Tune swappiness

vm.swappiness (0–100) controls how eagerly the kernel swaps. Servers usually do better with a low value:

sysctl vm.swappiness=10

Make it permanent:

echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf

Resize or remove swap

  1. Turn it off (needs enough free RAM to absorb what is in swap): swapoff /swapfile
  2. To resize, recreate it: fallocate -l 4G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
  3. To remove, delete the fstab line, then rm /swapfile.

Check memory and swap use

  • free -h shows total, used and available memory and swap.
  • vmstat 5: the si/so columns show swap in/out per second. Constant non-zero values mean memory pressure.
  • See which processes use swap: for f in /proc/*/status; do awk '/VmSwap|Name/{printf $2" "}END{print ""}' $f; done | sort -k2 -n | tail

Troubleshooting

  • swapon: Invalid argument: the file has holes (common with fallocate on some filesystems); recreate it with dd.
  • swapon: Operation not permitted: container-based VPS (OpenVZ/LXC) cannot add swap; it is controlled by the host.
  • Server still killed by OOM: swap delays the problem; find the memory-hungry process and reduce its limits.

Swap FAQ

Does swap slow down my server?

Only when it is actively used. Unused swap costs nothing but disk space.

Is swap bad for NVMe drives?

Occasional swapping is fine for modern NVMe drives. Constant heavy swapping means you need more RAM anyway.

Swap file or swap partition?

A swap file is easier to create and resize and performs the same on modern kernels.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.