AlmaLinux 9: swap sizing and swapfile steps
Test scope
Section titled “Test scope”Executed in an almalinux:9 container (AlmaLinux 9.8): fallocate -l 64M /swapfile, chmod 600, mkswap, swapon, cat /proc/swaps. Log: plans/content-expansion/evidence/logs/alma-swap-sizing.log.
swapon is refused in the container (Operation not permitted), so the “enable it” step is not verified here and must run on a real system.
Check the current swap
Section titled “Check the current swap”cat /proc/swapsswapon --showFilename Type Size Used Priority/dev/zram0 partition 24639296 22432652 32767/dev/vdc partition 1048572 385292 1Output means swap is in use; no output means none is active.
How big
Section titled “How big”| RAM | Suggested swap |
|---|---|
| ≤ 2 GB | 2× RAM |
| 2–8 GB | Equal to RAM |
| 8–64 GB | At least 4 GB |
| > 64 GB | Workload-dependent, at least 4 GB |
These are Red Hat’s recommended ranges (RHEL 9 storage docs, checked 2026-10-19); AlmaLinux follows the same guidance as a RHEL-compatible distribution. A starting point, not a law — databases and container hosts should size from real workload data.
Create the swapfile
Section titled “Create the swapfile”sudo fallocate -l 4G /swapfilesudo chmod 600 /swapfilels -l /swapfilesudo mkswap /swapfile-rw------- 1 root root 4294967296 ... /swapfileSetting up swapspace version 1, size = 4 GiB (4294963200 bytes)no label, UUID=...chmod 600 is mandatory: with 644 mkswap warns and swapon refuses, because a readable swapfile leaks memory contents.
Enable it (needs a real system)
Section titled “Enable it (needs a real system)”sudo swapon /swapfileswapon --showecho '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstabOnly when swapon --show lists /swapfile is it active; the /etc/fstab line keeps it after reboot.
Container limitation: inside
almalinux:9,fallocate,chmodandmkswapall succeed, butswaponreturnsOperation not permitted. That is a privilege limitation, not a configuration error; finish the enable and reboot check on a real system.
Common traps
Section titled “Common traps”- A filesystem without preallocation support: create the file with
ddinstead offallocate; - “Higher swappiness is better” is a myth: database workloads usually lower it (e.g. 10) so the kernel prefers RAM;
- zram and swapfile are not either/or: small hosts can prefer zram with a swapfile as a backstop, but both count in capacity planning.
Practice: 4 GB of swap on a 4 GB host
Section titled “Practice: 4 GB of swap on a 4 GB host”sudo fallocate -l 4G /swapfilesudo chmod 600 /swapfilesudo mkswap /swapfilesudo swapon /swapfileecho '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstabChecks:
swapon --show→ a/swapfileentry of about 4Gcat /proc/swaps | wc -l→ one line more than before- After reboot,
swapon --show→ still there sudo swapoff /swapfile && sudo swapon /swapfile→ disables and re-enables cleanly
常见问题
Is `swapon: Operation not permitted` a permissions problem?
In a container it is usually a capability limit rather than file permissions: fallocate, chmod and mkswap succeed and only swapon is refused. Retry on a real host or an environment with the needed privileges.
Why must the swapfile be mode 600?
Swap may contain sensitive in-memory data. With 644 mkswap warns and swapon refuses, because other users could read the file.
Do I still need a swapfile if zram is enabled?
It depends: zram compresses memory and suits small hosts, while a swapfile provides disk-backed overflow and supports hibernation. They can coexist, but plan capacity and IO for both.
Next steps
Section titled “Next steps”- Host checks: 7 checks after install
- Log rotation: logrotate in practice
- Transaction rollback: dnf history undo