Add swap space to a small VPS
Swap is space on disk that the system can use as overflow when memory runs short. On a small VPS, a swap file keeps a sudden memory spike from crashing your services. It is slower than real memory, so think of it as a safety net rather than a replacement.
Before you begin
- A sudo-enabled user logged in over SSH.
- At least 2 GB of free disk space. Check with
df -h /. - Some familiarity with what your server runs, so you can tell whether memory use is normal.
Step 1: Check memory and swap
The free -h command shows memory and swap in human-readable units. On a fresh server, swap is usually zero:
free -h
total used free shared buff/cache available
Mem: 952Mi 180Mi 510Mi 1.0Mi 262Mi 648Mi
Swap: 0B 0B 0B
Step 2: Check free disk space
Swap lives on your disk, so confirm there is room for the file you plan to create:
df -h /
Look at the Avail column for the root filesystem, which is /. A 2 GB swap file needs at least that much free space, and more is better.
Step 3: Create the swap file
fallocate reserves a file of the size you choose almost instantly. This example creates a 2 GB file named /swapfile:
sudo fallocate -l 2G /swapfile
If fallocate reports an error on your filesystem, use this slower alternative instead. It writes zeros to the file, and count=2048 means 2048 blocks of 1 MB each:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Step 4: Restrict access to the file
Swap can contain pieces of memory, including passwords and session data. Only root should be able to read it. The chmod 600 command allows only the owner to read and write:
sudo chmod 600 /swapfile
Step 5: Format and enable the swap file
mkswap prepares the file for use as swap. swapon activates it immediately:
sudo mkswap /swapfile
sudo swapon /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=3f9c2b1e-0a4d-4c7e-9d21-8b6e5f0a7c12
Confirm it is active:
swapon --show
free -h
Step 6: Make swap survive a reboot
Activation with swapon lasts only until the next reboot. Adding a line to /etc/fstab, the file that lists filesystems mounted at boot, makes it permanent. The tee -a command appends the line to the file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Check that the line was added correctly:
grep swapfile /etc/fstab
Step 7: Tune how eagerly the system uses swap
The vm.swappiness setting controls how readily the kernel moves memory to swap. The default is 60. A lower value, such as 10, keeps programs in memory longer and uses swap only when really needed. Set it now and make it permanent:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system
Troubleshooting
- "swapon failed: Invalid argument." The file is not ready. Make sure the permissions from Step 4 are set and that the file was made with
fallocateordd. Delete it withsudo swapoff /swapfileandsudo rm /swapfile, then start again. - Swap is missing after a reboot. The line in
/etc/fstabis probably wrong or missing. Check it withgrep swapfile /etc/fstab, then runsudo mount -ato test the file. - The server is still slow or crashing. Find the biggest memory users with
ps aux --sort=-%mem | head -n 5. A runaway process may be the cause rather than a lack of memory. - The disk is full. Free space before creating the file, or choose a smaller size such as 1G.
Next steps
Swap is in place. Next, tighten SSH so that only key-based logins work, which protects the server from password guessing.
Something not working? Chat or email our support team and we will help you get unstuck.
Contact support