Updated: 18 October 2026 · Applies to: util-linux 2.41 and systemd 257 on Debian 13 (tested on a real Linux 6.12 kernel), zram-generator 1.2 (Fedora 44 default); the same steps work on Ubuntu 24.04 and 26.04, AlmaLinux and Rocky Linux 9 and 10
To turn swap off in Linux, run sudo swapoff -a. That lasts until the next reboot. To disable swap permanently, also put a # in front of every swap line in /etc/fstab and run sudo systemctl daemon-reload; on Fedora, also switch off the compressed swap in RAM (zram). swapon --show printing nothing means no swap is active.
Think before you do it. Without swap, the kernel's out-of-memory (OOM) killer ends a process as soon as RAM runs out, often the largest one, such as your database. On a VPS with little RAM, swap is the buffer that keeps a short peak from becoming an outage. A common reason to switch it off anyway: according to the Kubernetes documentation, the kubelet does not start on a Linux node with swap enabled unless it is configured for swap. If the server only swaps too eagerly, lower swappiness instead (step 7). More: What Is the Linux OOM Killer and How to Prevent It.
1. See which swap is active
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/ubdb partition 256M 0B -2
/mnt/data/swapfile file 256M 0B -3
$ free -h
total used free shared buff/cache available
Mem: 302Mi 23Mi 18Mi 0B 268Mi 278Mi
Swap: 511Mi 0B 511Mi
(Lab example with a swap partition on the test disk /dev/ubdb and a swap file.) A swap partition or LVM volume shows as partition, a swap file as file. Compressed swap in RAM appears as /dev/zram0 with priority 100. To see which programs use swap, see How to Check Swap Usage in Linux and Find What Uses It.
2. Turn swap off now
sudo swapoff -a # all swap areas sudo swapoff -v /swap.img # only one, prints "swapoff /swap.img"
Everything in swap has to move back into RAM first, so check free -h before: available memory must be clearly larger than swap used. In our lab, programs had 118 MiB in swap and only 8 MiB of RAM was available. swapoff -a ended with Killed; the kernel log showed Out of memory: Killed process 801 (swapoff). The programs survived, but the swap partition stayed on. Free memory first, or add RAM, then try again. Emptying swap without turning it off is covered in How to Free Up Linux Memory: Clear Cache, Buffer, and Swap Safely.
3. Keep swap off after a reboot: /etc/fstab
- Open
/etc/fstaband put a#in front of every line withswapin the third column, for example:#/dev/mapper/almalinux-swap none swap defaults 0 0 #/swap.img none swap sw 0 0 #UUID=598f7f9a-47a1-4d3f-a50e-2eabdcba59ed none swap sw 0 0
The Ubuntu Server installer normally creates the swap file/swap.img; AlmaLinux and Rocky Linux often have an LVM swap volume. - Run
sudo systemctl daemon-reload. systemd turns each fstab swap line into a unit (Loaded: loaded (/etc/fstab; generated)), and the reload removes the unit for a commented line. - Reboot when convenient and check:
$ swapon --show $ systemctl list-units --type swap --all UNIT LOAD ACTIVE SUB DESCRIPTION 0 loaded units listed.
4. Mask the swap unit
Masking stops systemd from ever starting a swap unit, even when the fstab line is still there. Find the unit name first; units are named after the device, so the lab disk /dev/ubdb gives dev-ubdb.swap and /dev/sda2 would give dev-sda2.swap:
$ systemctl list-units --type swap UNIT LOAD ACTIVE SUB DESCRIPTION dev-ubdb.swap loaded active active /dev/ubdb dev-zram0.swap loaded active active Compressed Swap on /dev/zram0 $ systemd-escape -p --suffix=swap /dev/mapper/almalinux-swap dev-mapper-almalinux\x2dswap.swap
Stop the unit before you mask it. In our test, systemctl mask --now created the mask and then failed with Failed deactivating swap dev-ubdb.swap., leaving swap on until the reboot. This order worked:
$ sudo systemctl stop dev-ubdb.swap $ sudo systemctl mask dev-ubdb.swap Created symlink '/etc/systemd/system/dev-ubdb.swap' → '/dev/null'.
After the reboot the swap stayed off and systemctl status showed Loaded: masked. Keep the quotes around names that contain a backslash, for example sudo systemctl mask 'dev-mapper-almalinux\x2dswap.swap'. Undo with systemctl unmask.
5. Fedora and other systems with zram
Fedora installs zram-generator-defaults as part of its core package group. Its file /usr/lib/systemd/zram-generator.conf creates /dev/zram0, a compressed swap device in RAM, with zram-size = min(ram, 8192). Ubuntu 24.04 and 26.04 and Debian 13 do not enable zram unless you install a zram package such as systemd-zram-generator or zram-tools.
- Turn it off now:
sudo systemctl stop systemd-zram-setup@zram0.service(this also deactivatesdev-zram0.swap). - Keep it off: override the vendor file with an empty one,
sudo touch /etc/systemd/zram-generator.conf(as Fedora's file suggests), or with a link to /dev/null,sudo ln -s /dev/null /etc/systemd/zram-generator.conf(the zram-generator manual's way), or remove the package withsudo dnf remove zram-generator-defaults. In our test, the empty file, the /dev/null link andsudo systemctl mask systemd-zram-setup@zram0.serviceeach kept zram off after a reboot. The kernel parametersystemd.zram=0also stops the generator.
6. Remove the swap file or swap volume
Once swap is off and the fstab line is commented out, a swap file can be deleted to free the disk space: sudo rm /swap.img.
An LVM swap volume on AlmaLinux, Rocky Linux or RHEL is often named on the kernel command line. Red Hat's documentation shows resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap; check yours with cat /proc/cmdline. Before you delete the volume, remove only the swap parameters (never the root one), then the volume:
sudo grubby --update-kernel=ALL --remove-args="resume=/dev/mapper/almalinux-swap rd.lvm.lv=almalinux/swap" sudo lvremove /dev/almalinux/swap
Use the exact names from your own command line, and remove them from GRUB_CMDLINE_LINUX in /etc/default/grub too if they appear there.
7. Swap less instead of never
echo "vm.swappiness = 10" | sudo tee /etc/sysctl.d/90-swappiness.conf sudo sysctl --system
The default is 60 (also in our lab). A lower value makes the kernel prefer dropping file cache over swapping out programs, and swap stays available as a safety net for rare peaks. To size swap, see How Much Swap Should You Use in Linux.
Official documentation: swapon and swapoff, systemd.swap and zram-generator.conf.