Rename a swap partition without thinking it through and you can leave a boot entry pointing at a UUID that no longer exists. swaplabel reads and changes a swap area's label and UUID without recreating it, which is exactly the tool for doing this safely. Allow about ten minutes.
swaplabel command from util-linux and the device or regular file that contains the swap area.First confirm the binary and its version. This is a read-only check:
$ command -v swaplabel
/home/linuxbrew/.linuxbrew/sbin/swaplabel
$ swaplabel --version
swaplabel from util-linux 2.42.4
The local manual page is generated from util-linux 2.39.3, while the command found first in this shell reports util-linux 2.42.4. The option syntax used here is present in both, but keep that distinction in mind when comparing output from another host. The Debian package database on this machine reports util-linux 2.39.3-9ubuntu6.6, which is not the binary command -v actually selected.
Pass the swap device or file as the only positional argument. Replace the example path with the exact target you have checked:
$ swaplabel /dev/sdX2
LABEL: WORKSTATION_SWAP
UUID: 1399cb87-2d98-4f92-9ce8-1e55039d99b7
Your label and UUID will differ. A regular swap file is also accepted, for example /var/swapfile. Save the current values before changing anything: they are the recovery values if another configuration still refers to the old metadata.
Checkpoint: make sure the output identifies the intended swap area. If the path is wrong, stop here. Do not use mkswap to discover a device: it creates or rewrites a swap signature and is a different, potentially destructive operation.
A label or UUID is often referenced by /etc/fstab, an initramfs configuration, a boot menu, monitoring, or an automation script. Search for the old value before changing it:
$ grep -R --line-number --fixed-strings \
'1399cb87-2d98-4f92-9ce8-1e55039d99b7' \
/etc/fstab /etc/default /etc/initramfs-tools 2>/dev/null
Use the label instead if that is what your configuration contains. Review each match rather than replacing text blindly: changing a UUID can make a boot or activation entry stop finding the swap area until its reference is updated.
Safety boundary: changing metadata is a state change, and changing a UUID can affect boot-time behaviour. If the swap area is active, schedule the change for a suitable maintenance window even though swaplabel can change an actively used swap device. Do not disable live swap merely to follow this guide unless your operational procedure requires it.
Use -L or --label with a new label. On a real device, add the privilege your device permissions require:
$ sudo swaplabel --label WORKSTATION_SWAP /dev/sdX2
$ sudo swaplabel /dev/sdX2
LABEL: WORKSTATION_SWAP
UUID: 1399cb87-2d98-4f92-9ce8-1e55039d99b7
The UUID is unchanged in this example. Labels are limited to 16 characters. A longer value is truncated by the installed command and a warning is printed, so choose a short value deliberately rather than relying on truncation:
$ swaplabel --label THIS_LABEL_IS_LONGER_THAN_SIXTEEN /tmp/test.swap
swaplabel: label is too long. Truncating it to 'THIS_LABEL_IS_L'
LABEL: THIS_LABEL_IS_L
UUID: 01234567-89ab-cdef-0123-456789abcdef
Use -U or --uuid with a standard 8-4-4-4-12 UUID. Generate or obtain the value through your normal system process, then verify it before using it:
$ sudo swaplabel --uuid 01234567-89ab-cdef-0123-456789abcdef /dev/sdX2
$ sudo swaplabel /dev/sdX2
LABEL: WORKSTATION_SWAP
UUID: 01234567-89ab-cdef-0123-456789abcdef
The command rejects malformed UUIDs rather than silently accepting them:
$ swaplabel --uuid not-a-uuid /tmp/test.swap
swaplabel: failed to parse UUID: not-a-uuid
Do not invent a UUID that is already assigned to another filesystem or swap area. After a UUID change, update any configuration found in step 3, then repeat the read command and check the activation path used by your system. A successful metadata write does not update /etc/fstab for you.
There is no separate undo switch. Restore the exact value you recorded in step 2.
$ sudo swaplabel --label OLD_LABEL /dev/sdX2
$ sudo swaplabel /dev/sdX2
For a UUID change, use the old UUID in the same way:
$ sudo swaplabel --uuid 1399cb87-2d98-4f92-9ce8-1e55039d99b7 /dev/sdX2
$ sudo swaplabel /dev/sdX2
Recovery: replace OLD_LABEL and the example UUID with the values you actually recorded. If the value was not recorded, inspect configuration and deployment records before making another change. Do not run mkswap as a recovery action, because recreating the signature is not an undo for a metadata edit.
If swaplabel reports that it cannot read the device, check the path and permissions without changing the target:
$ ls -l /dev/sdX2
$ sudo swaplabel /dev/sdX2
Use sudo only when the device is readable by root but not by your account. It does not repair a wrong path or turn an ordinary file into a swap area.
swaplabel binary and util-linux version you are using.mkswap on an existing swap area as a discovery or recovery shortcut.