1. Introduction

Dealing with a partition often means using its Universal Unique Identifier (UUID). In fact, a swap partition is no different in this regard.

In this tutorial, we talk about ways to get and change the UUID of a swap partition. First, we briefly refresh our knowledge about the swap partition and how to list it. After that, we turn to a ubiquitous command for swap operations. Next, we use two fairly common ways to get data about different devices. Finally, we see how we can influence the current swap UUID.

We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.2.15. It should work in most POSIX-compliant environments unless otherwise specified.

2. Swap Partition

Linux and other UNIX-like operating systems employ swap as a means to compensate for low main memory (RAM).

As such, it’s not a typical universal storage container. This is underlined by the separate commands we have for managing swap:

$ swapon --show
NAME      TYPE      SIZE  USED PRIO
/dev/sdx3 partition 666M 66.6M   -2

Here, we employ the swapon command with its –show option. Thus, we see the current swap partition, its /dev path, size, and used space.

So, if a swap partition is special, does it still have a UUID like other storage containers?

3. Using swapon

Indeed, in addition to enabling swap, the swapon command can also show detailed information about the current swap partition settings:

$ swapon --show --output-all
NAME      TYPE      SIZE  USED PRIO UUID                                 LABEL
/dev/sdx3 partition 666M 66.6M   -2 5ebcaaf0-e098-43b9-beef-1f8deedd135e

The difference with our earlier example is the addition of the –output-all flag, which ensures the output contains all available columns. One of these columns is the UUID of the swap partition.

4. Using blkid

The blkid command can list different attributes such as identifiers of block devices that are currently in use.

Since we already know swap partitions have a block device path like /dev/sdx3, we can expect blkid to show information about them:

$ blkid
[...]
/dev/sdx2: UUID="a19b6660-1d06-44fc-d0d0-0a9bbeadbbd8" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="16660ded-02"
/dev/sdx1: UUID="606660e8-7a07-4fc2-dead-27eddeade7a0" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="16660ded-01"
/dev/sdx3: UUID="5ebcaaf0-e098-43b9-beef-1f8deedd135e" TYPE="swap" PARTUUID="16660ded-03"

Thus, we see the UUID of our one and only swap partition is 5ebcaaf0-e098-43b9-beef-1f8deedd135e.

To be more specific, we can even filter by the partition path:

$ blkid /dev/sdx3
/dev/sdx3: UUID="5ebcaaf0-e098-43b9-beef-1f8deedd135e" TYPE="swap" PARTUUID="16660ded-03"

In this case, we supply the block device path /dev/sdx3 and get information about that partition alone.

Finally, we can also filter by type if we supply TYPE=swap to the –match-token option:

$ blkid --match-token TYPE=swap
/dev/sdx3: UUID="5ebcaaf0-e098-43b9-beef-1f8deedd135e" TYPE="swap" PARTUUID="16660ded-03"

This is especially helpful when dealing with more than one swap partition.

5. Using lsblk

Unlike blkid, the lsblk command can also list devices that aren’t currently mounted:

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
[...]
sdx      8:0    0   32G  0 disk
├─sdx1   8:1    0 20.0G  0 part /
├─sdx2   8:2    0    1K  0 part
└─sdx3   8:3    0  666M  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

By default, lsblk doesn’t list the UUID of partitions but shows the proper type of [SWAP] in the MOUNTPOINTS column.

To get the UUID of a swap partition as well, we can use the –output switch along with a column list that includes UUID:

$ lsblk --output NAME,SIZE,TYPE,UUID
NAME    SIZE FSTYPE UUID
[...]
sdx      32G
├─sdx1 20.0G ext4   606660e8-7a07-4fc2-dead-27eddeade7a0
├─sdx2    1K        a19b6660-1d06-44fc-d0d0-0a9bbeadbbd8
└─sdx3  666M swap   5ebcaaf0-e098-43b9-beef-1f8deedd135e
sr0    1024M rom

Now, we can also see the UUID and FSTYPE of each partition. For our /dev/sdx3 swap partition, they are 5ebcaaf0-e098-43b9-beef-1f8deedd135e and swap respectively.

If we want to filter by the partition type alone, we can do so by passing the output through a tool like grep:

$ lsblk --output NAME,SIZE,FSTYPE,UUID | grep '[[:space:]]swap[[:space:]]'
└─sdx3  666M swap   5ebcaaf0-e098-43b9-beef-1f8deedd135e

By varying the displayed columns and filtering, we can extract only the information we’re after.

6. Change Swap UUID Using swaplabel

Finally, we can also change the UUID of a swap partition via the swaplabel command from the util-linux:

$ swaplabel --uuid $(cat /proc/sys/kernel/random/uuid) /dev/sdx3

In this case, we supply the –uuid (-U) option to request a change. The option value is automatically generated by the kernel by reading the /proc/sys/kernel/random/uuid UUID generator file from the /proc pseudo-filesystem with cat. We insert it after the –uuid option via $() command substitution. Lastly, we indicate the /dev/sdx3 swap partition as the target of the new UUID. Naturally, we can also supply our own UUID as a string.

Further, we can also change the –label (-L), if desired.

7. Summary

In this article, we discussed ways to check the UUID of the swap partition.

In conclusion, there are many methods to find information about swap in the system, some of which are more flexible than others.