1. Introduction
A universal unique identifier (UUID) is a generated semi-random sequence assigned to one or another hardware or virtual component. Although it can be used for different components like network interface cards, one of the most common applications and use cases for a UUID is for a storage medium such as a hard disk drive (HDD) and solid-state disk (SSD), partition, or even a USB drive.
In this tutorial, we explore ways to change the UUID of a storage medium. First, we go over the basic concept of a UUID. After that, we check ways to change the UUID of a partition. Finally, we note an important consideration when it comes to modifying live mounts.
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. Storage UUID
For every physical storage medium, Linux systems generate a new UUID:
$ blkid
/dev/loop1: TYPE="squashfs"
/dev/sdb1: UUID="99906661-b7fb-402e-deeb-25ebec66610a" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="2606100d-01"
/dev/loop4: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/loop0: TYPE="squashfs"
/dev/sda7: UUID="a19b6660-1d06-44fc-d0d0-0a9b5ab2bbd8" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="1666010f-07"
/dev/sda5: UUID="1f8c457e-1f01-49d8-bead-a9166610b12f" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="1666010f-05"
/dev/sda1: UUID="606660e8-7a07-4fc2-dead-27ed8425e7a0" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="1666010f-01"
/dev/sda8: UUID="100667f7-ed57-4af7-deaf-123034ce8dd8" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="1666010f-08"
/dev/sda6: UUID="5ebcaaf0-e098-43b9-beef-1f8d8251135e" TYPE="swap" PARTUUID="1666010f-06"
/dev/loop3: TYPE="squashfs"
In this case, blkid shows us each separate partition has a distinct identifier. This way, the system can locate and reference each storage component separately.
Another way to acquire the same information is by listing the /dev/disk/by-uuid path:
$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Sep 11 09:11 606660e8-7a07-4fc2-dead-27ed8425e7a0 -> ../../sda1
lrwxrwxrwx 1 root root 10 Sep 11 09:11 5ebcaaf0-e098-43b9-beef-1f8d8251135e -> ../../sda6
lrwxrwxrwx 1 root root 10 Sep 11 09:11 100667f7-ed57-4af7-deaf-123034ce8dd8 -> ../../sda8
lrwxrwxrwx 1 root root 10 Sep 11 09:11 99906661-b7fb-402e-deeb-25ebec66610a -> ../../sdb1
lrwxrwxrwx 1 root root 10 Sep 11 09:11 1f8c457e-1f01-49d8-bead-a9166610b12f -> ../../sda5
lrwxrwxrwx 1 root root 10 Sep 11 09:11 a19b6660-1d06-44fc-d0d0-0a9b5ab2bbd8 -> ../../sda7
Notably, loop devices are exempt from the requirement to have a UUID. Still, even swap and other special partitions employ this identification method.
Because of its prevalence, knowing how to generate and change the UUID can be important for storage management.
3. Change Storage Partition UUID
The way to change a UUID mostly depends on the tooling. On the other hand, the toolset we employ is usually linked with the filesystem type.
For each tool, we employ apt as our package manager.
In cases when the application output doesn’t tell us much, we can confirm our results by using blkid or the /dev/disk/by-uuid path.
3.1. ext* Filesystems
To manipulate ext* filesystems, we use the e2fsprogs package:
$ apt-get install e2fsprogs
By using this toolkit, we can modify different aspects of a partition. In particular, to change the UUID of an ext* partition, we leverage the tune2fs utility:
$ tune2fs -U $(cat /proc/sys/kernel/random/uuid) /dev/sdx3
tune2fs 1.47.0 (5-Feb-2023)
First, we supply the -U switch to request the change. The actual value comes from a $() command substitution construct that gets replaced with the contents of the /proc/sys/kernel/random/uuid UUID generator file from the /proc pseudo-filesystem via cat. Finally, we specify the /dev/sdx3 partition to apply the new UUID. Of course, we can supply our own UUID directly.
Alternatively, we can use the -u switch without any value to leave the UUID generation to tune2fs:
$ tune2fs -u /dev/sdb3
In fact, this command is more or less equivalent to the previous one.
3.2. XFS Filesystems
The XFS filesystem is also part of the Linux kernel. As such, there’s wide support for it, which includes tooling.
To begin with, we install the xfsprogs package with XFS tools:
$ apt-get install xfsprogs
Now, we can use a utility similar to tune2fs:
$ xfs_admin -U $(cat /proc/sys/kernel/random/uuid) /dev/sdx5
Clearing log and setting UUID
writing all SBs
new UUID = 11106661-b55b-402e-deeb-25ebec64efec
In this case, we employ xfs_admin with its -U option to set the UUID of the /dev/sdx5 XFS partition to a specific value.
Again, we supply it with a UUID and append the partition path as an argument to the command.
3.3. B-Tree Better Filesystem (btrfs)
The Better Filesystem or btrfs also includes a utility package which we can install:
$ apt-get install btrfs-progs
Notably, this installation also integrates the support for the actual filesystem.
Now, we can change the UUID of btrfs partitions:
$ btrfstune -U $(cat /proc/sys/kernel/random/uuid) /dev/sdx7
WARNING: it's recommended to run 'btrfs check --readonly' before this operation.
The whole operation must finish before the filesystem can be mounted again.
If cancelled or interrupted, run 'btrfstune -u' to restart.
We are going to change UUID, are your sure? [y/N]: y
Current fsid: 8bcc28d5-696d-4958-b40d-3a35119fc337
New fsid: ebaaa666-bad6-f0d0-b0d0-affe6660fbbe
Set superblock flag CHANGING_FSID
Change fsid in extent tree
Change fsid in chunk tree
Clear superblock flag CHANGING_FSID
Fsid change finished
In particular, we employ the btrfstune utility with its -U option and a specific UUID to set it as the new value of /dev/sdx7.
In tune with tune2fs, we can also just use the -u flag, which leaves the new UUID generation to btrfstune:
$ btrfstune -u /dev/sdx7
After getting the same prompt, we acquire a new UUID for the /dev/sdx7 partition.
3.4. Other Filesystems
Indeed, most modern filesystems come with a toolkit for attribute modification, including ways to change the UUID.
In fact, the ZFS filesystem even provides a whole vdev_id.conf file that handles naming and UUID.
4. Considerations
Depending on the Linux distribution or kernel, we might not be able to change the UUID of a mounted partition. While this wasn’t an obstacle in previous Linux versions, most major distributions have included it as a precaution against problematic mounts, especially when it comes to automation like fstab.
Some tools support a way to override this behavior. For example, tune2fs provides the -O switch as a method to toggle features:
$ tune2fs -O ^uninit_bg /dev/sdx3
In this case, we use ^ to disable the uninit_bg, responsible for preventing mounted ext* filesystem changes.
5. Summary
In this article, we explored the definition and function of a storage UUID and how to change that for a given partition.
In conclusion, the way we change a UUID depends on the component and, when it comes to storage, filesystem type.