MKSWAP(8)

๐Ÿ’– MKSWAP(8) โ€” aka mkswap, the swap-area glow-up station โœจ

okay bestie, welcome to the System Administration section of the manual, this is mkswap(8) and she is iconic ๐Ÿ’…


โœจ NAME โœจ

mkswap โ€” set up a Linux swap area ๐Ÿซถ


๐ŸŒธ SYNOPSIS ๐ŸŒธ

mkswap [options] device [blocks]

mkswap [options] --size size --file file

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So like, mkswap sets up a Linux swap area on a device or in a file. That's the whole job. That's her main character arc. ๐Ÿ‘‘

The device argument is usually gonna be a disk partition (something like /dev/sdb7) but ngl it can also totally be a file. Here's the tea though: the Linux kernel itself does not look at partition IDs, but a bunch of installation scripts will just assume that partitions of hex type 82 (LINUX_SWAP) are meant to be swap partitions. (Warning: Solaris also uses this type. Be careful not to kill your Solaris partitions!! ๐Ÿ˜ญ please I'm begging)

The blocks parameter is superfluous but it's been retained for backwards compatibility. (It specifies the desired size of the swap area in 1024-byte blocks. mkswap will use the entire partition or file if it is omitted. Specifying it is unwise โ€” a typo may destroy your disk. like. the whole disk. gone. ๐Ÿ’€)

After creating the swap area, you need the swapon(8) command to start actually using it. Usually swap areas are listed in /etc/fstab so they can be taken into use at boot time by a swapon -a command in some boot script. slay, automation. โœจ


โš ๏ธ WARNING โš ๏ธ

bestie this section is the "read it before you wreck it" section, pls don't skim ๐Ÿšจ

The swap header does not touch the first block. A boot loader or disk label can be there, but that is not a recommended setup. The recommended setup is to use a separate partition for a Linux swap area. ๐ŸŽ€

mkswap, like many others mkfs-like utils, erases the first partition block to make any previous filesystem invisible. bye bye old data ๐Ÿ‘‹

However, mkswap refuses to erase the first block on a device with a disk label (SUN, BSD, ...). she said "not today" ๐Ÿ’…


๐Ÿ’œ OPTIONS ๐Ÿ’œ

  • -c, --check โ€” Check the device (if it is a block device) for bad blocks before creating the swap area. If any bad blocks are found, the count is printed. ๐Ÿ”
  • -F, --file โ€” Create a swap file with the appropriate file permissions and populated blocks on disk.
  • -f, --force โ€” Go ahead even if the command is stupid. ๐Ÿ˜ญ This allows the creation of a swap area larger than the file or partition it resides on. Also, without this option, mkswap will refuse to erase the first block on a device with a partition table.
  • -q, --quiet โ€” Suppress output and warning messages. ๐Ÿคซ
  • -L, --label label โ€” Specify a label for the device, to allow swapon(8) by label. ๐Ÿท๏ธ
  • --lock[=mode] โ€” Use an exclusive BSD lock for the device or file that is operated upon. The optional argument mode can be yes (1), no (0), or nonblock. If the mode argument is omitted, it defaults to yes. This option overrides the environment variable $LOCK_BLOCK_DEVICE. The default is to not use any lock at all, but using a lock is recommended to avoid collisions with systemd-udevd(8) or other tools. ๐Ÿ”’
  • -p, --pagesize size โ€” Specify the page size (in bytes) to use. This option is usually unnecessary; mkswap reads the size from the kernel. ๐Ÿง 
  • -U, --uuid UUID โ€” Specify the UUID to use. The default is to generate a UUID. The format of the UUID is a series of hex digits separated by hyphens, like this: "c1b9d5a2-f162-11cf-9ece-0020afc76f16". The UUID parameter may also be one of the following:
    • clear โ€” clear the filesystem UUID ๐Ÿงผ
    • random โ€” generate a new randomly-generated UUID ๐ŸŽฒ
    • time โ€” generate a new time-based UUID โฐ
  • -e, --endianness ENDIANNESS โ€” Specify the ENDIANNESS to use, valid arguments are native, little or big. The default is native. ๐ŸŒ
  • -o, --offset offset โ€” Specify the offset to write the swap area to.
  • -s, --size size โ€” Specify the size of the created swap file in bytes and may be followed by a multiplicative suffix: KiB (=1024), MiB (=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is optional, e.g., "K" has the same meaning as "KiB"). If the file exists and is larger than size, it will be truncated to this size. This option only makes sense when used with --file. ๐Ÿ“
  • -v, --swapversion 1 โ€” Specify the swap-space version. (This option is currently pointless, as the old -v 0 option has become obsolete and now only -v 1 is supported. The kernel has not supported v0 swap-space format since 2.5.22 (June 2002). The new version v1 is supported since 2.1.117 (August 1998).) ๐Ÿ•ฐ๏ธ
  • --verbose โ€” Verbose execution. With this option mkswap will output more details about detected problems during swap area set up. ๐Ÿ—ฃ๏ธ
  • -h, --help โ€” Display help text and exit. ๐Ÿ†˜
  • -V, --version โ€” Display version and exit. ๐Ÿ†

๐ŸŒˆ ENVIRONMENT ๐ŸŒˆ

  • LIBBLKID_DEBUG=all โ€” enables libblkid debug output. ๐Ÿ›
  • LOCK_BLOCK_DEVICE=<mode> โ€” use exclusive BSD lock. The mode is "1" or "0". See --lock for more details. ๐Ÿ”

๐ŸŽ€ NOTES ๐ŸŽ€

The maximum useful size of a swap area depends on the architecture and the kernel version. iykyk.

The maximum number of the pages that is possible to address by swap area header is 4294967295 (32-bit unsigned int). The remaining space on the swap device is ignored. ๐Ÿ™ˆ

Presently, Linux allows 32 swap areas. The areas in use can be seen in the file /proc/swaps. ๐Ÿ‘€

mkswap refuses areas smaller than 10 pages. tiny swap? denied. ๐Ÿ’…

If you don't know the page size that your machine uses, you can look it up with getconf PAGESIZE. โœจ

Aside from mkswap --file, it is also possible to create the swapfile manually before initializing it with mkswap, e.g. using a command like

# dd if=/dev/zero of=swapfile bs=1MiB count=$((8*1024))

to create 8GiB swapfile. ๐Ÿ“

Since version 2.41, mkswap --file sets the nocow attribute for newly created files to support swapfiles on Btrfs. ๐ŸŒณ

In such a case, please read notes from swapon(8) about the swap file use restrictions (holes, preallocation and copy-on-write issues). ๐Ÿ“


๐Ÿ’ซ SEE ALSO ๐Ÿ’ซ

fdisk(8), swapon(8)


๐Ÿž REPORTING BUGS ๐Ÿž

For bug reports, use the issue tracker <https://github.com/util-linux/util-linux/issues>. go tell them bestie, they wanna know ๐Ÿซถ


๐Ÿ‘‘ AVAILABILITY ๐Ÿ‘‘

The mkswap command is part of the util-linux package which can be downloaded from Linux Kernel Archive <https://www.kernel.org/pub/linux/utils/util-linux/>. ๐Ÿ“ฆ


and that's the whole page bestie!! ๐Ÿฆ‹ this one's from util-linux 2.42.4, dated 2026-09-21 โ€” swapon responsibly, and remember: a typo in blocks can eat your disk, so maybe just don't ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: util-linux 2.42.4-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND ISC AND LGPL-2.1-or-later AND LicenseRef-PublicDomain
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms