Swap files, partitions and swappiness
Swap is the part of storage that is not a filesystem, and adding some is a common exam task because it touches allocation, permissions, fstab and sysctl at once. This guide reads what the two hosts already have - which turns out to differ - then adds a swap file, prioritises it, and takes it away again.
Storage Guide 35 of 38 Beginner
- PlatformsUbuntu 26.04 LTS + AlmaLinux 10.2
- LVM2.03.31 (Ubuntu) / 2.03.36 (AlmaLinux)
- nftables1.1.6 (Ubuntu) / 1.1.5 (AlmaLinux)
- TimeAbout 14 min
Ubuntu's installer creates a swap file at /swap.img; RHEL's creates a swap partition on LVM. Both are ordinary and the commands are identical. swapon, mkswap and swapoff are util-linux on both.
- Firewallufw 0.36.2 enabled but reporting inactive / firewalld active
- Network confignetplan + systemd-networkd / NetworkManager 1.56.0
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| LFCS-A01 | 192.168.0.70 | Ubuntu 26.04 LTS | Primary host - most guides run only here | 2 Core | 4 GB | 50 GB |
| LFCS-C01 | 192.168.0.72 | AlmaLinux 10.2 | The other distribution - dnf, firewalld and NetworkManager | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for the part of storage that is not a filesystem. This matters because adding swap is a task you may be handed, and a priority number decides which area gets used first.
- asking what the two hosts already have, three ways
- making a swap file by hand, with the permission it needs
- setting which swap area is used first
- changing how eagerly the kernel swaps, two ways
- taking it away again, and confirming what is left
Before you start
-
What the two hosts already have
-
A swap file, made by hand
-
Which swap area gets used first
-
How eagerly the kernel swaps
-
Taking it away