How to get disk UUID

Recently I needed to know UUID (universally unique identifier) of my bootable USB root partition as it is better way to distinguish storage devices because (external) device names can change depending on connection order.

There are at least couple of ways to get UUIDs and not all of them require root access.

$ ls -l /dev/disk/by-uuid/* 
lrwxrwxrwx 1 root root 10 maj  9 18:05 /dev/disk/by-uuid/5d2f85fe-b555-4504-a87f-3d1c6513c7d4 -> ../../sda2
lrwxrwxrwx 1 root root 10 maj  9 18:05 /dev/disk/by-uuid/678e905f-68cc-449e-99a9-cb90d7011d23 -> ../../sdb1
lrwxrwxrwx 1 root root 10 maj  9 18:05 /dev/disk/by-uuid/687aa5e4-3863-4529-a8bf-fa526716f523 -> ../../sdb2
lrwxrwxrwx 1 root root 10 maj  9 18:05 /dev/disk/by-uuid/8252dada-3e65-401b-b0cf-0123a7b62df6 -> ../../sda1
lrwxrwxrwx 1 root root 10 maj  9 18:05 /dev/disk/by-uuid/8b19014d-442a-4e2e-8367-569f000afaa0 -> ../../sda3
$ ls /dev/disk/by-uuid/* | xargs file
/dev/disk/by-uuid/5d2f85fe-b555-4504-a87f-3d1c6513c7d4: symbolic link to `../../sda2'
/dev/disk/by-uuid/678e905f-68cc-449e-99a9-cb90d7011d23: symbolic link to `../../sdb1'
/dev/disk/by-uuid/687aa5e4-3863-4529-a8bf-fa526716f523: symbolic link to `../../sdb2'
/dev/disk/by-uuid/8252dada-3e65-401b-b0cf-0123a7b62df6: symbolic link to `../../sda1'
/dev/disk/by-uuid/8b19014d-442a-4e2e-8367-569f000afaa0: symbolic link to `../../sda3'
$ find /dev/disk/by-uuid/* -exec echo -n {} "-> " \; -exec readlink {} \; | sort -k2 | sed 's/\.\.\///g'
/dev/disk/by-uuid/8252dada-3e65-401b-b0cf-0123a7b62df6 -> sda1
/dev/disk/by-uuid/5d2f85fe-b555-4504-a87f-3d1c6513c7d4 -> sda2
/dev/disk/by-uuid/8b19014d-442a-4e2e-8367-569f000afaa0 -> sda3
/dev/disk/by-uuid/678e905f-68cc-449e-99a9-cb90d7011d23 -> sdb1
/dev/disk/by-uuid/687aa5e4-3863-4529-a8bf-fa526716f523 -> sdb2
$ sudo blkid
/dev/loop0: TYPE="squashfs" 
/dev/sda1: UUID="8252dada-3e65-401b-b0cf-0123a7b62df6" TYPE="ext4" 
/dev/sda2: UUID="5d2f85fe-b555-4504-a87f-3d1c6513c7d4" TYPE="swap" 
/dev/sda3: UUID="8b19014d-442a-4e2e-8367-569f000afaa0" TYPE="ext4" 
/dev/sdb1: UUID="678e905f-68cc-449e-99a9-cb90d7011d23" TYPE="ext4" 
/dev/sdb2: LABEL="home-rw" UUID="687aa5e4-3863-4529-a8bf-fa526716f523" TYPE="ext4" 
$ sudo blkid -o list
device               fs_type   label       mount point       UUID
-------------------------------------------------------------------------------------------------
/dev/loop0           squashfs              (in use)
/dev/sda1            ext4                  (not mounted)     8252dada-3e65-401b-b0cf-0123a7b62df6
/dev/sda2            swap                  (not mounted)     5d2f85fe-b555-4504-a87f-3d1c6513c7d4
/dev/sda3            ext4                  /media/disk-1     8b19014d-442a-4e2e-8367-569f000afaa0
/dev/sdb1            ext4                  (not mounted)     678e905f-68cc-449e-99a9-cb90d7011d23
/dev/sdb2            ext4       home-rw    /home             687aa5e4-3863-4529-a8bf-fa526716f523
$ cat /dev/.blkid.tab                                                                                                 
<device DEVNO="0x0700" TIME="1336670951.551604" TYPE="squashfs">/dev/loop0</device>
<device DEVNO="0x0801" TIME="1336671278.523695" UUID="8252dada-3e65-401b-b0cf-0123a7b62df6" TYPE="ext4">/dev/sda1</device>
<device DEVNO="0x0802" TIME="1336670951.552155" UUID="5d2f85fe-b555-4504-a87f-3d1c6513c7d4" TYPE="swap">/dev/sda2</device>
<device DEVNO="0x0803" TIME="1336670951.552241" UUID="8b19014d-442a-4e2e-8367-569f000afaa0" TYPE="ext4">/dev/sda3</device>
<device DEVNO="0x0811" TIME="1336670951.568408" UUID="678e905f-68cc-449e-99a9-cb90d7011d23" TYPE="ext4">/dev/sdb1</device>
<device DEVNO="0x0812" TIME="1336670951.568610" LABEL="home-rw" UUID="687aa5e4-3863-4529-a8bf-fa526716f523" TYPE="ext4">/dev/sdb2</device>
$ cat /dev/.blkid.tab | sed 's/.*UUID="\(.*\)"\ .*>\(.*\)<.*/\1 -> \2/g' | grep -v ^\< | sort -k2
8252dada-3e65-401b-b0cf-0123a7b62df6 -> /dev/sda1
5d2f85fe-b555-4504-a87f-3d1c6513c7d4 -> /dev/sda2
8b19014d-442a-4e2e-8367-569f000afaa0 -> /dev/sda3
678e905f-68cc-449e-99a9-cb90d7011d23 -> /dev/sdb1
687aa5e4-3863-4529-a8bf-fa526716f523 -> /dev/sdb2
$ sudo tune2fs -l /dev/sda1 | grep UUID | tail -c 37
8252dada-3e65-401b-b0cf-0123a7b62df6

To change device UUID use tune2fs and uuidgen commands together:

$ sudo tune2fs -U `uuidgen` /dev/sda3 
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu