| Tested on | Ubuntu 25.04 (Plucky Puffin) |
|---|---|
| Package | util-linux 2.40.2 |
| Applies to | Ubuntu, Debian, Kali Linux, Linux Mint, Pop!_OS, Raspberry Pi OS, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux |
| Privilege | sudo or root |
| Man page | swapon(8) |
| Scope | Check swap usage, enable swap from fstab or a swapfile, set priority, and disable swap safely on Linux. |
| Related guides | Linux swap and swappiness Linux commands cheat sheet |
swapon turns on swap backing store (partition or file); swapoff turns it off. You use them after creating swap with mkswap, at boot via fstab, or when troubleshooting memory pressure. This cheat sheet walks through check → create → enable → inspect → disable on a real Ubuntu host, with trimmed terminal output you can diff locally.
swapoff -a on a busy system can take minutes while pages are copied back to RAM and may trigger OOM if memory is tight. Use on lab hosts or maintenance windows; see Do you need swap on Linux? for when swap matters.
swapon and swapoff — quick cheat sheet
| Command | Purpose |
|---|---|
free -h |
Human-readable RAM and swap usage |
swapon --show |
List active swap (preferred over -s) |
cat /proc/swaps |
Kernel view of the same data (kB columns) |
sudo swapon -a |
Enable all swap lines from /etc/fstab |
sudo swapon /path/to/swapfile |
Enable one swapfile or partition |
sudo swapon -p 10 /path |
Enable with priority (higher used first) |
sudo swapon -v /path |
Verbose enable (signature, page size) |
sudo swapon -ae |
Enable from fstab; skip missing devices (-e needs -a) |
sudo swapoff /path |
Disable one swap device or file |
sudo swapoff -a |
Disable all active swap |
swapon --show=NAME,SIZE,USED,PRIO |
Custom columns |
swapon --bytes |
Sizes in bytes |
swapon --noheadings / --raw |
Script-friendly table output |
Command syntax
swapon [options] [<spec>]
swapoff [options] [<spec>]<spec> can be a path (/swap.img, /dev/sda2), or LABEL=…, UUID=… as shown in swapon --help. Most changes need sudo.
Check whether swap is active
Start with free -h and swapon --show:
free -h
swapon --showtotal used free shared buff/cache available
Mem: 5.2Gi 1.9Gi 2.2Gi 2.7Mi 1.3Gi 3.2Gi
Swap: 2.4Gi 719Mi 1.7Gi
NAME TYPE SIZE USED PRIO
/swap.img file 2.4G 719.1M -1USED in swapon --show matches the swap line in free. The kernel exposes the same entries in /proc/swaps (sizes in kilobytes):
cat /proc/swapsFilename Type Size Used Priority
/swap.img file 2536444 736388 -1For background on what swap is doing in memory pressure, see Linux memory management overview.
Create a swapfile (before swapon)
swapon only works on space already prepared with mkswap. On this host the installer created /swap.img; to add your own file on ext4 (not on tmpfs—/tmp will fail with Invalid argument):
sudo fallocate -l 512M /var/tmp/lab-swap-512m
sudo chmod 600 /var/tmp/lab-swap-512m
sudo mkswap /var/tmp/lab-swap-512mSetting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=712c564c-1abc-4308-8668-584bc1bce2d0Make it persistent by adding a line to /etc/fstab (example):
/var/tmp/lab-swap-512m none swap sw 0 0Ubuntu’s default fstab entry for the installer swapfile looks like:
grep swap /etc/fstab/swap.img none swap sw 0 0Enable swap with swapon
Enable all fstab swap entries
sudo swapon -a
swapon --showAfter swapoff -a, re-enabling restores the swapfile with zero use until pressure returns:
NAME TYPE SIZE USED PRIO
/swap.img file 2.4G 0B -1Enable one swapfile
sudo swapon /var/tmp/lab-swap-512m
swapon --showNAME TYPE SIZE USED PRIO
/swap.img file 2.4G 719.1M -1
/var/tmp/lab-swap-512m file 512M 0B -1Set swap priority
Higher PRIO values are used before lower ones. Disable first, then enable with -p:
sudo swapoff /var/tmp/lab-swap-512m
sudo swapon -p 10 /var/tmp/lab-swap-512m
swapon --showNAME TYPE SIZE USED PRIO
/swap.img file 2.4G 719.1M -1
/var/tmp/lab-swap-512m file 512M 0B 10Verbose enable
-v prints how util-linux detected the swap signature:
sudo swapon -v /var/tmp/lab-swap-512mswapon: /var/tmp/lab-swap-512m: found signature [pagesize=4096, signature=swap]
swapon: /var/tmp/lab-swap-512m: pagesize=4096, swapsize=536870912, devsize=536870912
swapon /var/tmp/lab-swap-512mSkip missing fstab entries (-e with -a)
-e / --ifexists only applies with -a. Without it, a missing swap path in fstab errors out; with -e, that entry is skipped:
# fstab contains /missing-swap (does not exist) and /swap.img
sudo swapon -aT /tmp/fstab-swap-testswapon: cannot open /missing-swap: No such file or directorysudo swapon -aeT /tmp/fstab-swap-test
swapon --showNAME TYPE SIZE USED PRIO
/swap.img file 2.4G 0B -1(-T points at an alternate fstab file; omit it to use /etc/fstab.)
Inspect swap usage (swapon display options)
swapon -s and swapon --summary still work but are deprecated—prefer --show:
swapon --showCustom columns:
swapon --show=NAME,SIZE,USED,PRIONAME SIZE USED PRIO
/swap.img 2.4G 719.1M -1swapon --show=NAME,UUIDNAME UUID
/swap.img d3a0797b-f50c-4a3a-af43-559bff2a1a65Script-friendly output:
swapon --noheadings
swapon --raw
swapon --bytes/swap.img file 2.4G 719.1M -1
NAME TYPE SIZE USED PRIO
/swap.img file 2.4G 719.1M -1
NAME TYPE SIZE USED PRIO
/swap.img file 2597318656 754061312 -1Legacy summary (kB, same data as /proc/swaps):
swapon -sFilename Type Size Used Priority
/swap.img file 2536444 736388 -1Disable swap with swapoff
Disable one swapfile or partition
sudo swapoff /var/tmp/lab-swap-512m
swapon --showOnly /swap.img remains active.
Disable all swap
sudo swapoff -a
free -h | grep Swap:Swap: 0B 0B 0BRe-enable when finished:
sudo swapon -a
free -h | grep Swap:Swap: 2.4Gi 0B 2.4GiOn a busy system, swapoff -a can take a minute or longer while swapped pages are migrated back to RAM.
Other swapon options
| Option | Role |
|---|---|
-d, --discard |
Enable discard on swap devices that support TRIM (swapfiles on ext4 may not) |
-f, --fixpgsz |
Reinitialize swap if page size changed |
-o, --options |
Comma-separated mount-style options (see man swapon) |
-T, --fstab |
Alternate fstab path (used with -a) |
Full flag list:
swapon --helpTroubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
swapon: cannot open … No such file |
Wrong path or missing file | Fix /etc/fstab or create the file; use swapon -ae to skip bad entries at boot |
swapon: … Invalid argument on a file |
Swapfile on tmpfs, btrfs subvolume without proper flags, or similar |
Place the swapfile on ext4/XFS per distro docs |
swapon: … needs -a for -e |
-e only valid with -a |
Run sudo swapon -ae, not swapon -e /path alone |
swapoff hangs |
Large amount of data in swap | Wait; reduce load first; avoid on production without a plan |
Browse the full index in our Linux commands reference.
References
- swapon(8) / swapoff(8) — util-linux manual
Summary
Use swapon --show and free -h to inspect swap, sudo swapon -a or sudo swapon /path to enable, and sudo swapoff /path or sudo swapoff -a to disable. Prepare new files with mkswap on a supported filesystem, persist with /etc/fstab, and use -p for priority. For tuning when swap is used—not just how to toggle it—see the swap and swappiness guide.

