Operations 7 min read

How to Permanently Disable Swap on Modern Linux Systems

This guide explains why the traditional /etc/fstab comment method fails on newer Linux distributions and provides two reliable techniques—masking the swap.target unit with systemd and adding the noauto option to the fstab swap entry—to permanently disable swap across Ubuntu, CentOS, openEuler, and other major systems.

Raymond Ops
Raymond Ops
Raymond Ops
How to Permanently Disable Swap on Modern Linux Systems

Disabling Swap on Linux

<code>sed -ri 's/.*swap.*/#&/' /etc/fstab</code>

Previously you could disable swap by commenting the swap line in

/etc/fstab

and rebooting. From Ubuntu 20.04, CentOS Stream 10, openEuler 24.04, OpenCloudOS 9, Anolis OS 23, openSUSE 15 onward, that method no longer works.

Method 1

Systemd.swap units control swap after Ubuntu 20.04. Example commands:

<code># Show current swap
root@ubuntu2404:~# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/sda5 partition   4G   0B   -2

root@ubuntu2404:~# free -h
               total   used   free   shared  buff/cache   available
Mem:           1.9Gi  388Mi  1.4Gi   1.2Mi   286Mi   1.5Gi
Swap:          4.0Gi   0B   4.0Gi

# /etc/fstab entry
/dev/disk/by-uuid/e47400f5-4648-4aef-81de-dee66f909261 none swap sw 0 0

# List swap units
root@ubuntu2404:~# systemctl list-unit-files | grep swap
device-disk-by\x2duuid-e47400f5\x2d4648\x2d4aef\x2d81de\x2ddee66f909261.swap generated       -
swap.target                                 static

# Check swap.target status
root@ubuntu2404:~# systemctl status swap.target
● swap.target - Swaps
   Loaded: loaded (/usr/lib/systemd/system/swap.target; static)
   Active: active since Mon 2025-02-10 10:22:05 UTC; ...

# Mask swap.target
root@ubuntu2404:~# systemctl mask swap.target
Created symlink /etc/systemd/system/swap.target → /dev/null.

# Reboot and verify
root@ubuntu2404:~# swapon --show
# (no output)
root@ubuntu2404:~# free
... Swap: 0 0 0</code>

This method does not work on CentOS 7 and Ubuntu 18.04.

Method 2

Adding the

noauto

option to the swap entry in

/etc/fstab

prevents both systemd.swap and fstab from activating swap automatically.

<code># Example on Ubuntu/Debian
root@ubuntu2404:~# vi /etc/fstab
/dev/disk/by-uuid/e47400f5-4648-4aef-81de-dee66f909261 none swap sw,noauto 0 0

# Or using sed
sed -ri.bak '/swap/s/(.*)(sw)(.*)/\1\2,noauto\3/g' /etc/fstab

# Example on Rocky/AlmaLinux/CentOS/openEuler/openCloudOS/Anolis/openSUSE
root@openeuler24 ~]# vi /etc/fstab
UUID=2bd0a169-4f2b-4fce-8f4b-0b2ea6273990 none swap defaults,noauto 0 0

# Or using sed
sed -ri.bak '/swap/s/(.*)(defaults)(.*)/\1\2,noauto\3/g' /etc/fstab

# Reboot and verify
root@ubuntu2404:~# swapon --show
# (no output)
root@ubuntu2404:~# free
... Swap: 0 0 0</code>

Method 2 works on all tested systems.

operationsLinuxCentOSswapUbuntusystemdfstabnoauto
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.