Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Increase Swap File Size on Ubuntu VPS

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Check current swap usage. Run free -h to see how much swap is active. Also run swapon --show to list the swap file path (often /swapfile) and its current size.
  2. 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.
  3. 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.
  4. Create a new swap file of the desired size. Use dd to 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, use fallocate for faster allocation: sudo fallocate -l 4G /swapfile (note: fallocate may not work on all filesystems, e.g., some cloud block storage).
  5. Set the correct permissions. Only root should read/write the swap file: sudo chmod 600 /swapfile.
  6. Format the file as swap. Run sudo mkswap /swapfile.
  7. Enable the new swap file. Run sudo swapon /swapfile.
  8. Verify the change. Run free -h and swapon --show to confirm the new size is active.
  9. Make it permanent. Check if /swapfile is already in /etc/fstab with grep swap /etc/fstab. If not, add the line: /swapfile none swap sw 0 0. If you changed the path, update the entry accordingly.
  10. Reboot to test (optional). Run sudo reboot and then check free -h after 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 -a before modifying.
  • Using the wrong file size in dd. The count is in blocks, and with bs=1M, it's megabytes. For 2GB, use count=2048.
  • Incorrect permissions. If the swap file is world-readable, swapon will fail with a warning. Always chmod 600.
  • Not updating /etc/fstab. If you don't add the entry, the swap file won't be available after a reboot.
  • Using fallocate on filesystems that don't support it. Some cloud block storage (like certain NFS or overlay filesystems) may create a file with holes, causing corruption. Use dd if 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 -p or in /etc/fstab (e.g., pri=10).
  • Monitor swap usage. Use htop or vmstat to 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

  • swapon fails with "Operation not permitted": You need root privileges. Use sudo.
  • "swapon: /swapfile: insecure permissions": Run sudo chmod 600 /swapfile and 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/fstab for 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 fallocate if 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.