Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Manage swap on Linux: file, partition, zram and swappiness

By updated

Improve this page

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 mkswap and swapon
  • Choose between disk swap, a partition and zram (compressed in RAM)
  • Make swap persistent in /etc/fstab by UUID
  • Tune vm.swappiness and size the swap

Prerequisites

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:

Terminal window
free -h
swapon --show # empty if no swap is active

Create a swap file

The swap file is the method recommended today: no repartitioning, easy to grow or remove. It works on every distribution.

  1. Create the file (here 512 MB) and secure it immediately.

    Terminal window
    sudo dd if=/dev/zero of=/swapfile bs=1M count=512 status=none
    sudo chmod 0600 /swapfile

    The chmod 0600 is mandatory: a swap file readable by everyone would expose the contents of memory. Do not count on mkswap to stop you: on a 0644 file, it prints insecure permissions 0644, fix with: chmod 0600 then formats anyway, with a zero exit code (measured with util-linux 2.40 and 2.41).

  2. Format as swap space.

    Terminal window
    sudo mkswap /swapfile
    Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
    no label, UUID=c1d4b2fe-5513-404b-9133-18b22e25fdae
  3. Enable the swap and check.

    Terminal window
    sudo swapon /swapfile
    swapon --show
    NAME 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.

Terminal window
sudo mkswap /dev/sdb1
sudo swapon --priority 10 /dev/sdb1
swapon --show
NAME TYPE SIZE USED PRIO
/dev/sdb1 partition 256M 0B 10

The 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.

MechanismPrincipleNeeds a disk?
zrama virtual disk compressed in RAM (/dev/zram0) serves as swapNo
zswapa compressed cache in RAM placed in front of a real disk swapYes

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:

Terminal window
sudo modprobe zram
ZDEV=$(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).

Terminal window
sudo blkid -s UUID -o value /dev/sdb1
/etc/fstab
UUID=e28f4374-0322-4875-922c-bfe3f274d461 none swap sw,pri=10 0 0

For a swap file, give its path:

/etc/fstab
/swapfile none swap sw 0 0

Test without rebooting with swapon -a, which enables all the swap entries of fstab:

Terminal window
sudo swapon -a
swapon --show

Tune 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.

Terminal window
sysctl -n vm.swappiness

On 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/:

Terminal window
echo "vm.swappiness = 10" | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
# vm.swappiness = 10

The 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 RAMRecommended swapWith hibernation
≤ 2 GB2 × RAM3 × RAM
2 to 8 GB= RAM2 × RAM
8 to 64 GBat least 4 GB1.5 × RAM
> 64 GBat least 4 GBnot 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 (root only), 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: kubelet long refused to start with active swap (failSwapOn: true). Swap support (the NodeSwap feature) became stable in Kubernetes v1.34 (2025), but must still be enabled explicitly and requires cgroup v2; the default behaviour is NoSwap. 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.

SymptomProbable causeSolution
insecure permissions 0644 warning from mkswap or swaponSwap file too open, enabled anywaysudo chmod 0600 /swapfile
swapfile has holesFile created with fallocate/truncateRecreate with dd (or mkswapfile on Btrfs)
Swap missing after rebootEntry missing in /etc/fstabAdd the line, test swapon -a
The system swaps too much, latencyvm.swappiness too high, or tuned profileLower to 10, check tuned-adm active
Boot blocked after editing fstabWrong swap lineRescue 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; fallocate is not reliable on ext4.
  • zram swaps in compressed RAM, without a disk; zswap speeds up an existing disk swap.
  • Make swap persistent in /etc/fstab by UUID; zram, for its part, goes through zram-generator.
  • vm.swappiness goes 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

6 questions
6 min
70% to pass

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

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn