160

I'm running Ubuntu, and want to find out the UUID of a particular filesystem (not partition). I know I can use e2label /dev/sda1 to find out the file system label, but there doesn't seem to be a similar way to find the UUID.

Brad Gilbert
  • 2,633

13 Answers13

201

Another command that might be available and also works quite well for this is 'blkid'. It's part of the e2fsprogs package. Examples of its usage:

Look up data on /dev/sda1:

topher@crucible:~$ sudo blkid /dev/sda1
/dev/sda1: UUID="727cac18-044b-4504-87f1-a5aefa774bda" TYPE="ext3"

Show UUID data for all partitions:

topher@crucible:~$ sudo blkid

/dev/sda1: UUID="727cac18-044b-4504-87f1-a5aefa774bda" TYPE="ext3" /dev/sdb: UUID="467c4aa9-963d-4467-8cd0-d58caaacaff4" TYPE="ext3"

Show UUID data for all partitions in an easier-to-read format: (Note: in newer releases, blkid -L has a different meaning, and blkid -o list should be used instead)

topher@crucible:~$ sudo blkid -L

device fs_type label mount point UUID

/dev/sda1 ext3 / 727cac18-044b-4504-87f1-a5aefa774bda /dev/sdc ext3 /home 467c4aa9-963d-4467-8cd0-d58caaacaff4

Show just the UUID for /dev/sda1 and nothing else:

topher@crucible:~$ sudo blkid -s UUID -o value /dev/sda1

727cac18-044b-4504-87f1-a5aefa774bda

12

For GPT Partitioned Disks Only

On a GPT formatted disk each partition is assigned a GUID, which is a form of UUID, though probably not what the original poster was referring to. Therefore this answer is probably less helpful to the original questioner. Nevertheless I believe there's an important distinction to be noticed.

To get the GUID of partition 1 on GPT formatted disk /dev/sda, as well as its partition label and so on:

sudo sgdisk -i 1 /dev/sda

or all with:

ls -l /dev/disk/by-partuuid

To boot with the root of the file system being on a certain partition you would use the Linux kernel parameter syntax of:

root=PARTUUID=87654321-4321-4321-abcd-123456789012

In this case, you can specify just the beginning of the UUID—enough to be unique. This parameter is more primitive and can be understood by the kernel earlier in its boot process.


There's a difference in semantics between these:

A disk holds partitions, a partition holds a file system, a file system holds directories and files. For some setups and operating systems there are more layers.

The GUID UUID and associated label refer to a partition, but not the partition's contents. A new partition on the same disk, or a partition on a new disk will have a new GUID UUID. The same partition could hold one file system one day and another on a different day. It only exists for GPT-formatted disks, but not for legacy partitioned disks. There usually aren't anymore utility here than specifying root=/dev/sda1 or root=8:1.

The other current answers refer to the UUID of a file system in some containing partition. If the file system is copied, as a whole, to another partition or hard disk that value remains the same. This UUID is useful in finding a moved file system. Therefore this is probably more pertinent to most people. Linux kernel parameter root=UUID=87654321-4321-4321-a567-123456789012 refers to this.

I believe root=LABEL= and root=UUID= are implemented by early userspace, the init code I saw the other day on my system translated these parameters to /dev/disk/by-uuid and /dev/disk/by-label (links I believe are created by udev in userspace on my system).

[1] devt_from_devname()

10

The script-clean way to do this which works on any type of filesystem is:

lsblk -no UUID <device-containing-FS>

Or, given the mountpoint (or any file within it):

lsblk -no UUID $(df -P <file> | awk 'END{print $1}')

The output is the UUID, the whole UUID, and nothing but the UUID.

Tom Hale
  • 1,242
6

The easiest way to do this for ext2/ext3/ext4 is:

/sbin/tune2fs -l /dev/sda1
Eddie
  • 11,524
3

The recommended way to do this is to do

sudo vol_id -u /dev/sda2

For more on using UUIDs, see this article (from Ubuntu help, but it should work for any Linux distribution using UUIDs).

As noted in comments to this question, vol_id may not be in your path. On Ubuntu, it is in /sbin, so the above will work.

For Fedora, it appears to need:

sudo /lib/udev/vol_id -u /dev/sda2
3

This seems to work for me:

sudo dumpe2fs /dev/sda1 | grep UUID
2

The simplest and best way to find the UUID of a filesystem:

blkid /dev/sda1
asmath
  • 399
2

Assuming you want the UUID for sda1, you could try something like this:

for v in /dev/disk/by-uuid/* ; do echo "`readlink $v`: $v" | grep ../sda1 | cut -d\: -f2 | cut -d/ -f5 ; done

Adjust sda1 accordingly. To get the UUIDs for all partitions, drop the 'greps' and 'cuts', a la:

for v in /dev/disk/by-uuid/* ; do echo "`readlink $v`: $v" ; done

Sample output for sda1 on my desktop:

[mihailim@home ~]$ for v in /dev/disk/by-uuid/* ; do echo "`readlink $v`: $v" | grep ../sdb3 | cut -d\: -f2 | cut -d/ -f5 ; done

dc8c49f1-e2dc-46bc-ba02-013f26c85f70

Please note that this solution, while more contrived than the udev->vol_id one, does not require root privileges, will work on any post-2005 or so kernel, and relies on tools present in any Linux distribution which are by default in the path for any user.

1

You can use the following to get the UUID for a particular drive,

sudo vol_id -u /dev/sda1
1

You can also use this to print all the UUIDs:

for disk in /dev/disk/by-uuid/*; do 
    basename "$(readlink "$disk")"
    basename "$disk"
    echo
done

or this arguably simpler command, replacing sda1 with the device you'd like to search for:

disk=sda1
find /dev/disk/by-uuid -type l -exec sh -c "readlink {} | grep -o $disk && basename {}" \;

an adaptation of the second method to print all UUIDs:

find /dev/disk/by-uuid -type l -exec sh -c 'basename $(readlink {}); basename {}; echo' \;
kiri
  • 111
1
ls -l /dev/disk/by-uuid | grep `lsblk | grep "/" | awk '{print $1}'` | awk '{print $9}'

The above seems to work on most (all I have found) Linux systems over many years. It may have flaws, I don't know. I would prefer to get the serial number but ... this is the UUID of the root filesystem.

If anyone has a way to get the serial number without needing to be root (as mine is) and not installing "unusual" packages that are different in different versions of Unix I would appreciate it - always can learn something. And I am aware I am mixing things - the is the root file system UUID, not a disk.

The purpose, BTW, is to generate a unique number per machine that cannot be modified (like a disk serial number and like MAC addresses once were long ago).

It is used for encoding of software to a single machine. MAC address was fine until they allowed them to be virtual ... some sleazy customers have simply set their MAC address to a constant (on different networks of course) and avoided paying me.

In AIX there is a single call to get one number that identifies the machine. It does not care if the hardware changes or software updates occur so I have no idea how they do it ... If the motherboard changes, then the number changes, so I think they hide it there. And that is beyond rare.

0
sudo grep swap /etc/fstab
## Output like:
# swap was on /dev/sda6 during installation
# UUID=34e3f31b-16ec-4c84-8f4d-339f38d04a3b none swap sw 0 0

sudo lsblk -f -l| grep SWAP
## Output like:
sda6 swap 34e3f31b-16ec-4c84-8f4d-339f38d04a3b [SWAP]
0

You can use the following to get the UUID for a particular drive,

sudo vol_id -u /dev/sda1

or you can use this to list all UUIDs for the attached media,

ls /dev/disk/by-uuid