Enhance server stability: Learn how to create, enable, and fine-tune swap space on your Ubuntu system.
Estimated Time: Approximately 15 - 25 minutes
Swap space on a Linux server is a designated area on your hard drive that the operating system can use when the amount of physical RAM (Random Access Memory) is full. It acts as a temporary overflow for your system's memory, allowing your server to continue functioning when memory demands exceed available RAM.
Why is Swap Important?
This guide will walk you through creating a swap file (the most common method for adding swap space), enabling it, and optimizing its usage for your Ubuntu server.
15 - 25 minutes
Beginner
Basic familiarity with the Linux command line is helpful. No prior swap configuration experience is required.
sudo
privileges./
) to accommodate the desired swap file size (e.g., 2GB or more).While beneficial, swap space comes with important considerations:
Swap should be a fallback, not a primary memory source.
Before adding swap, it's good to check if your system already has any active swap space (either a swap partition or a swap file).
sudo swapon --show
free -h
Ensure you have enough free disk space on your root filesystem to create the swap file. The swap file will typically be created in the `/` directory.
df -h
We'll use `fallocate` to create a file of a specific size. A common recommendation is to have swap equal to 1x your RAM (if RAM < 2GB) or 0.5x your RAM (if RAM > 2GB). For most general-purpose servers, a 2GB swap file is a good starting point.
sudo fallocate -l 2G /swapfile
For security, only the root user should be able to read and write to the swap file. Set permissions to `600` (read/write for owner, no access for others).
sudo chmod 600 /swapfile
Inform the system that this new file should be used as swap space by initializing it.
sudo mkswap /swapfile
Activate the newly created swap file.
sudo swapon /swapfile
Verify immediately:
sudo swapon --show
free -h
The swap file you just enabled will be lost after a reboot unless you add it to the `/etc/fstab` file.
1. Backup `/etc/fstab`:
sudo cp /etc/fstab /etc/fstab.bak
2. Open `/etc/fstab` for editing:
sudo nano /etc/fstab
3. Add the following line to the end of the file:
/swapfile none swap sw 0 0
Save (`Ctrl+O`, `Enter`) and exit `nano` (`Ctrl+X`).
4. Test `fstab` entry without rebooting:
sudo mount -a
These kernel parameters control how aggressively your system uses swap space and manages memory caches. Adjusting them can optimize performance and reduce SSD wear.
1. Understanding Swappiness (`vm.swappiness`):
2. Understanding Cache Pressure (`vm.vfs_cache_pressure`):
3. Check current values:
cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure
4. Set temporary values (for current session):
sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50
5. Make persistent (`/etc/sysctl.conf`):
To ensure these settings apply after a reboot, you need to add them to `/etc/sysctl.conf`.
sudo nano /etc/sysctl.conf
Add the following lines to the end of the file:
vm.swappiness=10
vm.vfs_cache_pressure=50
Save (`Ctrl+O`, `Enter`) and exit `nano` (`Ctrl+X`).
6. Apply changes from `/etc/sysctl.conf`:
sudo sysctl -p
If you ever need to remove the swap file (e.g., if you've added more RAM and no longer need it, or want to resize it), follow these steps:
1. Deactivate the swap file:
sudo swapoff /swapfile
2. Remove the entry from `/etc/fstab`:
sudo nano /etc/fstab
Delete the line you added earlier: `/swapfile none swap sw 0 0`.
Save and exit.
3. Delete the swap file:
sudo rm /swapfile
4. Verify swap is removed:
sudo swapon --show
free -h
Confirm your swap space is correctly configured and optimized:
You have successfully created, enabled, and optimized a swap file on your Ubuntu server. This will significantly improve your system's stability by providing an emergency memory buffer for your applications.
Keep these considerations in mind as you monitor and manage your server:
Need Expert Server Optimization or Memory Management? Contact Us!