Skip to content
novasean.

VPS access and diagnosis

How to add a swap file to an Ubuntu VPS safely

Check whether swap will help, create and verify a guest-level Ubuntu swap file, and avoid disk, filesystem and restart mistakes.

By Team Novasean · Published

Decide whether swap is the right change

A swap file lets Linux move some memory pages to disk. It does not add physical RAM. On an Ubuntu VPS, first confirm that memory pressure is the problem and that there is enough free disk space; a busy swap device can make an already slow application much slower.

This procedure is for an authorised administrator of the guest operating system. Novasean’s base VM offer leaves guest OS and application changes with the customer; an accepted managed-service Order may assign particular work to a named operator. Check that scope before making a privileged change. If a process was killed, use the OOM diagnosis first. If the VPS is slow without a confirmed memory shortage, use the CPU and memory checks.

Check existing swap, disk space and filesystem

Run free -h, swapon --show, df -h / and findmnt -no FSTYPE /. Record the outputs and the incident time. Existing swap may already be adequate; adding another file does not explain why a service needs more memory. Leave headroom for the operating system, logs, application writes and updates rather than allocating all free disk space to swap.

The worked commands below assume an ordinary local ext4 or XFS filesystem, a new file named /swapfile-extra, sufficient free space and an agreed 1 GiB example size. Confirm that this exact path does not already exist. If the root filesystem is Btrfs, encrypted or uses another special storage layout, stop and follow that filesystem’s supported swap-file procedure with the responsible operator. Btrfs has copy-on-write and placement restrictions; a generic file may be rejected by swapon. Check whether disk-backed swap may contain sensitive application data and how the guest disk, snapshots and backups are protected.

Create and enable a 1 GiB example file

After confirming the path is unused and that 1 GiB is an appropriate illustrative size for your workload, create a private, fully allocated regular file. Run these commands as an authorised guest administrator, one at a time, and stop on any error:

sudo install -m 600 /dev/null /swapfile-extra
sudo dd if=/dev/zero of=/swapfile-extra bs=1M count=1024 status=progress
sudo chmod 600 /swapfile-extra
sudo mkswap /swapfile-extra
sudo swapon /swapfile-extra

dd writes actual blocks; truncate can create holes and be rejected for swap. Do not run mkswap against a disk device, existing application file or a path you have not verified: it writes a swap signature. If allocation fills the disk or swapon rejects the file, do not keep retrying or change filesystem flags blindly. Preserve the error, confirm the filesystem and free capacity, and consult the operator before cleanup.

Make the change survive a reboot

First confirm that swapon --show lists /swapfile-extra and that free -h shows the expected total swap. Save a copy of /etc/fstab before editing it. Add exactly one line for this file, using the actual path you created:

/swapfile-extra none swap defaults 0 0

Do not add a second entry for an existing swap area or copy a placeholder path into production. Review the final file for syntax and duplicate entries. A reboot is a separate availability change: schedule it under your service’s maintenance and recovery procedure, then check swapon --show and the application again after boot. A successful live swapon alone does not prove the boot-time entry works.

Check the workload, not only the swap counter

Compare the original symptom, guest memory, swap use, disk I/O and application response after the change. Some swap use is normal; steadily increasing swap with high I/O wait or repeated OOM events calls for workload analysis, a memory-limit review or a capacity decision. A full filesystem is a separate failure: use the disk-space guide before increasing swap allocation.

Keep a record of the chosen size, file path, filesystem, fstab edit, pre- and post-change readings and who approved the work. If a managed scope applies, give that evidence to the responsible operator without sharing secrets or raw customer data.

Remove the example safely if it is unsuitable

First establish that RAM and any remaining swap can hold the current workload. Remove only the /swapfile-extra entry from /etc/fstab so it will not reactivate at boot. Then ask the authorised operator to run sudo swapoff /swapfile-extra and verify that swapon --show no longer lists it. swapoff can fail when memory is insufficient; stop and restore the intended configuration rather than forcing a deletion. Delete the file only after it is inactive and the exact path is confirmed. Recheck the original symptom and disk space.

Common questions

Will swap prevent every OOM kill? No. A cgroup or service memory limit can still terminate a process, and sustained memory demand can overwhelm the guest.

Can I use fallocate instead of dd? Preallocated files can be interpreted as having holes on some filesystems; the util-linux manual calls dd with /dev/zero the most portable method.

Does adding 1 GiB of swap increase my VPS plan’s RAM? No. It consumes guest disk space. Assess actual memory demand and the applicable plan before a capacity change.

Why does swapon reject my Btrfs file? Btrfs swap files have special no-copy-on-write and allocation constraints. Use the Btrfs-supported creation procedure for your installed version rather than repeating the ext4/XFS example.

Sources and technical references

Prepared by Team Novasean from the references above. Menus and available features depend on your installed version and service permissions.

Related guides

Was this guide helpful?

If you accepted optional analytics, your Yes/No answer and this guide’s page address are sent to Google Analytics. No written feedback is collected. Read our privacy statement or change your choice in .

Need help with your service?

Open your client area or contact Novasean. Describe the symptom and checks you have performed. Keep passwords, recovery codes and private keys out of your message.