How to Inspect and Change Swap Partition Labels and UUIDs in Linux Using swaplabel

In Linux system administration, storage devices and partitions are frequently referenced in /etc/fstab using Universally Unique Identifiers (UUIDs) or human-readable filesystem labels (such as LABEL=SWAP-NVME) rather than volatile device nodes like /dev/sdb2 or /dev/nvme0n1p2. Device paths can shift when hardware is added or controller buses reorder during boot, potentially preventing the kernel from mounting the designated swap space. While standard utilities like e2label or tune2fs operate exclusively on ext2/ext3/ext4 filesystems, swap partitions maintain a specialised binary header format. The swaplabel utility, part of the standard util-linux package, provides administrators with a dedicated tool to inspect, assign, and modify labels and UUIDs on existing swap partitions without reformatting them.

Understanding Swap Headers and Partition Identifiers

When a partition is prepared as swap space using mkswap, the kernel writes a swap signature header containing page size information, bad block lists, a 128-bit UUID, and an optional 16-character filesystem volume label. Identifiers provide distinct advantages:

  • Filesystem Labels: Provide human-readable names (e.g. LABEL=swap-fast or LABEL=backup-swap) that make disk configuration in /etc/fstab and storage scripts self-explanatory.
  • UUIDs: Provide 128-bit cryptographically random hex strings that guarantee unique hardware references across multiple physical drives, SAN volumes, and software RAID arrays.
  • Preserving Swap Data: Traditionally, changing a swap label required running mkswap -L newlabel /dev/sdX, which overwrites the entire header and generates a completely new UUID, breaking existing fstab entries. swaplabel modifies only the target field in the existing header, preserving partition UUIDs or updating them selectively.

Inspecting Active Swap Partitions and Existing Labels

Before modifying swap attributes, inspect which swap devices are currently active on your Linux system using swapon or lsblk:

swapon --show

To view the current block devices alongside their UUIDs and labels:

lsblk -f

To inspect the swap header directly using swaplabel, execute the command followed by the device node path:

sudo swaplabel /dev/sdb2

The command outputs the current configuration:

LABEL: swap-primary
UUID:  4b7a1f82-3d8e-4a6c-912b-8910e527a412

If no label has been assigned previously, the LABEL: field will display as blank.

Deactivating the Swap Partition Before Modifications

Although swaplabel only updates header bytes, modifying active memory swap partitions can risk race conditions with the kernel virtual memory manager. Always deactivate the swap space before changing labels or UUIDs:

sudo swapoff -v /dev/sdb2

Verify that the partition is no longer listed in active swap memory:

swapon --show

Changing the Swap Partition Label with swaplabel -L

To assign a new label to the swap partition, use the -L (or --label) option. Note that Linux swap volume labels are restricted to a maximum of 16 characters:

sudo swaplabel -L "SWAP-SYSTEM" /dev/sdb2

Verify that the new label has been recorded in the partition header:

sudo swaplabel /dev/sdb2

To completely remove an existing label without replacing it, pass an empty string:

sudo swaplabel -L "" /dev/sdb2

Generating and Assigning a New UUID with swaplabel -U

If you have cloned a disk image (for example, using dd or virtual machine template cloning), multiple disks may share identical swap UUIDs, creating conflicts during boot. You can generate a fresh UUID using the -U (or --uuid) option.

To assign a specific UUID string:

sudo swaplabel -U "a1b2c3d4-e5f6-7890-abcd-ef1234567890" /dev/sdb2

To instruct swaplabel to automatically generate a brand new random UUID, use the keyword random:

sudo swaplabel -U random /dev/sdb2

Verify the new UUID by querying the header:

sudo swaplabel /dev/sdb2

Updating /etc/fstab and Reactivating Swap Space

If you changed the partition’s UUID or switched mounting methods from device path to label, update /etc/fstab to prevent boot stalls.

Open /etc/fstab in your editor:

sudo nano /etc/fstab

Configure the swap line using either the new label:

LABEL=SWAP-SYSTEM   none   swap   sw   0   0

Or using the newly generated UUID:

UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890   none   swap   sw   0   0

Save and close the file. Finally, reactivate the swap partition:

sudo swapon -a

Confirm that the partition is mounted and active:

swapon --show

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.