UUIDs and Linux: Everything you ever need to know [Update]

What is so special about UUIDs in Linux? I don’t know! But a single, simple short tip about looking up UUIDs in Linux from 2007 is one of the most successful posts I ever wrote! And is still looked up by hundreds each day! So I decided: Feed the masses.

Here is everything you ever need to know about UUIDs on Linux. The list is feature complete. Of course. *cough*

Background

UUIDs are 128 bit long numbers represented by 32 hexadecimal digits and which are used in software development to uniquely identify information with no further context. They are described in RFC 4122, an example UUID is:

13152fae-d25a-4d78-b318-74397eb08184

UUIDs are probably best known in Linux as identifier for block devices. The Windows world knows UUIDs in the form of Microsoft’s globally unique identifiers, GUID, which are used in Microsoft’s Component Object Model.

The UUIDs are generated in various variants: originally most of them were derived from the Computer’s MAC, later hash sums of names were used. And about the question, how many UUIDs there are and how big the chance is that you will generate a a number you already own, here are some numbers from Wikipedia’s UUID article:

After generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.

Usage in fstab

As mentioned UUIDs are most often used in Linux to identify block devices. Imagine, you have a couple of hard disks attached via USBs, than there is no persistent, reliable naming of the devices: sometimes the first USB hard disk is named “sda”, sometimes it is named “sdb”. So to uniquely address the right disk for example in your /etc/fstab, you have to add an entry like:

Linux implementation and generation

There is also the library libuuid which is used by uuidgen and especially by the ext2/3/4 tools E2fsprogs to generate UUIDs:

$ uuidgen
f81cc383-aa75-4714-aa8a-3ce39e8ad33c

How to get them, bash style

The most interesting part in UUIDs is most likely how to get the current UUIDs of the hard disks. As already mentioned years ago, there are two major ways to retrieve them: a simple ls call in a special directory, and the tool blkid.

So, first the ls call which has to be made in the directory /dev/disk/by-uuid. The directory contains links named after the UUIDs and pointing to the “real” block device files. Pretty handy if you are on a system where hardly anything is installed.

As you see hwinfo lists huge amounts of data about your hardware – among them are the UUIDs of the devices. Use it when you are grabbing for more data about the block devices anyway.

Or how about udevadm? It is the udev provided tool for querying data from the udev database. This database contains all the information udev has about the system, so the UUID info is just one among many, many other data. If you are writing a “modern” script which integrates with Linux standard tools nicely, I guess I would go with udev. But for pure, quick and dirty command line utilization, it produces a bit too many information, just like hwinfo.

In this context the tool udevinfo is also mentioned sometimes. However, that is deprecated, most distributions don’t ship it anymore. Also, another often mentioned way to retrieve the UUIDs is the program /lib/vol/vol_id. But as described in bug redhat#476379vol_id is only a private udev function. It should not be used by outside programs (or people) since the application interface is not stable. Also, the entire program might be removed in the future and in fact is already removed on some distributions.

How to get them, GUI style

If you are afraid of shells, there is of course a KDE-GUI tool available as well to look up the UUID: /usr/bin/kcmshell4 devinfo

Setting a UUID

As mentioned in the comment section, it can be also interesting to set a UUID. Since the UUID is part of the superblock the way to set it depends on the used file system. For ext file systems you can use tune2fs:

# tune2fs -U new_uuid /dev/sdaX

Anything else

If you have any other, further information, please post them in the comments! I will happily add them here. After all, my years old short tip already got me 250 k visits, I wonder how many a comprehensive list will bring me…

13 thoughts on “UUIDs and Linux: Everything you ever need to know [Update]”

Yes, you can translate it, as mentioned in the licence cc-by-sa (also shown on the left side). So: you are free to translate the post, but need to put it under the same licence, and you have to link back to the original. =)

The problem arises mainly when you are using system imaging. Lets suppose I want to put the same image on multiple machines. The UUID is generated during format. So if you format the drive in the new machine and then copy all the files over, the files now refer to a UUID that doesn’t exist. If I can copy the partition information over as well, then formatting isn’t necessary and the UUID will be the same. But complex disk setups, like with brtrfs or LVM tend to make things very difficult. There really should be a way to dictate a UUID when formatting.

I wrote “16 byte wide”, which is correct: the Wikipedia article and also the RFC both state the octet is 128 bit wide, and that is in fact 16 byte.
But I will correct the article to reuse the wording of the Wikipedia article and the RFC.

Blog Stats

Google Plus

License – CC-BY-SA

Beware however that this refers only to parts which are obviously written by me and do not have any other information about licensing. Quoted text, pictures and other content created by others is copyrighted by the corresponding authors. If you are in doubt, ask before republishing any content.