Friday, January 28, 2011

How do I find the UUID of a partition

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

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

    /sbin/tune2fs -l /dev/sda1
    
    Hamish Downer : This will work provided your filesystem is formatted as ext2, ext3 or ext4. Most filesystems are one of these but not all. It will also not work for swap partitions. See my answer for a universal way.
    From Eddie
  • 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
    

    Edit: 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.

    Eddie : This will work on any computer running a recent enough devfs.
  • 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 should work for any linux distro 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
    

    If other distributions have vol_id in other places then post a comment and I'll add it to this answer.

    Eddie : This doesn't work on my Fedora 10 laptop.
    Mihai Limbăşan : This is a much better solution than mine. Eddie, vol_id is located in /lib/udev. mish, could you edit your answer to prefix the full path in front of vol_id? /lib/udev isn't in root's path by default on any distribution I'm aware of.
    Eddie : "/lib/udev/vol_id /dev/sda2" appears to work. Few people will have /lib/udev in their path.
    Brad Gilbert : On my Ubuntu computer there is a symbolic link from `/sbin/vol_id` to `/lib/udev/vol_id`
    Brad Gilbert : Even though this is exactly what I asked for, `blkid` would have been more useful, when I went to edit `/etc/fstab`.
  • 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
    
    From Rich Adams
  • Another command that might be available and also works quite well for this is 'blkid'. It's part of the e2fsprogs package. Examples of it's 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 easier to read format:

    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
    
    Brad Gilbert : On my Ubuntu computer, I don't need to use sudo.
    Brad Gilbert : Just typing `blkid`, got me exactly what I wanted, but not quite what I asked for. ( I'm accepting it anyway, because I am sure I will use it often )

0 comments:

Post a Comment