Loading Smiley Hoster
Skip to content

Add a swap file to an Ubuntu VPS and tune swappiness

Smiley Hoster Team · · 2 min read
A disk-based swap reserve sits beside RAM and a swappiness adjustment slider.

Cloudflare's recent DNS-cache engineering post is a reminder that memory demand can be reduced. On a smaller VPS, swap provides another tool for short peaks: this procedure adds a disk-backed reserve and tests its effect instead of treating it as extra RAM.

1. Confirm the scope and available space

Use an Ubuntu VPS with sudo and a local ext4 filesystem. The virtualization platform must permit swap. Btrfs, network filesystems and restricted containers need their own setup instructions.

Example
free -hswapon --showfindmnt -no FSTYPE /df -h /sysctl vm.swappinesssudo test -e /swap-vps && echo "STOP: file exists"

Stop if the destination already exists. Reformatting an existing swap file is not part of this guide. Record the original swappiness setting.

The worked example allocates 2 GiB. Choose a size that leaves room for application data, logs and upgrades; consuming the final free gigabytes introduces a different failure.

Swap lets the kernel move some memory pages to disk. It can cushion system-wide pressure, but disk access is slower. It cannot guarantee recovery from a leak, an application memory ceiling or a container's own resource limit.

2. Allocate the file and enable it at boot

After confirming that /swap-vps is absent, run each command and stop on any error:

Example
sudo install -m 600 /dev/null /swap-vpssudo dd if=/dev/zero of=/swap-vps \ bs=1M count=2048 status=progresssudo mkswap /swap-vpssudo swapon /swap-vpsswapon --show

Do not continue after an allocation error such as “No space left”. Mode 600 restricts the file to root.

Back up the mount configuration before editing:

Example
sudo cp -a /etc/fstab /etc/fstab.before-swapsudoedit /etc/fstab

Add this entry once, leaving existing mounts intact:

Example
/swap-vps none swap sw 0 0
Example
sudo systemctl daemon-reloadsudo findmnt --verifysudo swapon --all

Resolve validation errors before rebooting. The active swap list should now contain the intended file and size.

3. Compare a tuning trial with the baseline

First observe your existing swappiness value with swap enabled. Swappiness expresses a relative reclaim preference, not the percentage of RAM that must fill before swapping begins.

If disk activity is hurting response times, compare a temporary value under a similar workload:

Example
sudo sysctl vm.swappiness=20vmstat 1 10free -hsudo journalctl -k --since "30 minutes ago" \ | grep -Ei 'out of memory|killed process'

Twenty is an example experiment, not a universal setting. In vmstat, inspect si and so after the first line, then compare application latency. Sustained paging alongside poor responses points toward reducing concurrency or increasing RAM.

Keep a value only if the measurements support it:

Example
echo 'vm.swappiness=20' | sudo tee \ /etc/sysctl.d/90-vps-swap.confsudo sysctl --systemsysctl vm.swappiness

Check the effective result because another configuration file may override it. After a planned reboot, repeat swapon --show and confirm the file is still enabled.

Common mistakes: duplicate fstab entries, unsupported swap inside restricted virtualization, and assuming a lower swappiness setting prevents OOM failures. To undo the tuning, restore the recorded value and remove the new persistent setting. Do not disable occupied swap merely to empty it without first checking whether RAM can absorb those pages.

Sources: Cloudflare, Ubuntu.

Slide 1 of 5: Ubuntu VPS guide. Add swap and tune swappiness safely. Build a disk-backed reserve, then test its effect under load.
Slide 2 of 5: Step 1 of 3. Run the pre-checks. Confirm sudo, local ext4 storage, free space and swap support. Stop if /swap-vps exists. Record the current swappiness value.
Slide 3 of 5: Step 2 of 3. Create persistent swap. Allocate the file, restrict it to root, enable it and add one fstab entry. Verify the configuration and resolve every error before rebooting.
Slide 4 of 5: Step 3 of 3. Tune from evidence. Trial a temporary value under a similar load. Keep it only when paging, latency and kernel logs support the change.
Slide 5 of 5: Full command guide. Make short memory peaks more manageable. Follow the commands, validation checks and rollback notes in the complete guide.
On social media

See it in our social posts

The key steps of this article, as a carousel. Follow us to catch the next ones.

Did this article answer your question?
Your feedback shapes what we write next.

Your site online today

Free migration* · 30-day refund

Get started* A site under 30 GB, cPanel, WordPress and VPS plans.