A complete hands-on guide — swap files, swap partitions and LVM swap, with tuning tips (AlmaLinux 9 / RHEL 9)
─────────────────────────────────────────
─────────────────────────────────────────What is swap and why do you need it?
Swap is disk space the kernel uses as overflow when physical RAM fills up. It keeps the system alive under memory pressure instead of killing processes, and it's also where memory pages go during hibernation. It's slower than RAM, so it's a safety net — not a replacement for real memory.
Check Current Swap First
See what swap is active
Code: Select all
swapon --show # lists active swap devices/files
free -h # RAM and swap usage overview
cat /proc/swaps # raw kernel view─────────────────────────────────────────
1 Create Swap
There are three common ways to add swap. Pick one based on your setup.
Method A — Swap file (most flexible)
Code: Select all
# Create a 2 GB file.
# On XFS (AlmaLinux 9 default) use dd, NOT fallocate — see warning below:
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
# Lock down permissions (required, or swapon refuses it)
chmod 600 /swapfile
# Format as swap and enable
mkswap /swapfile
swapon /swapfileMethod B — Swap partition
Code: Select all
# Mark the partition type as Linux swap, then:
mkswap /dev/sdb2
swapon /dev/sdb2Code: Select all
lvcreate -L 2G -n lv_swap vg_data
mkswap /dev/vg_data/lv_swap
swapon /dev/vg_data/lv_swapCode: Select all
swapon --show
free -h2 Extend Swap
You can't resize an active swap file in place — you disable it, grow/recreate it, then re-enable. The cleanest approach is to add more, or rebuild bigger.
Option A — Just add a second swap file
Code: Select all
dd if=/dev/zero of=/swapfile2 bs=1M count=2048 status=progress
chmod 600 /swapfile2
mkswap /swapfile2
swapon /swapfile2Option B — Rebuild a swap file bigger
Code: Select all
swapoff /swapfile # disable first
dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfileCode: Select all
swapoff /dev/vg_data/lv_swap # must be off to resize
lvextend -L +2G /dev/vg_data/lv_swap # grow the LV
mkswap /dev/vg_data/lv_swap # re-format (swap has no resize tool)
swapon /dev/vg_data/lv_swap # re-enable─────────────────────────────────────────
3 Persistent Swap Configuration
swapon only lasts until reboot. To make swap survive restarts, add it to /etc/fstab.
Add the right line to /etc/fstab
Code: Select all
# Swap file:
/swapfile none swap defaults 0 0
# Swap partition or LVM swap (by device path):
/dev/vg_data/lv_swap none swap defaults 0 0Code: Select all
blkid /dev/vg_data/lv_swap # get the UUID
# then in /etc/fstab:
UUID=xxxx-xxxx none swap defaults 0 0Code: Select all
swapoff -a
swapon -a # reads /etc/fstab; errors show here
swapon --show─────────────────────────────────────────
Tuning — Swappiness
swappiness (0–100) controls how aggressively the kernel uses swap. Lower = prefer RAM, swap only when needed. Servers often use 10.
Check and set temporarily
Code: Select all
cat /proc/sys/vm/swappiness # current value (default often 30 or 60)
sysctl vm.swappiness=10 # apply now (resets on reboot)Code: Select all
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.conf
sysctl --system # reload─────────────────────────────────────────
Remove Swap (when needed)
Code: Select all
swapoff /swapfile # disable
# remove its line from /etc/fstab
rm -f /swapfile # delete the file
# for LVM: swapoff then lvremove /dev/vg_data/lv_swapQuick Reference Cheat Sheet
- Show swap — swapon --show ; free -h
- Make swap file (XFS) — dd if=/dev/zero of=/swapfile bs=1M count=2048
- Permissions — chmod 600 /swapfile
- Format — mkswap /swapfile
- Enable — swapon /swapfile
- Extend (LVM) — swapoff -> lvextend -> mkswap -> swapon
- Persistent — add line to /etc/fstab, test with swapon -a
- Swappiness — sysctl vm.swappiness=10
- Disable — swapoff /swapfile
How much swap do you run on your servers, and what swappiness do you set? Share your setup below.