Swap is a space Linux uses as an extension of RAM: when RAM fills up, the least used pages are moved to it. This guide shows how to add swap through a file, a partition or compressed zram, make it persistent at boot, and tune the swappiness. For beginner to intermediate administrators, on Debian, Ubuntu or RHEL. All commands were tested on an AlmaLinux 10 VM.
What you will learn
- Create a secure swap file with
mkswapandswapon - Choose between disk swap, a partition and zram (compressed in RAM)
- Make swap persistent in
/etc/fstabby UUID - Tune
vm.swappinessand size the swap
Prerequisites
- A Linux server with a
sudoaccount - Free space on a file system (for a swap file)
- Knowing how to identify a disk with
lsblk
What swap is for
Swap (exchange memory) acts as a safety valve: when RAM is saturated, the kernel moves inactive memory pages to it to free up room. Without swap, a memory spike triggers the OOM killer (Out Of Memory), which abruptly kills a process.
Swap is not a patch for a lack of RAM: a system that swaps permanently is slow, because even an NVMe SSD remains orders of magnitude slower than RAM. It plays two roles: absorbing occasional spikes and enabling hibernation (which writes all of RAM into swap).
Check the current state with free -h and swapon --show:
free -hswapon --show # empty if no swap is activeCreate a swap file
The swap file is the method recommended today: no repartitioning, easy to grow or remove. It works on every distribution.
-
Create the file (here 512 MB) and secure it immediately.
Terminal window sudo dd if=/dev/zero of=/swapfile bs=1M count=512 status=nonesudo chmod 0600 /swapfileThe
chmod 0600is mandatory: a swap file readable by everyone would expose the contents of memory. Do not count onmkswapto stop you: on a0644file, it printsinsecure permissions 0644, fix with: chmod 0600then formats anyway, with a zero exit code (measured with util-linux 2.40 and 2.41). -
Format as swap space.
Terminal window sudo mkswap /swapfileSetting up swapspace version 1, size = 512 MiB (536866816 bytes)no label, UUID=c1d4b2fe-5513-404b-9133-18b22e25fdae -
Enable the swap and check.
Terminal window sudo swapon /swapfileswapon --showNAME TYPE SIZE USED PRIO/swapfile file 512M 0B -2
The swapon --show line confirms that the 512 MB swap file is active. To disable it: sudo swapoff /swapfile.
Use a swap partition
A dedicated swap partition remains common, especially for hibernation. The logic is identical: mkswap then swapon. You can assign a priority when several swap areas coexist.
sudo mkswap /dev/sdb1sudo swapon --priority 10 /dev/sdb1swapon --showNAME TYPE SIZE USED PRIO/dev/sdb1 partition 256M 0B 10The PRIO column shows the priority: the kernel fills the swaps with the highest priority first (0 to 32767). At equal priority, it uses them in parallel (RAID 0-style striping), useful to spread over several disks.
Compressed swap: zram and zswap
Rather than writing to a slow disk, Linux can compress pages in memory. Two mechanisms coexist, not to be confused.
| Mechanism | Principle | Needs a disk? |
|---|---|---|
| zram | a virtual disk compressed in RAM (/dev/zram0) serves as swap | No |
| zswap | a compressed cache in RAM placed in front of a real disk swap | Yes |
zram suits machines with little RAM or without a dedicated disk: it swaps in RAM, without any disk I/O. Fedora enables it by default; on Debian, Ubuntu and RHEL you have to install it (zram-generator package, configured through /etc/systemd/zram-generator.conf). Here is the manual creation, tested on AlmaLinux:
sudo modprobe zramZDEV=$(sudo zramctl --find --size 256M --algorithm zstd)sudo mkswap "$ZDEV"sudo swapon --priority 100 "$ZDEV"zramctl "$ZDEV"NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT/dev/zram0 zstd 256M 4K 58B 20K 2 [SWAP]The COMPR column shows the data really stored after compression: zstd offers the best ratio. zswap, for its part, is only useful if you already have a disk swap: it speeds it up by keeping hot pages compressed in RAM (echo 1 | sudo tee /sys/module/zswap/parameters/enabled).
Make swap persistent
Swap enabled with swapon disappears at reboot. For it to come back, add it to /etc/fstab, by UUID rather than by device name (which can change).
sudo blkid -s UUID -o value /dev/sdb1UUID=e28f4374-0322-4875-922c-bfe3f274d461 none swap sw,pri=10 0 0For a swap file, give its path:
/swapfile none swap sw 0 0Test without rebooting with swapon -a, which enables all the swap entries of fstab:
sudo swapon -aswapon --showTune the swappiness
The vm.swappiness parameter controls how aggressive swapping is. Contrary to a widespread belief, its range goes from 0 to 200 (the maximum was raised from 100 to 200 in kernel 5.8). The kernel default is 60, but that is not necessarily what your machine shows.
sysctl -n vm.swappinessOn a freshly installed AlmaLinux 10, the answer is 30, without any file in /etc/sysctl.d/, /usr/lib/sysctl.d/ or /etc/sysctl.conf declaring anything: the value comes from the active tuned profile, as the box below explains. Check, do not assume: someone who "fixes" an imaginary 60 towards a low value may change nothing at all.
On a server with enough RAM, a low value (10) keeps data in memory and only uses swap as a last resort. Conversely, with zram or an NVMe, swapping costs little: a value above 100 becomes relevant. Make the setting durable in /etc/sysctl.d/:
echo "vm.swappiness = 10" | sudo tee /etc/sysctl.d/99-swappiness.confsudo sysctl -p /etc/sysctl.d/99-swappiness.conf# vm.swappiness = 10The durable setting therefore comes down to a single file in /etc/sysctl.d/, stronger than the tuned profile. The reverse path, removing that file to roll back, holds a surprise however.
Size the swap
How much swap should you plan? It depends on the RAM and on the need for hibernation. Here is the Red Hat recommendation (RHEL 8/9/10), the reference for the RHCSA:
| Installed RAM | Recommended swap | With hibernation |
|---|---|---|
| ≤ 2 GB | 2 × RAM | 3 × RAM |
| 2 to 8 GB | = RAM | 2 × RAM |
| 8 to 64 GB | at least 4 GB | 1.5 × RAM |
| > 64 GB | at least 4 GB | not recommended |
The old rule "2 × RAM" now only applies to very small configurations. On a well-equipped modern server, 4 to 8 GB are enough: swap absorbs spikes, it does not replace RAM.
Security and points of attention
- Strict permissions: a swap file is always in
0600(rootonly), because it contains potentially sensitive memory pages. - Encryption: swap can write secrets (keys, passwords) in clear text. On a sensitive system, encrypt it, either on a LUKS volume or with an ephemeral key through
/etc/crypttab(regenerated at each boot). The ephemeral key is incompatible with hibernation; see the Encrypt a disk with LUKS guide. - Containers and Kubernetes:
kubeletlong refused to start with active swap (failSwapOn: true). Swap support (theNodeSwapfeature) became stable in Kubernetes v1.34 (2025), but must still be enabled explicitly and requires cgroup v2; the default behaviour isNoSwap. Before adding swap on a node, check the cluster's policy.
Troubleshooting
Swap failures almost all come down to three causes: permissions too open, a sparse file and an /etc/fstab entry missing or wrong. A fourth one is about tuning: a misunderstood swappiness, often imposed by tuned.
| Symptom | Probable cause | Solution |
|---|---|---|
insecure permissions 0644 warning from mkswap or swapon | Swap file too open, enabled anyway | sudo chmod 0600 /swapfile |
swapfile has holes | File created with fallocate/truncate | Recreate with dd (or mkswapfile on Btrfs) |
| Swap missing after reboot | Entry missing in /etc/fstab | Add the line, test swapon -a |
| The system swaps too much, latency | vm.swappiness too high, or tuned profile | Lower to 10, check tuned-adm active |
| Boot blocked after editing fstab | Wrong swap line | Rescue mode, fix /etc/fstab |
Put it into practice
A swap declared in fstab but never tested at reboot is a bet, not a configuration. In this VM lab, you create a secure swap file with mkswap and swapon, write it into /etc/fstab and adjust the swappiness: the validation inspects the machine's state after a reboot to confirm that the swap space did come back.
Key points
- Swap absorbs memory spikes and enables hibernation; it does not make up for a lack of RAM.
- The swap file (
dd+chmod 0600+mkswap+swapon) is the most flexible method;fallocateis not reliable on ext4. - zram swaps in compressed RAM, without a disk; zswap speeds up an existing disk swap.
- Make swap persistent in
/etc/fstabby UUID; zram, for its part, goes throughzram-generator. vm.swappinessgoes from 0 to 200 (60 by default on the kernel side, 30 on a fresh AlmaLinux 10); 10 on a server, above 100 with zram. Beware of the tuned profile on RHEL.- A swap file is always in
0600; on Kubernetes nodes, swap (NodeSwap, stable in v1.34) must still be enabled explicitly.
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Manage logical storage with LVM
A new full quiz with random questions
Work again only on the questions you missed