−25%

on annual Windows plans, until 31 Oct. See plans

EQVPS

How to add a swap file on a Linux VPS

Create a swap file in five commands, make it survive reboots and tune swappiness — plus how much swap to add and when more RAM is the real fix.

Small servers run out of memory at the worst moments: an apt upgrade, a Docker build, a nightly backup. Without swap the kernel's OOM killer picks a process and ends it — often your database. A swap file is a cheap seatbelt that gives those spikes somewhere to go. It takes five commands.

1. Check what you have

swapon --show
free -h

No output from swapon --show means there's no swap yet.

2. Create the swap file

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

chmod 600 matters: swap can contain fragments of anything in memory, including secrets, so only root may read it. If fallocate fails with "operation not supported", use dd instead:

dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

3. Make it permanent

Without this step, swap disappears after a reboot:

echo '/swapfile none swap sw 0 0' >> /etc/fstab

4. Tune swappiness

Tell the kernel to prefer RAM and use swap mainly under pressure:

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

5. Verify

swapon --show
free -h

You should see a 2 GB swap device, and cat /proc/sys/vm/swappiness should print 10.

How big should it be?

Don't size swap to "twice the RAM". On a server, swap exists to absorb short spikes, not to run workloads that don't fit.

When swap isn't the answer

Watch it for a few days:

vmstat 5

The si and so columns show pages swapped in and out per second. Occasional activity is fine. Constant non-zero values mean the server is short of memory and everything is waiting on disk. At that point the fix is more RAM — see how much RAM a VPS needs. On EQVPS you can move to a bigger plan without reinstalling; your data stays.

FAQ

How much swap should I add?

On servers with 1–2 GB of RAM, a 1–2 GB swap file is a good default. On bigger servers, 1–2 GB is still plenty as a safety net — swap sized 'twice the RAM' is advice from the days of 256 MB machines.

Will swap make my server faster?

No. Swap only prevents crashes when memory briefly runs out — a package upgrade, a Docker build, a backup. Memory pages on disk are thousands of times slower than RAM. If the server swaps constantly, it needs more RAM.

What does swappiness do?

It controls how eagerly the kernel moves idle memory to swap. The default of 60 swaps fairly early; 10 tells the kernel to keep things in RAM and use swap mostly under real pressure, which suits most servers.

Why use a swap file instead of a swap partition?

A file can be created, resized and removed on a running server in seconds without touching the partition table. Performance on modern filesystems is the same.

How do I remove swap later?

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

Comments

No comments yet. Be the first.

Leave a comment

Comments are moderated before they appear.