System and processes
The swapon command
Enables partitions or files as swap and shows which swap is in use.
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 --showCopy-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
| Message | Cause | Fix |
|---|---|---|
| swapon: cannot open /dev/sdb1: Permission denied | Without 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 failed | There 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
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.
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