swapon and swapoff Command in Linux [Cheat Sheet]

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.

WARNING
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

text
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:

bash
free -h
swapon --show
text
total        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   -1

USED in swapon --show matches the swap line in free. The kernel exposes the same entries in /proc/swaps (sizes in kilobytes):

bash
cat /proc/swaps
text
Filename				Type		Size		Used		Priority
/swap.img                               file		2536444		736388		-1

For 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):

bash
sudo fallocate -l 512M /var/tmp/lab-swap-512m
sudo chmod 600 /var/tmp/lab-swap-512m
sudo mkswap /var/tmp/lab-swap-512m
text
Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=712c564c-1abc-4308-8668-584bc1bce2d0

Make it persistent by adding a line to /etc/fstab (example):

text
/var/tmp/lab-swap-512m  none  swap  sw  0  0

Ubuntu’s default fstab entry for the installer swapfile looks like:

bash
grep swap /etc/fstab
text
/swap.img	none	swap	sw	0	0

Enable swap with swapon

Enable all fstab swap entries

bash
sudo swapon -a
swapon --show

After swapoff -a, re-enabling restores the swapfile with zero use until pressure returns:

text
NAME      TYPE SIZE USED PRIO
/swap.img file 2.4G   0B   -1

Enable one swapfile

bash
sudo swapon /var/tmp/lab-swap-512m
swapon --show
text
NAME                   TYPE SIZE   USED PRIO
/swap.img              file 2.4G 719.1M   -1
/var/tmp/lab-swap-512m file 512M     0B   -1

Set swap priority

Higher PRIO values are used before lower ones. Disable first, then enable with -p:

bash
sudo swapoff /var/tmp/lab-swap-512m
sudo swapon -p 10 /var/tmp/lab-swap-512m
swapon --show
text
NAME                   TYPE SIZE   USED PRIO
/swap.img              file 2.4G 719.1M   -1
/var/tmp/lab-swap-512m file 512M     0B   10

Verbose enable

-v prints how util-linux detected the swap signature:

bash
sudo swapon -v /var/tmp/lab-swap-512m
text
swapon: /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-512m

Skip 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:

bash
# fstab contains /missing-swap (does not exist) and /swap.img
sudo swapon -aT /tmp/fstab-swap-test
text
swapon: cannot open /missing-swap: No such file or directory
bash
sudo swapon -aeT /tmp/fstab-swap-test
swapon --show
text
NAME      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:

bash
swapon --show

Custom columns:

bash
swapon --show=NAME,SIZE,USED,PRIO
text
NAME      SIZE   USED PRIO
/swap.img 2.4G 719.1M   -1
bash
swapon --show=NAME,UUID
text
NAME      UUID
/swap.img d3a0797b-f50c-4a3a-af43-559bff2a1a65

Script-friendly output:

bash
swapon --noheadings
swapon --raw
swapon --bytes
text
/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   -1

Legacy summary (kB, same data as /proc/swaps):

bash
swapon -s
text
Filename				Type		Size		Used		Priority
/swap.img                               file		2536444		736388		-1

Disable swap with swapoff

Disable one swapfile or partition

bash
sudo swapoff /var/tmp/lab-swap-512m
swapon --show

Only /swap.img remains active.

Disable all swap

bash
sudo swapoff -a
free -h | grep Swap:
text
Swap:             0B          0B          0B

Re-enable when finished:

bash
sudo swapon -a
free -h | grep Swap:
text
Swap:          2.4Gi          0B       2.4Gi

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

bash
swapon --help

Troubleshooting

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


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.


Frequently Asked Questions

1. What is the difference between swapon and swapoff?

swapon activates a swap partition or swapfile for paging; swapoff deactivates it. Both require root or sudo. Use swapon --show to list active swap and free -h to see used swap space.

2. How do I enable all swap entries from fstab?

Run sudo swapon -a. That reads /etc/fstab and enables every line with the swap filesystem type. On Ubuntu you will usually see /swap.img enabled this way after boot or after swapoff -a.

3. How do I disable swap temporarily on Linux?

sudo swapoff -a turns off all active swap devices and files. Re-enable with sudo swapon -a. Avoid swapoff on production unless you accept OOM risk while swap is off.

4. Is swapon -s still valid?

It works but is deprecated. Prefer swapon --show for a readable table, or swapon --bytes when you need exact byte counts.

5. Why does swapon fail on my swapfile?

Common causes: file not initialized with mkswap, permissions not 600, path on tmpfs or an unsupported filesystem, or a stale fstab entry. swapon -v path prints verbose errors.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)