262

My /etc/fstab contains this:

# / was on /dev/sda1 during installation
UUID=77d8da74-a690-481a-86d5-9beab5a8e842 /               ext4    errors=remount-ro 0       1

There are several other disks on this system, and not all disks are being mounted to the correct location (For example, /dev/sda1 and /dev/sdb1 are sometimes reversed).

How can I see the UUIDs for all disks on my system? Can I see the UUID for the third disk on this system?

Stefan Lasiewski
  • 19,754
  • 24
  • 70
  • 85

12 Answers12

269

There's a tool called blkid (use it as root or with sudo),

# blkid /dev/sda1
/dev/sda1: LABEL="/" UUID="ee7cf0a0-1922-401b-a1ae-6ec9261484c0" SEC_TYPE="ext2" TYPE="ext3"

you can check this link for more info

setzamora
  • 3,039
  • 1
    I like blkid, especially when LVM2. – Grzegorz Wierzowiecki Sep 18 '11 at 21:23
  • 20
    Just a minor comment: looks like being a member of group disk is sufficient to run blkid; no need for full superuser privileges. – arielf Dec 14 '13 at 23:19
  • 21
    If you want only the UUID (like for parsing in a script), you can do blkid /dev/sda1 -s UUID -o value. – Jack O'Connor Jun 20 '16 at 21:35
  • 4
    Quick comment here : in my distro (Debian 8) this yields UUID as well as "PARTUUID", which is rather confusing. I used {lsblk} commands which gives only one value. – Toluene Sep 12 '16 at 11:57
  • 2
    This one saves a great deal of time, though I prefer to do blkid /dev/sd* to list all drives.. The info that spits out is generally more than enough to find the drive you need. :) –  Jul 01 '17 at 08:04
  • 3
    PARTUUID for GPT-partitioned disks is the GPT UUID for the partition, not for the filesystem in it. Might be useful when a filesystem is not created yet. On a MBR-partitioned disk, PARTUUID is not a real UUID, but simply a Windows Disk Signature from the MBR + a dash + partition number. – telcoM Jan 28 '18 at 01:32
  • @arielf Apparently I'm not in this "disk" group and had to run sudo to get it to work! So users may need superuser privs (LUbuntu 18.04) – Michael Apr 23 '19 at 01:46
  • @Michael if you can sudo & are not further restricted, you may add your username to any group: edit /etc/group, by (e.g. if you use vi) sudo vipw -g. This approach is generally better than always running as root. You will need to re-login to be included in recently added groups (use the id command to see which groups you're in) – arielf Apr 23 '19 at 17:08
  • @takumar The PARTUUID is often more useful. Besides the reasons mentioned by telcoM above, you will also need it when you migrate the filesystem from one partition to another because they will get the same filesystem UUID. PARTUUID will always be there and never collide on one GPT. – Franklin Yu Sep 03 '19 at 19:29
  • this is to list partitions not physical disks. – None Dec 21 '22 at 09:08
235

In /dev/disk/by-uuid there are symlinks mapping each drive's UUID to its entry in /dev (e.g. /dev/sda1)

You can view these with the command ls -lha /dev/disk/by-uuid

Brian Z
  • 583
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • 4
    It's not readable when LVM partitions. – Grzegorz Wierzowiecki Sep 18 '11 at 21:22
  • 1
    LVM already uses long UUID-like identifiers (although presented differently) in its structure. I think the only reason for using filesystem UUIDs with LVM would be as an unified interface for some sort of automation, as LVM already does the mapping of LVs to human-friendly names for you. – telcoM Jan 28 '18 at 01:27
  • Just for reference, the result has changed and will be different from blkid and won't work for booting: https://i.imgur.com/ocgoi3g.png – Torxed Jul 08 '20 at 12:38
  • I often use this approach, especially becuase blkid needs SU privileges. But /dev/disk/by-uuid unfortunately has much less information and it will get you into trouble for some cases. For me this was working with RAID and btrfs volumes as different partitions share a uuid, but there is only one entry for each UUID in the symlinks... – Hotsndot Feb 02 '21 at 12:44
  • This does not work on all linux – James Stevens Dec 21 '21 at 15:29
103

The best command to use is lsblk -f. It will list all the devices and partitions, how they are mounted (if at all) and the tree structure of the devices in the case of using LVM, crypto_LUKS, or multiple volume groups on the same drive.

John Rea
  • 1,031
27

To only get the UUID of a specific disk device (for example to be used in a script) you can use:

sudo blkid -s UUID -o value /dev/sdXY

where /dev/sdXY is the name of the device.

17
lsblk -o +uuid,name

You can see all the outputs that can be added to the -o (--output) with

lsblk --help

Also this will do the job

# blkid
16

This works for me:

ls -la /dev/disk/by-uuid

If you want to check what type the partition is, use:

df -Th

and it will show you if you have ext3 or ext2. Today it helped me because there was a formatted ext2 partition and I thought it was ext3, which was causing the mount to fail.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
MIrra
  • 1,257
4

The previous answers do not work for multiple devices or for devices with identical UUIDs.

Try this:

sudo blkid /dev/sd*
Kevin
  • 41
  • A universally unique identifier (UUID) should always be unique. The entire purpose of a UUID is to be a unique, universally. If not, there's a problem. I have seen duplicated UUIDs in cloned VMs, at least for network devices. – Stefan Lasiewski Sep 14 '16 at 17:00
  • 7
    If you clone a partition with the dd command the copy will have the same uuid and yes, that is a problem. The other answers here wouldn't show that. – Kevin Sep 18 '16 at 09:22
1

With the following command line you can see UUID plus the mapping to partitions.

ls /dev/disk/by-uuid -lt

lrwxrwxrwx 1 root root 10 Sep  1 18:51 57eacf4e-1940-436e-b945-85f8d4833aa5 -> ../../sda2
lrwxrwxrwx 1 root root 10 Sep  1 18:51 656f4cae-8527-43a0-a80f-00ac82818744 -> ../../sda1
lrwxrwxrwx 1 root root  9 Sep  1 18:51 d627595d-4060-440e-8380-a1fe9f3f2a81 -> ../../md0
lrwxrwxrwx 1 root root 10 Sep  1 18:51 0dfd6dfe-1852-460d-852c-676a5b9035ed -> ../../sda4
lrwxrwxrwx 1 root root 10 Sep  1 18:51 b1ddf850-8f81-429f-a653-38ae4a4ebb6f -> ../../sda3
lrwxrwxrwx 1 root root  9 Sep  1 18:51 b4b729f7-5699-411c-8f5a-424bbc7c89fc -> ../../sdb
1

I have the same problem as you: renaming by kernel of /dev/sd** after a reboot:

Of course all my automatic mounting in /etc/fstab are referenced by LABEL or by UUID, so basically there is no problem for that. And all the commands above ,blkid or lsblk, give this kind of information.

But the trouble begins as in my case, when you are using partition in RAW mode, in the currently booted system point-of-view: for example either: the partition is used as raw device, to make a virtual disk for VirtualBox (so the reference to this partition is something like: /dev/sdf3) or the partition is used as raw device, to make a LUN for iSCSI (so the reference to this partition is something like: /dev/sdc6)

So now at boot , for example in rc.local, you have to find in a reliable manner, what is the /dev/sdXX device of your dedicated RAW partition, and adapt some file:

EXAMPLE 1

The VirtualBox disk *.vmk description of this raw disk, in the part something like:

\# Extent description
RW 488397167 FLAT "/dev/sdXX" 0

and then restart the VirtualBox service

EXAMPLE 2

in tgtd configuration, a target :target0 was associated to /dev/sdd6 at build time. After reboot you get the same partition renamed /deb/sdc6 This happens with a removable disk, USB or eSATA! So how to find the new device automatically ? Again in /etc/rc.d/rc.local

So in this case we need a reliable manner to find what is the new device name. GPT partition offers unique GUID for any GPT partition, written in GPT table.

gdisk does not provide this info with listing mode, but only in interactive mode with: i command. Fortunately, blkid does it!

So you need to write a shell script, to look in all your disks, which is the device /dev/sdXX, associated to the GUID noticed at partition creation time.

Something like, search_device_by_partUUID.sh:

\#!/bin/bash

PART_UUID=$1
if [ "$PART_UUID" = "" ]
then
    echo "Syntax: $0 <a valid partition UUID>"
    exit 3
fi
lsblk | grep '^sd' | awk '{print $1}' | while read DISK_DEVICE
do 
    INFO=`blkid /dev/${DISK_DEVICE}* | grep "PARTUUID=\"$PART_UUID\"" `
    if [ "$INFO" != "" ]
    then
        echo INFO : "$INFO"
        BLK_DEVICE=`echo "$INFO" | awk '{print $1}'`
        echo $BLK_DEVICE > /dev/shm/blkdevice
        echo -n "BLK_DEVICE : " ; cat /dev/shm/blkdevice
    fi
done

and then use /dev/shm/blkdevice, in your rc.local script.

karel
  • 2,030
0

You need to check /dev/disk/by-partuuid in these cases. there are symlinks mapping each drive's PARTUUID to its entry in /dev/sdb1 etc

0

https://serverfault.com/a/1116758/969454

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]

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Nov 28 '22 at 08:13
0

To see the uuid of a hard disk partition I just boot the system up with a Linux CD and goto my computer mount, click on, the partition I want to see. The uuid number of the Linux partition will be displayed.

You can also see disk uuid by running Linux Disk utility after the Linux CD boot up.

Anthon
  • 79,293
  • What's "my computer mount"? And what's "Linux Disk utility", sounds like gnome-disk-utility aka Disks? – Xen2050 Nov 30 '18 at 04:15