Setting up Swap File on Linux
How to create and configure a swap file to expand virtual memory on Linux systems
February 11, 2026
Setting up Swap File on Linux
When the system's physical RAM is insufficient, you can improve system stability by setting up a
Swap File that uses a portion of the disk space as memory.Check Current Swap Status
First, check if there is any swap space already configured on the system.
Check current swapsh
Create Swap File
Allocate disk space to create a swap file.
This is an example of creating a 2GB file.
This is an example of creating a 2GB file.
Allocate swap filesh
Set Permissions
For security, modify permissions so that only the root user can access the swap file.
Secure swap filesh
Format and Enable Swap
Format the file as swap space and activate it on the system.
Activate swapsh
Permanent Configuration
Register the swap setting in the
/etc/fstab file so that it persists after reboot.Add to fstabsh
Optimize Swappiness
Swappiness is a value (0-100) that determines how aggressively the system will use swap.
For desktops or servers, a low value around
For desktops or servers, a low value around
10 is usually recommended.Adjust Swappinesssh
If you are using an SSD, excessive swap writes can affect its lifespan, so unless memory is extremely low, it is recommended to use the ZRAM settings discussed earlier.