Skip to content

introduction to uuid's

(Written by Paul Cobbaut, https://github.com/paulcobbaut/, with contributions by: Alex M. Schapelle, https://github.com/zero-pytagoras/)

A uuid or universally unique identifier is used to uniquely identify objects. This 128bit standard allows anyone to create a unique uuid. That is, the number of uuid's that can be generated is so large that the probability of generating a duplicate is extremely low.

This chapter takes a brief look at uuid's.

lsblk -f

You can quickly locate the uuid of file systems with lsblk -f. The following example is from a VM running Ubuntu 24.05.

student@ubuntu:~$ lsblk -f
NAME     FSTYPE  FSVER   LABEL UUID                                FSAVAIL FSUSE% MOUNTPOINTS
sda
├─sda1
├─sda2   ext4    1.0           0e751ccc-2139-4c7a-a90e-e41e9a522aee   1.7G     5% /boot
└─sda3   LVM2_me LVM2 001      d2OsZK-N5Ih-NCA3-TOIv-h9Ul-wXzA-UoQKtz
  └─ubuntu--vg-ubuntu--lv
         ext4    1.0           40b3fedd-d848-4a74-b7ef-f3acac9554ed  25.5G    11% /

The same command will also work on recent EL systems, e.g. AlmaLinux 9:

[student@el ~]$ lsblk -f
NAME   FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda                                                                           
├─sda1 swap   1           a4814ebe-c0b2-4819-8129-30f32b3e8772                [SWAP]
└─sda2 xfs                303db791-9236-4ac4-a176-e2a033576d89   60.4G     2% /

tune2fs

Use tune2fs to find the uuid of a file system.

student@ubuntu:~$ sudo tune2fs -l /dev/sda2 | grep UUID
Filesystem UUID:          0e751ccc-2139-4c7a-a90e-e41e9a522aee

uuid

There is more information in the manual of uuid(1), a tool that can generate uuid's.

[student@el ~]$ sudo dnf install uuid
[student@el ~]$ uuid
c4212384-75ca-11ef-829a-080027c76768
[student@el ~]$ man uuid

(On Debian/Ubuntu/Mint, use sudo apt install uuid.)

uuid in /etc/fstab

You can use the uuid in /etc/fstab to make sure that a volume is universally uniquely identified. The device name can change depending on the disk devices that are present at boot time, but a uuid never changes.

First we use tune2fs to find the uuid.

[root@linux ~]# tune2fs -l /dev/sdc1 | grep UUID
Filesystem UUID:          7626d73a-2bb6-4937-90ca-e451025d64e8

Then we check that it is properly added to /etc/fstab, the uuid replaces the variable devicename /dev/sdc1.

[root@linux ~]# grep UUID /etc/fstab 
UUID=7626d73a-2bb6-4937-90ca-e451025d64e8 /home/pro42 ext3 defaults 0 0

Now we can mount the volume using the mount point defined in /etc/fstab.

[root@linux ~]# mount /home/pro42
[root@linux ~]# df -h | grep 42
/dev/sdc1             397M   11M  366M   3% /home/pro42

The real test now, is to remove /dev/sdb from the system, reboot the machine and see what happens. After the reboot, the disk previously known as /dev/sdc is now /dev/sdb.

[root@linux ~]# tune2fs -l /dev/sdb1 | grep UUID
Filesystem UUID:          7626d73a-2bb6-4937-90ca-e451025d64e8

And thanks to the uuid in /etc/fstab, the mountpoint is mounted on the same disk as before.

[root@linux ~]# df -h | grep sdb
/dev/sdb1             397M   11M  366M   3% /home/pro42

uuid as a boot device

Recent Linux distributions (Debian, Ubuntu, ...) use grub with a uuid to identify the root file system.

This example shows how a root=/dev/sda1 is replaced with a uuid.

title           Ubuntu 9.10, kernel 2.6.31-19-generic
uuid            f001ba5d-9077-422a-9634-8d23d57e782a
kernel          /boot/vmlinuz-2.6.31-19-generic \
root=UUID=f001ba5d-9077-422a-9634-8d23d57e782a ro quiet splash 
initrd          /boot/initrd.img-2.6.31-19-generic

The screenshot above contains only four lines. The line starting with root= is the continuation of the kernel line.

RHEL and derived distributions boot from LVM after a default install.

practice: uuid and filesystems

  1. Find the uuid of one of your Linux system's partitions with tune2fs.

  2. Use this uuid in /etc/fstab and test that it works with a simple mount.

  3. (optional) Test it also by removing a disk (so the device name is changed). You can edit settings in vmware/Virtualbox to remove a hard disk.

  4. Display the root= directive in /boot/grub/menu.lst.

  5. (optional on ubuntu) Replace the /dev/xxx in /boot/grub/menu.lst with a uuid (use an extra stanza for this). Test that it works.

solution: uuid and filesystems

  1. Find the uuid of one of your Linux system's partitions with tune2fs.

    student@ubuntu:~$ sudo tune2fs -l /dev/sda2 | grep UUID
    Filesystem UUID:          0e751ccc-2139-4c7a-a90e-e41e9a522aee
    
  2. Use this uuid in /etc/fstab and test that it works with a simple mount.

    $ tail -1 /etc/fstab
    UUID=60926898-2c78-49b4-a71d-c1d6310c87cc /home/pro42 ext3 defaults 0 0
    
  3. (optional) Test it also by removing a disk (so the device name is changed). You can edit settings in vmware/Virtualbox to remove a hard disk.

  4. Display the root= directive in /boot/grub/menu.lst.

    student@ubuntu:~$ grep root= /boot/grub/menu.lst
    kernel          /boot/vmlinuz-2.6.26-2-686 root=/dev/hda1 ro selinux=1 quiet
    kernel          /boot/vmlinuz-2.6.26-2-686 root=/dev/hda1 ro selinux=1 single
    
  5. (optional on ubuntu) Replace the /dev/xxx in /boot/grub/menu.lst with a uuid (use an extra stanza for this). Test that it works.