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 swap
sh

Create Swap File

Allocate disk space to create a swap file.
This is an example of creating a 2GB file.
Allocate swap file
sh

Set Permissions

For security, modify permissions so that only the root user can access the swap file.
Secure swap file
sh

Format and Enable Swap

Format the file as swap space and activate it on the system.
Activate swap
sh

Permanent Configuration

Register the swap setting in the /etc/fstab file so that it persists after reboot.
Add to fstab
sh

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 10 is usually recommended.
Adjust Swappiness
sh
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.
Jooojub
System S/W engineer
Explore Tags
Series
    Recent Post
    © 2026. jooojub. All right reserved.