Swap Management on Linux — Create, Extend & Make Swap Persistent

Installation, package management, users, groups, permissions, filesystems, systemd, storage management, and core administration concepts.
Post Reply
Murali Krishna
Posts: 14
Joined: Wed Jun 10, 2026 8:34 am

Swap Management on Linux — Create, Extend & Make Swap Persistent

Post by Murali Krishna »

Swap Management on Linux — Create, Extend & Make Swap Persistent
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
Tip: A common starting rule: swap = equal to RAM for systems up to 2 GB, then half of RAM above that. For hibernation you need swap at least equal to RAM.

─────────────────────────────────────────

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 /swapfile
WARNING — XFS users: Do not use fallocate for swap files on XFS. fallocate can leave holes in the file, and swapon will fail with "Invalid argument". Always use dd for contiguous allocation. On ext4, fallocate -l 2G /swapfile is fine.

Method B — Swap partition

Code: Select all

# Mark the partition type as Linux swap, then:
mkswap /dev/sdb2
swapon /dev/sdb2
Method C — LVM swap volume (clean on LVM systems)

Code: Select all

lvcreate -L 2G -n lv_swap vg_data
mkswap /dev/vg_data/lv_swap
swapon /dev/vg_data/lv_swap
Verify it's live

Code: Select all

swapon --show
free -h
─────────────────────────────────────────

2 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 /swapfile2
The kernel uses multiple swap areas together, so total swap simply grows.

Option 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 /swapfile
Option C — Extend an LVM swap volume

Code: 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
Tip: Swap has no online resize. Whatever the method, the pattern is always: swapoff -> change size -> mkswap -> swapon.

─────────────────────────────────────────

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 0
Better: reference a partition/LV by UUID (survives device renaming)

Code: Select all

blkid /dev/vg_data/lv_swap            # get the UUID
# then in /etc/fstab:
UUID=xxxx-xxxx       none    swap    defaults    0 0
Test the fstab entry without rebooting

Code: Select all

swapoff -a
swapon -a                             # reads /etc/fstab; errors show here
swapon --show
WARNING: A bad /etc/fstab swap line can stall or break boot. Always test with swapon -a after editing, and double-check the path/UUID before you reboot.

─────────────────────────────────────────

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)
Make swappiness persistent

Code: Select all

echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.conf
sysctl --system                   # reload
Tip: Set swap priority when running multiple swap areas so faster storage is used first: swapon -p 10 /dev/vg_data/lv_swap (higher number = used first), or add pri=10 in the fstab options column.

─────────────────────────────────────────

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_swap
─────────────────────────────────────────

Quick 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.
Post Reply