How To Get Uuid In Linux : Retrieving UUID With Blkid Command

Your system’s UUID is a permanent hardware identifier that you can retrieve with the `blkid` command. If you are wondering how to get uuid in linux, this guide will walk you through multiple methods. A UUID, or Universally Unique Identifier, is a 128-bit label used to uniquely identify devices, partitions, or filesystems in Linux. It remains constant even if you change the device name or connection port.

How To Get Uuid In Linux

Understanding UUIDs In Linux

A UUID is a standardized identifier that stays the same across reboots and hardware changes. Unlike device names like `/dev/sda1`, which can shift when you add or remove drives, a UUID is permanent. This makes UUIDs ideal for mounting filesystems, configuring bootloaders, and managing storage.

UUIDs are typically formatted as 36 characters in a 8-4-4-4-12 pattern, like `a1b2c3d4-e5f6-7890-abcd-ef1234567890`. Linux uses them in the `/etc/fstab` file to ensure consistent mounting. Knowing how to get uuid in linux helps you avoid boot failures and disk errors.

Method 1: Using The Blkid Command

The `blkid` command is the most common way to find UUIDs. It shows block device attributes including UUIDs, filesystem types, and labels.

  1. Open a terminal window.
  2. Type `blkid` and press Enter.
  3. Look for the UUID field next to each device.

Example output:
“`
/dev/sda1: UUID=”a1b2c3d4-e5f6-7890-abcd-ef1234567890″ TYPE=”ext4″
/dev/sda2: UUID=”b2c3d4e5-f6a7-8901-bcde-f12345678901″ TYPE=”swap”
“`

You can also specify a particular device:
“`
sudo blkid /dev/sda1
“`

The `sudo` prefix ensures you see all devices, including those requiring root access. This method works on almost all Linux distributions.

Method 2: Checking /Etc/fstab

Your system’s filesystem table (`/etc/fstab`) already lists UUIDs for mounted partitions. This is a quick way to see which UUIDs your system uses.

  1. Open `/etc/fstab` with a text editor: `sudo nano /etc/fstab`
  2. Look for lines starting with `UUID=`. Each line shows the UUID for a partition.
  3. Note the mount point and filesystem type next to each UUID.

Example line:
“`
UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 / ext4 defaults 0 1
“`

This method is read-only and safe. It only shows UUIDs currently in use, not all available devices.

Method 3: Using The Lsblk Command

The `lsblk` command lists block devices in a tree-like format. It can show UUIDs with the `-f` option.

  1. Run `lsblk -f` in the terminal.
  2. Look for the UUID column in the output.
  3. Identify the device by its name or mount point.

Example output:
“`
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 a1b2c3d4-e5f6-7890-abcd-ef1234567890 /
└─sda2 swap b2c3d4e5-f6a7-8901-bcde-f12345678901 [SWAP]
“`

The `lsblk` command is cleaner than `blkid` for visual scanning. It also shows mount points, making it easier to map UUIDs to partitions.

Method 4: Reading /Dev/disk/by-uuid Directory

Linux maintains a special directory with symbolic links named after UUIDs. Each link points to the actual device file.

  1. Navigate to `/dev/disk/by-uuid/`: `cd /dev/disk/by-uuid/`
  2. List the contents: `ls -l`
  3. Each symlink name is a UUID, and it points to the device.

Example output:
“`
lrwxrwxrwx 1 root root 10 Jan 1 12:00 a1b2c3d4-e5f6-7890-abcd-ef1234567890 -> ../../sda1
“`

This method is useful for scripting. You can use the UUID as a stable path to access the device.

Method 5: Using The Udevadm Command

The `udevadm` command queries the udev device manager. It provides detailed information about devices, including UUIDs.

  1. Find the device path: `udevadm info –query=property –name=/dev/sda1`
  2. Look for `ID_FS_UUID` in the output.
  3. You can also use `–query=all` for complete details.

Example:
“`
udevadm info –query=property –name=/dev/sda1 | grep UUID
“`

This method is more advanced but gives you precise control over device queries.

Method 6: Using The Findmnt Command

The `findmnt` command shows mounted filesystems. With the `-o` option, you can display UUIDs.

  1. Run `findmnt -o TARGET,SOURCE,UUID`
  2. Review the output for UUIDs of mounted devices.
  3. Add `-D` to include filesystem details.

Example output:
“`
TARGET SOURCE UUID
/ /dev/sda1 a1b2c3d4-e5f6-7890-abcd-ef1234567890
“`

This command is great for seeing which UUIDs are actively mounted.

Method 7: Using The Vol_id Command (Legacy)

Older Linux systems used `vol_id` to retrieve UUIDs. While deprecated, it may still be available on some distributions.

  1. Install `vol_id` if needed: `sudo apt install vol_id` (Debian/Ubuntu)
  2. Run `sudo vol_id /dev/sda1`
  3. Look for `ID_FS_UUID` in the output.

This method is less common today but useful for legacy systems.

Method 8: Using The Dumpe2fs Command For Ext Filesystems

For ext2, ext3, and ext4 filesystems, `dumpe2fs` can show the UUID.

  1. Run `sudo dumpe2fs -h /dev/sda1 | grep UUID`
  2. The `-h` flag shows the superblock information.
  3. Look for `Filesystem UUID` in the output.

Example:
“`
Filesystem UUID: a1b2c3d4-e5f6-7890-abcd-ef1234567890
“`

This method is filesystem-specific but very reliable for ext partitions.

Method 9: Using The Xfs_admin Command For XFS Filesystems

For XFS filesystems, use `xfs_admin` to retrieve the UUID.

  1. Run `sudo xfs_admin -u /dev/sda1`
  2. The output shows the UUID directly.
  3. You can also use `xfs_admin -l` for more details.

Example:
“`
UUID = a1b2c3d4-e5f6-7890-abcd-ef1234567890
“`

This method is essential for XFS filesystems commonly used in enterprise environments.

Method 10: Using The Btrfs Filesystem Commands

For Btrfs filesystems, use `btrfs filesystem show` to find UUIDs.

  1. Run `sudo btrfs filesystem show /dev/sda1`
  2. Look for the `UUID` line in the output.
  3. You can also use `btrfs device usage` for more details.

Example:
“`
Label: none uuid: a1b2c3d4-e5f6-7890-abcd-ef1234567890
“`

Btrfs UUIDs are critical for managing subvolumes and snapshots.

Practical Applications Of UUIDs

Knowing how to get uuid in linux is valuable for several tasks:

  • Mounting partitions in /etc/fstab: Use `UUID=…` instead of device names for stable mounts.
  • Configuring GRUB bootloader: GRUB uses UUIDs to locate the root filesystem.
  • Backup scripts: Use UUIDs to ensure you always target the correct drive.
  • Disk management: Identify drives even when device names change.
  • Network storage: UUIDs help in iSCSI and NFS configurations.

Common Issues And Troubleshooting

UUID Not Showing In blkid Output

If `blkid` shows no UUID, the device may not have a filesystem. Run `sudo mkfs.ext4 /dev/sda1` to create one, or check if the device is a partition.

Permission Denied Errors

Some commands require root privileges. Always use `sudo` when needed. For example, `sudo blkid` shows all devices.

Duplicate UUIDs

Duplicate UUIDs can cause boot failures. Use `sudo tune2fs -U random /dev/sda1` to generate a new UUID for an ext filesystem.

UUID Changes After Formatting

Reformatting a partition generates a new UUID. Update `/etc/fstab` and bootloader configs accordingly.

Automating UUID Retrieval With Scripts

You can create a simple script to list all UUIDs:

#!/bin/bash
echo "Device UUIDs:"
for dev in /dev/sd*[1-9]; do
    uuid=$(blkid -s UUID -o value "$dev" 2>/dev/null)
    if [ -n "$uuid" ]; then
        echo "$dev: $uuid"
    fi
done

Save as `list-uuids.sh`, make executable with `chmod +x list-uuids.sh`, and run with `sudo ./list-uuids.sh`.

Using UUIDs In /Etc/fstab

To mount a partition by UUID, edit `/etc/fstab`:

  1. Get the UUID: `sudo blkid /dev/sda1`
  2. Add a line like: `UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 /mnt/data ext4 defaults 0 2`
  3. Create the mount point: `sudo mkdir -p /mnt/data`
  4. Mount it: `sudo mount -a`

This ensures the partition always mounts to the same location, even if device names change.

UUIDs In Bootloader Configuration

GRUB uses UUIDs in `/boot/grub/grub.cfg`. To update GRUB after changes:

  1. Run `sudo update-grub` (Debian/Ubuntu) or `sudo grub-mkconfig -o /boot/grub/grub.cfg` (others).
  2. Verify UUIDs in the generated config file.
  3. Reboot to test.

Generating New UUIDs

Sometimes you need to change a UUID, for example after cloning a drive.

  • ext2/3/4: `sudo tune2fs -U random /dev/sda1`
  • XFS: `sudo xfs_admin -U generate /dev/sda1`
  • Btrfs: `sudo btrfs filesystem uuid /dev/sda1` (regenerates UUID)
  • Swap: `sudo mkswap -U random /dev/sda1`

Always update `/etc/fstab` and bootloader after changing UUIDs.

Security Considerations

UUIDs are not secret. They can be read by any user with access to the device. However, exposing UUIDs in logs or scripts is generally safe. For encrypted partitions, UUIDs identify the encrypted container, not the content.

Comparing UUIDs With Other Identifiers

  • UUID: Permanent, unique, survives reboots.
  • PARTUUID: Partition table UUID, changes if partition table is rewritten.
  • PTUUID: Disk-level UUID from partition table.
  • LABEL: User-defined name, can be changed.
  • Device name: Like `/dev/sda1`, can change dynamically.

UUIDs are the most reliable for persistent identification.

Frequently Asked Questions

How do I find the UUID of a specific partition?

Use `sudo blkid /dev/sda1` or `lsblk -f /dev/sda1`. Replace `/dev/sda1` with your partition.

Can I change the UUID of a filesystem?

Yes, using `tune2fs -U random /dev/sda1` for ext filesystems, or `xfs_admin -U generate` for XFS. Be careful to update configs.

Why does my UUID show as all zeros?

This usually means the partition has no filesystem. Create one with `mkfs` or check if it’s an unformatted partition.

How do I use UUID in /etc/fstab?

Write `UUID=your-uuid-here /mount/point filesystem-type defaults 0 2`. Replace with your values.

What is the difference between UUID and PARTUUID?

UUID identifies the filesystem, while PARTUUID identifies the partition entry in the partition table. UUID survives filesystem changes; PARTUUID changes if you repartition.

Conclusion

Knowing how to get uuid in linux is a fundamental skill for system administration. Whether you use `blkid`, `lsblk`, or the `/dev/disk/by-uuid/` directory, each method provides reliable access to these permanent identifiers. UUIDs ensure your system boots correctly, mounts filesystems consistently, and manages storage predictably.

Practice these commands on a test system to build confidence. Once you master UUID retrieval, you can automate backups, configure complex storage setups, and troubleshoot boot issues with ease. The permanence of UUIDs makes them indispensable in any Linux environment.