System and processes
The mkswap command
Prepares a partition or a file as a swap area, ready to be enabled.
Most useful options
-c, --checkChecks for bad blocks before creating the area and prints how many it finds-F, --fileCreates a swap file with the right permissions and its blocks already written to disk. Only since util-linux 2.40-f, --forceDoes it even if it looks like a bad idea: allows an area larger than the device and erasing the first block of a disk with a partition table-q, --quietSuppresses output and warning messages-L, --label <etiqueta>Sets a label on the area, to enable it later with swapon -L-s, --size <tamaño>Size of the swap file, with suffixes such as M or G. Only makes sense with --file and, like it, exists since util-linux 2.40Copy-ready examples
$ lsblkfinds the exact partition before writing to it.$ sudo mkswap --size 2G --file /swapfilecreates a 2 GiB swap file, with util-linux 2.40 or later. Use a name that does not exist yet.$ sudo mkswap -L swap-datos /dev/sdb1sets up the partition as swap with a label. Destructive: the first block is erased and what was before stops being visible. Confirm the name with lsblk.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| mkswap: error: /dev/sdb1 is mounted; will not make swapspace | The partition is mounted, and mkswap refuses to write to a mounted device. | Unmount it with sudo umount /dev/sdb1 and run the command again. |
| mkswap: cannot open /dev/sdb1: Permission denied | Without administrator privileges you cannot open the device for writing. | Run it with sudo. |
Typical mistakes
- mkswap only prepares the area: the system does not use it until you run swapon.
- On a partition, mkswap erases its first block so that anything that was there before stops being visible. If you want to keep data, do not use it.
- If the device has a partition table, mkswap refuses to erase its first block without -f. Point it at the partition, not the whole disk.
Differences from similar commands
- mkfs: builds a file system. mkswap sets up a swap area, which the kernel does not mount as files.
- dd: copies data byte by byte. You can use dd to create the empty file; mkswap then writes the swap header into it.
- fdisk: creates and edits the partition table of a disk. mkswap works inside a partition that already exists.
Security
mkswap on the wrong device erases its first block. Confirm the name with lsblk before running it with sudo.
FAQ
Why is mkswap alone not enough to use swap?
Because mkswap only sets up the area. swapon enables it and, to survive a reboot, it must be listed in /etc/fstab.
How do I change the size of a swap area already created?
Turn it off with swapoff, grow the partition or recreate the file, run mkswap on it and enable it with swapon. mkswap generates a new UUID: if fstab uses the old one, update it.
Is a file or a partition better for swap?
Both work. A partition avoids the problems with holes, copy-on-write on Btrfs and NFS that the swapon manual documents.
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