Home / Alt manpages / mkswap(8)

  • mkswap(8)
  • Admin command
  • linux

Create and Verify a Linux Swap File with mkswap

You will create a file-backed Linux swap area, check that the kernel-visible metadata is right, and activate it only when you are ready. The example uses an 8 GiB file named /var/swapfile. It does not touch a disk partition.

Allow about ten minutes, plus the time needed to allocate the file. You need a shell, mkswap from util-linux, enough free space, and elevated privileges for the target directory. The installed manpage on this machine documents util-linux 2.39.3. The mkswap found on this shell is util-linux 2.41.3, so check the local help if you are scripting across distributions.

Warning

mkswap writes a swap header and makes a previous filesystem on its target unusable. Treat the path as destructive. Read it twice before pressing Enter.

1. Check the command and the destination

Confirm which executable and package you are about to use. These checks are ordinary and do not need sudo:

$ command -v mkswap
/usr/sbin/mkswap
$ mkswap --version
mkswap from util-linux 2.41.3

Choose a path that is not already used by a mounted filesystem or another service. Inspect it before creating anything:

$ sudo findmnt /var
$ sudo ls -ld /var /var/swapfile
ls: cannot access '/var/swapfile': No such file or directory

The exact findmnt output is host-specific. The useful checkpoint is that /var/swapfile is absent, or that you have deliberately decided to replace it. If a file already exists, stop and identify it with ls -lh and file. Do not initialise an unknown file just because its name looks convenient.

2. Create the swap file

Create an 8 GiB file with fallocate, if it is available:

$ sudo fallocate --length 8G /var/swapfile
$ sudo chmod 600 /var/swapfile
$ sudo ls -lh /var/swapfile
-rw------- 1 root root 8.0G ... /var/swapfile

Restricting the file to root is a useful security boundary. Swap can contain pages from processes that may hold credentials or other sensitive data, so do not leave it world-readable.

If fallocate is unavailable, use a zero-filled file instead:

$ sudo dd if=/dev/zero of=/var/swapfile bs=1MiB count=8192 status=progress
$ sudo chmod 600 /var/swapfile

Use one method, not both. The command may take longer with dd. If either command fails, check free space with df -h /var and remove only the incomplete file you just created, after checking its exact path:

$ sudo ls -lh /var/swapfile
$ sudo rm -- /var/swapfile

That rm is the recovery action for this example, not a general clean-up command. Do not run it against a path you have not rechecked.

3. Initialise the file with mkswap

Pass the file path as the device argument. Omit the optional size argument: the manpage retains it for backwards compatibility and warns that a typo can damage a disk.

$ sudo mkswap --label host-swap /var/swapfile
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
LABEL=host-swap, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

The UUID is generated by default. A label lets swapon refer to this area by name, while the UUID is useful for a stable /etc/fstab entry. The final UUID on your machine will differ.

Checkpoint

A successful command is not enough if the wrong path was supplied. Verify the metadata without activating swap:

$ sudo blkid -o export /var/swapfile
LABEL=host-swap
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
TYPE=swap

If blkid does not report TYPE=swap, stop. Check the path, permissions and command output rather than trying --force. The documented force option permits unsafe situations, including creating an area larger than its backing file or erasing the first block on a device with a partition table.

4. Activate it deliberately

mkswap only writes the swap area. It does not start using it. Activate the verified file with swapon:

$ sudo swapon /var/swapfile
$ swapon --show
NAME           TYPE SIZE USED PRIO
/var/swapfile  file   8G   0B   -2

swapon --show displays active areas; sizes and priority vary by system. This step changes kernel state and may affect memory pressure behaviour, so perform it during a suitable maintenance window on a busy host. If activation fails, leave the file in place while you inspect the error and sudo file /var/swapfile; do not repeatedly re-run mkswap on it.

To undo activation without deleting the file, use:

$ sudo swapoff /var/swapfile
$ swapon --show

swapoff can require free RAM while it moves pages back into memory. On a pressured machine it may fail or take time. Do not treat a failed swapoff as permission to delete the file.

5. Make activation persistent only after testing

If the swap area behaves as expected, add it to /etc/fstab so a boot-time swapon -a can find it. Use the UUID printed by blkid, not the example value:

$ sudoedit /etc/fstab
# Add one line, with your real UUID:
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx none swap sw 0 0

Keep the entry as one line. Before rebooting, ask findmnt and swapon to read the configuration now:

$ sudo swapoff /var/swapfile
$ sudo swapon -a
$ swapon --show
/var/swapfile  file   8G   0B   -2

If swapon -a reports a bad entry, restore the previous /etc/fstab line from your backup or remove the line you just added with sudoedit. Do not reboot until the configuration parses cleanly. The file itself remains available for another test.

Common traps

  • Do not pass a human size such as 8G as the old positional size argument. Leave it out and size the file before initialisation.
  • Do not use --force to silence a warning about a partition or disk label. Investigate the target first. mkswap erases the first partition block when it creates a swap area, although it refuses some devices with disk labels.
  • Do not assume a file is suitable just because it has the right length. Swap-file restrictions include holes, preallocation and copy-on-write behaviour; check the installed swapon(8) documentation for your filesystem and util-linux version.
  • Do not confuse a valid swap header with active swap. Use blkid for metadata and swapon --show for kernel state.

Done means

  • The intended file path was checked before it was changed.
  • The file is the intended size and has mode 0600.
  • mkswap reported a swap UUID and blkid reported TYPE=swap.
  • swapon --show lists the area only if you deliberately activated it.
  • Any /etc/fstab entry uses the real UUID and passes a live swapon -a test.