System and processes

The swapon command

Enables partitions or files as swap and shows which swap is in use.

Syntax

swapon --show | swapon DISPOSITIVOTry it in the terminal →

Most useful options

-a, --allEnables the swap entries in /etc/fstab, except those with noauto. Ones already active are skipped silently
-p, --priority <prioridad>Priority from 0 to 32767. A higher number is used before a lower one
-e, --ifexistsSilently skips devices that do not exist
-L <etiqueta>Enables the partition that has that label, instead of its name
-U <uuid>Enables the partition that has that UUID
--show [=columnas]Shows a table of the active swap areas. You can choose comma-separated columns
--noheadingsDoes not print the table header of --show

Copy-ready examples

$ swapon --showtable of the active swap, with its size and how much is used.

Example output

NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition   2G   0B   -2
$ swapon --show=NAME,SIZE,PRIO --noheadingsonly three columns and no header, meant for a script.
$ sudo swapon -p 10 /dev/sdb1enables that partition with priority 10, above the ones with less.
$ sudo swapon -L swap-datosenables the partition with that label, the one set with mkswap -L.
$ sudo swapon -aenables everything fstab marks as swap.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
swapon: cannot open /dev/sdb1: Permission deniedWithout privileges you cannot open the device, and swapon stops before enabling anything.Run it with sudo.
swapon: /swapfile: skipping - it appears to have holes.The file has holes: truncate or cp created it, and swapon rejects it.Create it again without holes with dd if=/dev/zero of=/swapfile bs=1M count=2048, adjusting the size, then chmod 600 and mkswap on it.
swapon: /swapfile: read swap header failedThere is no readable swap header: usually the mkswap step was skipped on that file or partition.Run sudo mkswap /swapfile before enabling it.

Typical mistakes

  • You need sudo: enabling swap is an administrator task, and without privileges the device cannot even be opened.
  • A file with holes, created with truncate or cp, cannot be used as swap. Create it with dd or, since util-linux 2.40, with mkswap --file.
  • If the file is not owned by root or its permissions give access to other users, swapon warns that it is insecure.

Differences from similar commands

  • swapoff: does the opposite: it disables swap. With -a it uses the areas in /proc/swaps or in fstab.
  • mount: mounts file systems. swapon -a only reads the fstab entries of type swap.
  • free: shows total RAM and swap in use. swapon --show lists each active swap area separately.

Related commands

FAQ

What happens to active swap on reboot?

swapon does not save it: it stays active only while the system is running. To come back at boot, list it in /etc/fstab, which swapon -a reads.

What priority does a swap get if I do not use -p?

The kernel assigns it a negative number. With -p you choose a value from 0 to 32767, and the highest number is used first.

Can I enable swap on a regular file?

Yes, if it has no holes, it has been prepared with mkswap, and the system supports it. The manual recommends creating it with dd from /dev/zero, the most portable way.

More System and processes commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course