Subscribe to RSS

How to get the UUID Of Devices in Ubuntu Linux

Back to MainPage. Back to LinuxStuff.

Quick UUID Intro

The Universally Unique IDentifier UUID of a device can be used to pinpoint devices, regardless of their mount-points.  You'll see the UUID referenced in the /etc/fstab file of most modern Linux distrubutions.  For example, part of my fstab looks like:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
# /dev/sda4
UUID=f1b8cc54-4904-44ed-87f8-140f21f18900 /               ext3    relatime,errors=remount-ro 0       1
# /dev/sdb1
UUID=16075a89-f8b5-4141-be61-51f4e9d2db91 /media/sdb1     ext3    relatime        0       2

Note that the first entry on the line isn't the traditional /dev descriptor, but rather it's the UUID of the partition.  This should mean that I can re-order my devices, or use the UUID to identify and specify distinct mount points for hotpluggable and removable media (flash-drives, SD cards, removable hard disks, etc).

Finding a UUID

To do that, I need to find the UUID of my devices.  There are a number of ways, depending on your distribution.  Try all of the following.

The /dev/disk/by-uuid directory

This directory lists known devices by their UUID, and links back to their path in the traditional device tree.  Use ls -l to be shown the target of each link, as well as the UUID:

$ ls -l /dev/disk/by-uuid 
total 0
lrwxrwxrwx 1 root root 10 2009-09-05 16:43 f1b8cc54-4904-44ed-87f8-140f21f18900 -> ../../sda4
lrwxrwxrwx 1 root root 10 2009-09-05 10:47 0915-5385 -> ../../sdd1

Note that sda4's UUID correlates to the entry in fstab.

Adding to /etc/fstab

sdd1 here is a removable drive, one that I want to assign to the same mountpoint, whenever I mount it.  I'll add an entry to /etc/fstab for this SD card, as:

# removable SD card
UUID=0915-5385 /media/card_4g_a vfat user,noauto,rw 0 0

blkid

Method #2: using blkid

$ blkid /dev/sdd1
/dev/sdd1: UUID="0915-5385" TYPE="vfat"

vol_id

Method #3: using vol_id

$ vol_id /dev/sdd1
ID_FS_USAGE=filesystem
ID_FS_TYPE=vfat
ID_FS_VERSION=FAT32
ID_FS_UUID=0915-5385
ID_FS_UUID_ENC=0915-5385
ID_FS_LABEL=
ID_FS_LABEL_ENC=
ID_FS_LABEL_SAFE=

or

$ vol_id /dev/sdd1 |grep UUID=
ID_FS_UUID=0915-5385

Changing a UUID

Perhaps you've got a filesystem or a device that you cloned from another.  In this case, the UUIDs may be the same, which violates the second U (uniqueness) of UUID.  You need to change one.  Here's how:

$ uuidgen
$ tune2fs /dev/sdd1 -U output_from_previous_command 

Summary

Use a UUID in place of a /dev path to treat a drive consistently, despite it moving around in the /dev tree.  Useful for removable media, hotpluggable devices and reconfiguring your servers.

References:

  1. http://liquidat.wordpress.com/2007/10/15/short-tip-get-uuid-of-hard-disks/
  2. http://en.wikipedia.org/wiki/Universally_Unique_Identifier
  3. http://linux.byexamples.com/archives/321/fstab-with-uuid/

Back to MainPage. Back to LinuxStuff.

Edited on Sat, Sep 5, 2009 at 4:37 a.m.