Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Disks and Filesystems

Linux swapon and mkswap commands

Reference for swapon, swapoff and mkswap: create a swap file or partition, enable it, make it permanent in fstab, check usage with /proc/swaps and tune swappiness.

Swap is disk space the kernel can use when physical memory is under pressure. It lets the system move rarely used memory pages out of RAM, keeps the page cache larger, and gives a safety margin before the out-of-memory killer starts ending processes. It is also where a laptop writes its memory when it hibernates.

Three tools manage it: mkswap writes a swap signature onto a partition or file, swapon tells the kernel to start using it, and swapoff stops using it. You can swap on a dedicated partition, a logical volume, or an ordinary file.

Terminal
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Try it

These commands need root and the sandbox has no swapon or mkswap, so the creating steps above are read-only. The examples below do what is possible without privileges: reading the kernel's swap information.

Bash Read swap status from /proc

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Swappiness and a file for a swap area

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
mkswap [-L LABEL] [-U UUID] [-f] DEVICE_OR_FILE
swapon [-p PRIORITY] [-a] [DEVICE_OR_FILE...]
swapon --show
swapoff [-a] [DEVICE_OR_FILE...]
Command or option Description
mkswap DEVICE Write a swap signature; erases whatever was on the device.
mkswap -L LABEL Give the area a label, usable as LABEL=... in fstab.
mkswap -f Force, even if the target looks in use or is larger than needed.
swapon FILE Enable that area now.
swapon -a Enable all areas marked swap in /etc/fstab (except noauto).
swapon --show Print active areas with size, usage and priority.
swapon -p N Set priority 0-32767; higher priority areas fill first, equals are used round-robin.
swapoff FILE / -a Stop using the area; pages are read back into memory first.

Returns: 0 on success. Non-zero for failures such as "Operation not permitted" (not root), "Invalid argument" (no swap signature, or a file with holes) or "insufficient permissions 0644" warnings for files readable by others.

Usage notes

  • Swap files must be fully allocated. fallocate works on ext4 and xfs; on btrfs, copy-on-write files and holes need special handling (btrfs filesystem mkswapfile). Using dd always produces a fully written file, just slower.
  • Mode 0600, owned by root. Newer swapon refuses or warns about world-readable swap files because they would expose memory contents.
  • Permanent swap means an fstab line: /swapfile none swap sw 0 0, or UUID=... none swap sw 0 0 for a partition. See fstab and mount options.
  • How much swap? There is no single rule. A few GB is enough on most servers; hibernation needs roughly the size of RAM. Swap does not make a machine with too little RAM fast: constant paging ("thrashing") makes it crawl.
  • vm.swappiness controls how eagerly anonymous memory is swapped compared with dropping cache. Change it at run time with sudo sysctl vm.swappiness=10 and keep it in /etc/sysctl.d/.
  • zram and zswap compress pages in RAM instead of (or before) writing them to disk. Many distributions enable one of them by default and have little or no disk swap.
  • Swapping to an encrypted device keeps memory contents private; for hibernation the swap must be available early in boot.
  • swapoff can take a long time and fails if the remaining RAM cannot hold the swapped-out pages.
  • Check usage with free -h, swapon --show or vmstat 1, where the si and so columns show swap-in and swap-out per second.