Home / Alt manpages / swapon(8)

  • swapon(8)
  • Admin command
  • linux

Enable, Inspect and Safely Disable Linux Swap with swapon

A server thrashing under memory pressure at 2am is a bad time to be guessing swapon syntax. This guide gets swapon and swapoff sorted before you need them in a hurry. You will activate an already prepared swap device or file, check that the kernel is using it, and disable one area safely when required. Allow about ten minutes for a single prepared area, plus time to confirm the machine can spare the memory before disabling swap.

The examples use the swapon and swapoff commands from util-linux. The local manpage is from util-linux 2.39.3, while the executable found first in this shell is util-linux 2.42.4 from Linuxbrew. Check your own command path before relying on version-specific output.

1. Check the command and current state

These are read-only checks and do not need elevated privileges:

$ command -v swapon
/home/linuxbrew/.linuxbrew/sbin/swapon
$ swapon --version
swapon from util-linux 2.42.4
$ swapon --show
NAME     TYPE      SIZE USED PRIO
/dev/md0 partition  16G 3.9G   -2

Your path and table will differ. The useful fields are the area name, its type, total size, used bytes and priority. swapon --show is the preferred inspection form in this util-linux family; the older swapon --summary option is deprecated. An empty table means no swap area is currently active; it does not tell you whether a prepared area exists elsewhere.

Checkpoint

Write down the exact device or file you intend to change. Do not use a guessed path such as /dev/sda2; storage layouts vary.

2. Enable one prepared swap area

Enabling swap changes live kernel state, so it requires elevated privileges and should be done only with a device or file that has already been initialised as swap. Replace the placeholder with an exact path:

$ sudo swapon /dev/mapper/example-swap
$ swapon --show
NAME                    TYPE      SIZE USED PRIO
/dev/mapper/example-swap partition   8G   0B   -2

A swap file can be supplied in the same position:

$ sudo swapon /var/lib/swap/swapfile
$ swapon --show=NAME,TYPE,SIZE,USED,PRIO
  • No filesystem holes. Files containing holes, commonly made with cp or truncate, are rejected.
  • Preallocation depends on the filesystem. Btrfs swap files require the conditions documented by btrfs(5), including the nocow attribute on supported Linux versions.
  • NFS may not work. Swap over NFS is not reliably supported.

If you need to create a new area, treat its preparation as a separate, storage-changing task and verify the target before initialising it. If the command succeeds it normally prints nothing; the follow-up --show table is the checkpoint. If it fails, fix the reported path, permissions or swap-format problem; do not add --fixpgsz casually.

3. Enable the configured set from fstab

For boot-time configuration, sudo swapon --all reads entries marked with the swap type in /etc/fstab. Entries with noauto are skipped, and areas already in use are silently skipped:

$ sudo swapon --all
$ swapon --show
NAME                    TYPE      SIZE USED PRIO
/dev/mapper/example-swap partition   8G   0B   -2
/var/lib/swap/swapfile  file        2G   0B   -1

Inspect the relevant fstab lines before running this on a production host:

$ grep -nE '[[:space:]]swap[[:space:]]' /etc/fstab

The default priority is -1. A higher priority is preferred when the kernel chooses among areas, and priorities range from -1 to 32767. For fstab-driven activation, use pri=VALUE in the options field; the command-line equivalent is --priority VALUE. Keep this setting deliberate: changing priority changes paging selection, not the amount of swap available.

4. Use discard only when you have a reason

Some backing devices support discard or trim. You can request it for a one-off activation:

$ sudo swapon --discard=pages /dev/mapper/example-swap
  • --discard=once. Discards the whole swap area once at activation.
  • --discard=pages. Asynchronously discards freed pages.
  • Bare --discard. Enables both policies.

The manpage cautions that discard often does not improve performance, so measure before making it a persistent fstab option. The fstab spellings are discard, discard=once and discard=pages.

5. Disable one area, with a memory check first

Warning

swapoff forces used pages back into RAM or another active swap area. It can fail when the system has insufficient memory and can increase pressure on a busy host. Check usage before changing state:

$ swapon --show
$ free -h
$ sudo swapoff /var/lib/swap/swapfile
$ swapon --show

The final table should no longer contain the disabled area. The undo is to run sudo swapon /var/lib/swap/swapfile again, provided the file still exists and remains a valid swap area. If the area is listed in /etc/fstab, disabling it is temporary until the next swapon --all or boot; remove or comment out the fstab entry only as a separate, deliberate configuration change.

sudo swapoff --all disables every known swap area. Reserve it for controlled maintenance. Since util-linux 2.36, its exit status distinguishes outcomes: 0 means all succeeded, 32 means all failed, and 64 means some succeeded and some failed. Check the status and then inspect swapon --show rather than assuming the command was all-or-nothing.

6. Diagnose the likely traps

  • "Invalid argument" or a rejected file. Verify the file is prepared swap and has no filesystem holes. Do not overwrite it with mkswap until you have confirmed the path, because initialisation destroys existing file content.
  • An area is missing after --all. Inspect /etc/fstab for noauto, a wrong type, a missing path or an intentional nofail setting.
  • Disabling fails or the host becomes pressured. Stop and restore capacity with another active area if available. A swapoff exit status of 2 means insufficient memory; 4 reports another swapoff syscall failure.
  • Unexpected command behaviour. Compare command -v swapon, swapon --version and the installed manpage. A locally installed alternate binary can have newer options than the distribution package.

Done means

  • Area is active. The intended device or file appears in swapon --show after activation.
  • Numbers checked, not just status. You checked size, usage and priority instead of treating a zero exit status as the whole verification.
  • Fstab options intentional. Any fstab entry has the intended noauto, nofail, priority and discard behaviour.
  • Memory checked before disabling. You checked available memory before running swapoff, and know the matching swapon command to restore a temporarily disabled area.