How to Migrate cPanel Accounts Between Servers Using WHM Transfer Tool (Zero-Downtime Guide) Moving live…
How to Add a Swap File on a Linux VPS to Prevent Crash Outages

Most cloud VPS templates ship with zero swap memory configured out of the box. While that keeps initial disk usage low, it leaves your server without a safety net whenever traffic spikes or background tasks temporarily consume 100% of your physical RAM. Learning how to add swap space Linux servers use as an overflow buffer takes less than three minutes and prevents sudden database and web server crashes.
When a Linux server runs completely out of memory without swap configured, the kernel’s Out-Of-Memory (OOM) killer immediately terminates high-memory processes—usually MySQL, MariaDB, or PHP-FPM. To your visitors, that looks like an instant 502 Bad Gateway or “Error establishing a database connection” outage.
This practical tutorial shows you how to add swap space Linux distributions can mount immediately on Ubuntu, Debian, AlmaLinux, and Rocky Linux. You will learn how to create a swap file, lock down permissions, make it permanent across reboots, and tune swappiness for fast NVMe SSD performance.

Why Knowing How to Add Swap Space Linux Matters on Cloud VPS Plans
Swap space is a dedicated file or partition on your server’s NVMe SSD that Linux uses when physical RAM fills up. Less-active memory pages are temporarily moved from RAM to the swap file, freeing fast memory for active web requests, PHP workers, and database queries.
Although NVMe storage is slower than DDR4/DDR5 RAM, having a swap buffer gives your applications breathing room to survive temporary surges—such as search engine crawler bursts, scheduled backups, or software updates—instead of crashing outright. Use the sizing table below before following the commands on how to add swap space Linux administrators trust in production:
| VPS Physical RAM | Recommended Swap Size | Best Use Case |
|---|---|---|
| 512 MB – 1 GB RAM | 1 GB – 2 GB Swap | Lightweight blogs, reverse proxies, small APIs |
| 2 GB RAM | 2 GB Swap | Standard WordPress sites, control panels (CyberPanel, aaPanel) |
| 4 GB RAM | 2 GB – 4 GB Swap | WooCommerce stores, Node.js apps, small Docker stacks |
| 8 GB+ RAM | 4 GB Swap | High-traffic apps, databases, multi-container workloads |
Skipping swap on 1 GB and 2 GB instances is one of the top issues covered in our common VPS mistakes guide. Adding a 2 GB swap file solves the problem permanently.
How to Add Swap Space Linux Step-by-Step
Connect to your VPS via SSH as root or a user with sudo privileges, then follow these five steps to complete how to add swap space Linux cleanly.
Step 1: Check Current RAM and Swap Status
Before you start how to add swap space Linux, check whether your server already has swap enabled and verify that you have enough free disk space on your root partition:
free -h
swapon --show
df -h /
If swapon --show returns blank output and the Swap: row in free -h reads 0B, your VPS currently has no swap configured. Confirm that df -h / shows at least 5–10 GB of free space before creating a 2 GB swap file.
Step 2: Create a 2 GB Swap File
The fastest method for how to add swap space Linux kernels support is allocating a contiguous file using fallocate (adjust 2G to 1G or 4G based on the table above):
sudo fallocate -l 2G /swapfile
Verify that the file was created with the exact size:
ls -lh /swapfile
# Output: -rw-r--r-- 1 root root 2.0G Nov 19 03:30 /swapfile
Note: On certain older filesystems or specific XFS setups, fallocate may fail or create a file with holes that swapon rejects. If that happens, create the swap file using dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Step 3: Lock Down Swap File Permissions (chmod 600)
A critical security step when learning how to add swap space Linux is locking down file permissions so only the root user can read or write to /swapfile. Because swap can hold sensitive application data, encryption keys, or database records moved out of RAM, world-readable permissions are a serious security risk:
sudo chmod 600 /swapfile
ls -lh /swapfile
# Output: -rw------- 1 root root 2.0G Nov 19 03:30 /swapfile
Step 4: Format and Activate the Swap File
Once permissions are set to -rw-------, format the file as Linux swap space and enable it to finish how to add swap space Linux for your current session:
sudo mkswap /swapfile
sudo swapon /swapfile
Confirm that Linux now recognizes the active swap space:
swapon --show
free -h
You should now see /swapfile listed with 2.0G total capacity in both outputs.
Step 5: Make the Swap File Permanent Across Reboots
By default, manually activated swap files disappear when you reboot your VPS. Add an entry to /etc/fstab so Linux mounts /swapfile automatically at boot—otherwise you would have to repeat how to add swap space Linux after every server restart.
First, back up your existing fstab file, then append the swap configuration line:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify that the new line appears cleanly at the bottom of the file:
tail -n 3 /etc/fstab

Tune Swappiness After Learning How to Add Swap Space Linux
Once you complete how to add swap space Linux, Linux’s default memory settings are tuned for desktop computers rather than production servers. Two kernel parameters control how aggressively your VPS moves data between RAM and your NVMe swap file:
vm.swappiness(Default:60): Ranges from0to100. At60, Linux starts moving memory pages to disk even when 40% of your RAM is still free. On a VPS, setting this to10keeps active applications in fast RAM until memory genuinely gets tight.vm.vfs_cache_pressure(Default:100): Controls how quickly the kernel reclaims memory used for directory and inode caches. Lowering this to50keeps filesystem metadata cached longer, speeding up repeated file lookups for WordPress and PHP applications.
As detailed in the Linux Kernel virtual memory sysctl documentation, you can apply both optimizations immediately without rebooting:
sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50
Make both tuning values permanent across reboots by appending them to /etc/sysctl.conf:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
How to Resize or Remove a Swap File Later
If you later upgrade your VPS plan to higher RAM—or need to expand a 1 GB swap file to 4 GB—you can safely deactivate and remove the old file in four commands:
# 1. Deactivate the active swap file:
sudo swapoff -v /swapfile
# 2. Remove the /swapfile line from /etc/fstab:
sudo sed -i '/\/swapfile/d' /etc/fstab
# 3. Delete the old swap file to reclaim disk space:
sudo rm /swapfile
From there, repeat Steps 2 through 5 with your new desired file size.
Frequently Asked Questions About Linux Swap Space
How much swap should I create when following how to add swap space Linux?
For most modern cloud VPS plans with 1 GB to 4 GB of RAM, a 2 GB swap file is the ideal balance between crash protection and disk efficiency. You no longer need to follow the old rule of doubling your RAM on large servers—on an 8 GB or 16 GB VPS, 2 GB to 4 GB of swap is plenty as an emergency safety buffer.
Does adding a swap file wear out my VPS NVMe SSD?
No, not when configured properly with vm.swappiness=10. Enterprise NVMe drives used in modern VPS infrastructure have high endurance ratings, and with swappiness set to 10, your server only writes to the swap file during genuine memory spikes. Keep in mind, however, that if your server is constantly thrashing swap every second, you should upgrade your RAM tier or diagnose memory leaks using our Linux server troubleshooting guide.
Is a swap file better than a swap partition on a VPS?
Yes. As noted in the official Ubuntu Swap FAQ, modern Linux kernels handle swap files with virtually identical performance to dedicated swap partitions, while making it vastly easier to resize, move, or delete swap space without repartitioning live cloud disks.
Why is my VPS using a few megabytes of swap even when free RAM is available?
That is completely normal Linux behavior. The kernel occasionally moves idle background daemon pages (such as inactive SSH or cron memory) into swap so more physical RAM remains available for active disk caching and live web requests. You only need to take action if swap usage climbs rapidly alongside high disk I/O wait.
Keep Your Linux VPS Protected Against Memory Spikes
Mastering how to add swap space Linux takes less than three minutes and gives every budget or mid-tier VPS crucial protection against sudden traffic bursts. By creating a chmod 600 swap file, registering it in /etc/fstab, and lowering vm.swappiness to 10, you keep your databases and web apps responsive under pressure.
If you are setting up a brand-new server stack, pair your new swap file with a lightweight control panel by following our step-by-step guides on how to install CyberPanel or how to install aaPanel.
