How to Increase Swap Memory on Linux
Create, enable, and permanently configure swap memory on Linux servers with step-by-step commands.
Before you start
Step-by-step instructions
1. Create directory for swap file
Create a dedicated directory to store the swap file.
mkdir /swap
2. Create 1GB swap file
Create a 1GB swap file using dd command.
dd if=/dev/zero of=/swap/swapfile bs=1024 count=1048576
count value controls size. This example creates about 1GB swap.
3. Move to swap directory
Navigate to the directory where the swap file is created.
cd /swap
4. Create swap area
Convert the file into a usable swap memory area.
mkswap swapfile
5. Activate swap file
Enable the swap file so the system can start using it.
swapon swapfile
6. Verify swap configuration
Check whether the swap memory is properly applied.
swapon -s
If swapfile is listed, the setup is complete.
Common issues
If the server does not have enough free storage, the swap file cannot be created successfully.
This usually means the /etc/fstab entry was not added or was written incorrectly.
Swap can help prevent memory crashes, but it is much slower than real RAM. If memory pressure is constant, adding actual RAM may be the better solution.
About this guide
This guide explains how to increase swap memory on a Linux server by creating a swap file, enabling it immediately, and making it permanent after reboot. It is useful when your server has limited RAM or needs temporary memory protection during spikes.
How to follow this guide
- Check whether swap is already configured on the server.
- Create a swap file with the size you want to add.
- Set safe permissions on the swap file.
- Format the file as swap and enable it.
- Register the swap file in /etc/fstab so it stays active after reboot.
- Verify that the swap memory is working properly.
Why use this method?
Swap memory can help reduce memory pressure when physical RAM is low. It is not a replacement for real RAM, but it can help prevent crashes, protect services during traffic spikes, and make small servers more stable.
Frequently Asked Questions
What is swap memory?
Swap is disk space that Linux can use as extra virtual memory when physical RAM becomes full.
Is swap as fast as RAM?
No. Swap is much slower than physical RAM because it uses storage instead of memory.
Do I need to reboot after creating swap?
No. You can enable the swap file immediately with swapon, then add it to /etc/fstab for future boots.
How can I check if swap is active?
You can run free -m or swapon --show to verify that swap is enabled and visible to the system.