Increase Swap File Size on Ubuntu VPS
In this article
This guide walks you through increasing the swap file size on an Ubuntu VPS, which gives your system more virtual memory when physical RAM runs low. You'll resize or replace the existing swap file, verify the change, and make it permanent across reboots. You need root or sudo access to your VPS.
What You'll Need
- Root or sudo access to your Ubuntu VPS
- SSH client (e.g., Terminal on macOS/Linux, PuTTY on Windows)
- Basic comfort with the command line
Step-by-Step
- Check current swap usage. Run
free -hto see how much swap is active. Also runswapon --showto list the swap file path (often/swapfile) and its current size. - Turn off the existing swap file. Run
sudo swapoff -a. This moves data from swap back into RAM. If your RAM is nearly full, you may need to close some processes first. - Remove the old swap file (optional). If you're replacing it, delete it with
sudo rm /swapfile. If you plan to keep the same file and just resize it, you can skip this step. - Create a new swap file of the desired size. Use
ddto allocate the file. For example, to create a 4GB swap file:sudo dd if=/dev/zero of=/swapfile bs=1M count=4096. Adjust the count (in MB) to your target size. Alternatively, usefallocatefor faster allocation:sudo fallocate -l 4G /swapfile(note: fallocate may not work on all filesystems, e.g., some cloud block storage). - Set the correct permissions. Only root should read/write the swap file:
sudo chmod 600 /swapfile. - Format the file as swap. Run
sudo mkswap /swapfile. - Enable the new swap file. Run
sudo swapon /swapfile. - Verify the change. Run
free -handswapon --showto confirm the new size is active. - Make it permanent. Check if
/swapfileis already in/etc/fstabwithgrep swap /etc/fstab. If not, add the line:/swapfile none swap sw 0 0. If you changed the path, update the entry accordingly. - Reboot to test (optional). Run
sudo rebootand then checkfree -hafter boot to confirm the swap is still active.
Common Mistakes
- Forgetting to turn off swap first. You cannot resize a swap file that is in use. Always run
swapoff -abefore modifying. - Using the wrong file size in
dd. Thecountis in blocks, and withbs=1M, it's megabytes. For 2GB, usecount=2048. - Incorrect permissions. If the swap file is world-readable,
swaponwill fail with a warning. Alwayschmod 600. - Not updating
/etc/fstab. If you don't add the entry, the swap file won't be available after a reboot. - Using
fallocateon filesystems that don't support it. Some cloud block storage (like certain NFS or overlay filesystems) may create a file with holes, causing corruption. Useddif you're unsure.
Tips
- Choose a sensible size. A common rule of thumb is 1-2x your RAM for systems with less than 2GB RAM, and 0.5-1x for systems with more. But for VPS, you may want to keep swap modest to avoid excessive disk I/O.
- Consider swap priority. If you have multiple swap devices, set priority via
swapon -por in/etc/fstab(e.g.,pri=10). - Monitor swap usage. Use
htoporvmstatto see if swap is being used heavily. If it is, you might need more RAM instead. - Use a swap file on a fast disk. If your VPS has SSD storage, swap performance is decent. On HDD, swap can be slow.
Troubleshooting
swaponfails with "Operation not permitted": You need root privileges. Usesudo.- "swapon: /swapfile: insecure permissions": Run
sudo chmod 600 /swapfileand try again. - System freezes after swapoff: This happens when RAM is insufficient to hold the swapped data. Free up memory by stopping services or kill processes before running swapoff.
- Swap file not active after reboot: Check
/etc/fstabfor the correct line. Also ensure the file exists at the specified path and has correct permissions. - dd command is slow: That's normal. It's writing every byte. You can use
fallocateif your filesystem supports it, but test it first.
Frequently Asked Questions
Can I increase swap without rebooting?
Yes, the steps above are all done live. You only need to reboot if you want to test the fstab entry.
What is the maximum swap file size?
On 64-bit systems, the theoretical max is huge (16 exabytes), but in practice it's limited by disk space and filesystem limits. For ext4, individual files can be up to 16TB.
Is swap necessary on a VPS?
It's not strictly necessary, but it provides a safety net against out-of-memory crashes. Many VPS providers include a small swap by default. You can disable it entirely if you prefer, but that increases the risk of OOM kills.
What's the difference between swap file and swap partition?
A swap file is a regular file on your filesystem, easy to resize. A swap partition is a dedicated disk partition. Files are more flexible, but partitions can be slightly faster in some cases.
How do I know if my VPS is using swap?
Run free -h and look at the "Swap" row. If the "used" column is high, the system is swapping. Also swapon --show shows the active swap devices.
Can I use a swap file on tmpfs?
It's not recommended because tmpfs is RAM-backed, defeating the purpose. Use a persistent filesystem like ext4.