Linux swap space.
When RAM fills up, swap is the disk-backed safety net. Watch RAM and swap bars fill, see the difference between a swap file and a swap partition, and learn the one tunable (swappiness) that decides how eagerly the kernel uses it.
Linux swap space animated tutorial. What swap is, swap files vs swap partitions, mkswap and swapon, persistent activation via /etc/fstab, and the vm.swappiness tunable. RHCSA EX200 ready.Swap is the disk-backed safety net when RAM fills up. Watch RAM and swap bars fill, see the file vs partition variants, and tune swappiness.
- Β·Comfortable with df / mount / fstab basics
- Β·Know what a block device (e.g. /dev/sdb1) is
- Β·Helpful: kernel-parameters lesson (sysctl)
Add swap either as a file or partition, persist it in fstab, and tune vm.swappiness for the workload.
pace: 8 minutes
What swap is for
Swap is disk space the kernel can use as overflow RAM. When physical memory fills, the kernel pages cold (rarely-accessed) memory out to swap, freeing RAM for hot pages. It's not a substitute for more RAM β disk is orders of magnitude slower β but it prevents the dreaded OOM killer from shooting your processes the moment RAM is full.
$free -h$swapon --show$cat /proc/swapsSwap is a safety net, not extra RAM. Heavy swapping always means "buy more RAM or fix the workload" β but having SOME swap is almost always better than running with none, because the kernel uses it to evict truly cold pages and free RAM for what matters. Tune swappiness for the workload; persist via fstab; never forget to chmod 600 a swap file.
$free -h$swapon --show$cat /proc/swaps$mkswap /path$swapon /path$swapoff /path$swapon -a / swapoff -a$fallocate -l 2G /swapfile$chmod 600 /swapfile$blkid /dev/sdb1$UUID=... none swap defaults 0 0$sysctl vm.swappiness$sysctl -w vm.swappiness=10$/etc/sysctl.d/99-swappiness.conf$vmstat 1$OOM killerYour server is swapping heavily and getting slow. Adding RAM isn't possible right now. What single tunable might reduce paging?
You created a swap file at /swapfile and ran swapon. It works now, but disappears after reboot. Why?
Why chmod 600 on a swap file?
Difference between a swap partition and a swap file?
These aren't graded β they're just for active recall, which is what actually makes the lesson stick.
Kernel parameters & tuned
vm.swappiness is one of hundreds of sysctl tunables. Learn the runtime-vs-persistent pattern and how tuned bundles them.
Read this lesson as text6 chapters Β· quiz answers
01What swap is for
Swap is disk space the kernel can use as overflow RAM. When physical memory fills, the kernel pages cold (rarely-accessed) memory out to swap, freeing RAM for hot pages. It's not a substitute for more RAM β disk is orders of magnitude slower β but it prevents the dreaded OOM killer from shooting your processes the moment RAM is full.
$ free -hβ RAM + swap usage$ swapon --showβ active swap areas + size$ cat /proc/swapsβ same, raw
02Swap file β flexible, easy
A swap FILE lives on a normal filesystem β create it, format it with mkswap, activate with swapon. Easy to add, resize, or delete. Use
fallocate(or dd for older kernels) to create a fixed-size file, lock down its perms (chmod 600 β anyone who can read it can read swapped memory), then mkswap + swapon.$ fallocate -l 2G /swapfileβ create 2G file$ chmod 600 /swapfileβ perms (security)$ mkswap /swapfileβ format as swap$ swapon /swapfileβ activate
03Swap partition β traditional, fast
A dedicated partition (type
swap/ 82) was the original approach. Slightly faster than a swap file (no FS overhead), but inflexible β resizing means repartitioning. Workflow: create a partition with parted/fdisk, mkswap on the block device, swapon. Tools and commands are otherwise identical.$ parted /dev/sdb mkpart primary linux-swap 0% 2GiBβ make partition$ mkswap /dev/sdb1β format as swap$ swapon /dev/sdb1β activate$ swapon -p 10 /dev/sdb1β priority β higher used first
04Persist with /etc/fstab
swapon activates swap for THIS boot only. To make it survive reboots, add a line to /etc/fstab. Format is: <device-or-file> none swap <opts> 0 0. Use UUID= (find with
blkidorlsblk -o UUID) for partitions so device renumbering doesn't break it. Test withmount -a(no-op for swap) andswapon -a(activates all fstab swap entries).$ blkid /dev/sdb1β get its UUID$ echo 'UUID=... none swap defaults 0 0' >> /etc/fstabβ persist$ swapon -aβ activate all fstab swap$ /swapfile none swap defaults 0 0β swap-file fstab line
05Swappiness β how eagerly to page out
vm.swappiness (0-100, default 60 on most distros, 30 on Ubuntu desktop) controls how aggressively the kernel prefers swap over evicting page cache. 0 = use swap only when truly necessary; 100 = swap freely. Lower it (e.g. 10) on database servers where swap is death-by-paging. Higher only if you're starved for RAM and care more about cache than processes. Change with sysctl, persist in /etc/sysctl.d/.
$ sysctl vm.swappinessβ current value$ sysctl -w vm.swappiness=10β set now (runtime)$ echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.confβ persist$ swapoff /swapfile && swapon /swapfileβ force flush swap
06Remove or replace swap
Deactivate with
swapoff <path>β the kernel pages everything back to RAM (make sure there's room, or it'll fail). Then remove the file (rm /swapfile) or wipe the partition. Don't forget to remove the fstab line too β leaving a stale entry means the next reboot will hang or fail to activate swap.$ swapoff /swapfileβ stop using it$ rm /swapfileβ delete the file$ swapoff -aβ deactivate ALL swap$ # remove fstab line!β or next boot breaks
Quiz β answers
- Your server is swapping heavily and getting slow. Adding RAM isn't possible right now. What single tunable might reduce paging?
- βLower vm.swappiness (e.g. to 10) so the kernel prefers reclaiming page cache before swapping process memory out.Lower swappiness makes the kernel prefer dropping cached file pages instead of paging anonymous (process) memory out to disk. It doesn't fix the root cause (not enough RAM), but it usually reduces visible swap thrashing β especially helpful on database servers.
- You created a swap file at /swapfile and ran swapon. It works now, but disappears after reboot. Why?
- βYou didn't add an /etc/fstab entry β without one, swap isn't reactivated on boot.
swaponactivates swap for the current boot only. Add a line like/swapfile none swap defaults 0 0to /etc/fstab soswapon -a(run by systemd at boot) reactivates it. - Why chmod 600 on a swap file?
- βSecurity β anyone who can read the file can read what was paged out (passwords, keys, etc.). Only root should access it.Swap contains arbitrary memory contents. World-readable swap is a memory-disclosure bug. Restrict to root with chmod 600 before mkswap.
- Difference between a swap partition and a swap file?
- βA swap partition is faster (no filesystem overhead) but inflexible (resizing means repartitioning); a swap file is slightly slower but trivially created, resized, or removed. Both use the same swapon/swapoff commands.Both work; tools are identical. Files win on flexibility, partitions on raw speed (the difference is tiny on modern kernels). Most cloud images now use swap files for that reason.
keep going β these pair well with what you just learned.
SELinux contexts
Why Apache can't read your file. Watch the policy engine decide β and learn the workflow for fixing denials. RHCSA-ready.
Linux boot process
From power button to login prompt. Every stage in the chain β UEFI, GRUB, kernel, initramfs, systemd β with the rescue moves that save you. RHCSA-ready.
LVM stacking
Layer-by-layer build of an LVM stack: disks β PVs β VG β LVs β filesystem. Then extend live and snapshot. RHCSA-ready.