Linux: Get UUID from Disk

Objective: Map a UUID (universally unique identifier ) to a device and vice versa on Linux.

A UUID is a practically unique (rather than guaranteed unique) 16-octet (or 128-bit) value. On Linux, the UUID may be used by mount, fsck, tune2fs, /etc/fstab (and possibly others) by specifying UUID=uuid instead of a block special device name like /dev/sda1.

There are a few methods to determine the UUID used for a disk or partition. The first is to list the files in /dev/disk/by-uuid/ directory.

$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Feb 21 02:45 247e0716-b646-4d49-a8d4-124b412b988d -> ../../dm-0
lrwxrwxrwx 1 root root 10 Feb 21 02:45 4bd882ad-ac4c-49ef-806c-285695c09381 -> ../../sda1
lrwxrwxrwx 1 root root 10 Feb 21 02:45 af39e348-b1a5-4653-880a-b8bf5b13f552 -> ../../dm-2

But, by using the above method, I could not find the UUID of volumes that are managed by LVM (logical volume manager).

The next method is to use the tune2fs for disks on ext2/ext3/ext4 filesystem. To get the UUID of /dev/sda1, use the following syntax.

# tune2fs -l /dev/sda1 | grep UUID
Filesystem UUID:          4bd882ad-ac4c-49ef-806c-285695c09381

The third method is to use the blkid command can also be used to get the UUID of a disk or partition. To get the UUID of a specific device, use the following syntax.

$ blkid /dev/sda1
/dev/sda1: UUID="4bd882ad-ac4c-49ef-806c-285695c09381" TYPE="ext4"

If you have the UUID and want to map it to a device, run blkid using the following syntax.

$ blkid -t UUID=4bd882ad-ac4c-49ef-806c-285695c09381
/dev/sda1: UUID="4bd882ad-ac4c-49ef-806c-285695c09381" TYPE="ext4"

To probe all known filesystems and devices, run blkid without any arguments and filter by UUID. Use “blkid -k” to print the supported list of filesystems.

$ blkid | grep UUID
/dev/sda1: UUID="4bd882ad-ac4c-49ef-806c-285695c09381" TYPE="ext4"
/dev/sda5: UUID="Ji1Dl0-fg7N-IvxH-uUEe-e9SD-qKF4-q6F1BJ" TYPE="LVM2_member"
/dev/sdc1: UUID="jnAj32-QHfF-SY2Q-Mnc2-k0xe-1Iiz-0Kdrhw" TYPE="LVM2_member"
/dev/sdb1: UUID="gCinnI-YQsM-CC38-ggMf-9dWP-ye0H-KzuwwS" TYPE="LVM2_member"
/dev/mapper/vg03-home: UUID="247e0716-b646-4d49-a8d4-124b412b988d" TYPE="ext4"
/dev/mapper/vg01-root: UUID="af39e348-b1a5-4653-880a-b8bf5b13f552" TYPE="ext4"

You can see from the above output that blkid also handle devices that are part of LVM.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }