In Linux systems, UUIDs (Universally Unique Identifiers) provide a more reliable way to identify storage devices than traditional device names like /dev/sda1
. Unlike labels which can be changed and device names which may shift during boot, UUIDs remain constant throughout the filesystem's lifetime.
The most straightforward command to display UUIDs is:
blkid
This will output all block devices with their filesystem types, UUIDs, and other attributes. For example:
/dev/sda1: UUID="5e3a1a7b-01" TYPE="ext4"
/dev/sdb2: UUID="a1b2c3d4-5678" TYPE="xfs"
To get the UUID of a particular mounted filesystem:
lsblk -f /dev/sdXn
Or more specifically:
sudo blkid /dev/sdXn
Replace sdXn
with your actual device (e.g., sda1
).
For systems using systemd, you can use:
systemd-mount --list
Or examine /etc/fstab
entries:
grep UUID /etc/fstab
When writing scripts, you might want to extract just the UUID value:
sudo blkid -s UUID -o value /dev/sdXn
Or for all devices:
lsblk -o NAME,UUID
If you're not seeing expected UUIDs:
- Ensure the device isn't currently mounted read-only
- Check for filesystem corruption with
fsck
- Verify the device exists with
lsblk
In Linux systems like Ubuntu, every filesystem is assigned a Universally Unique Identifier (UUID). Unlike partition labels which can be changed, UUIDs are persistent identifiers that remain constant even if the device name changes (e.g., from /dev/sda1
to /dev/sdb1
after hardware changes). This makes UUIDs particularly useful for:
- Stable mounting in
/etc/fstab
- Systemd mount units
- Disk management scripts
The most reliable method is using the blkid
command, which shows UUIDs for all block devices:
sudo blkid
Sample output:
/dev/sda1: UUID="5e3a1a7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f" TYPE="ext4"
/dev/sdb2: UUID="a1b2-c3d4" TYPE="vfat"
To find just the UUID of a specific partition:
sudo blkid -s UUID -o value /dev/sda1
This returns only the UUID string, perfect for scripting:
5e3a1a7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f
For systems without blkid
, try these approaches:
# Using lsblk
lsblk -o NAME,UUID
# From /dev/disk/by-uuid
ls -l /dev/disk/by-uuid/
# Using udevadm (for advanced users)
udevadm info -q all -n /dev/sda1 | grep ID_FS_UUID
Here's how to properly use UUIDs in your /etc/fstab
:
# Get UUID
UUID=$(sudo blkid -s UUID -o value /dev/sda1)
# Create backup
sudo cp /etc/fstab /etc/fstab.bak
# Append new entry
echo "UUID=${UUID} /mnt/data ext4 defaults 0 2" | sudo tee -a /etc/fstab
If you encounter problems:
- Verify UUIDs match between
blkid
and/etc/fstab
- Check for hidden characters when copying UUIDs
- Remember that some filesystems (like FAT) use shorter UUID formats
To change a filesystem UUID (use with caution!):
# For ext2/3/4
sudo tune2fs -U random /dev/sda1
# For XFS
sudo xfs_admin -U generate /dev/sda1
# For FAT/VFAT
sudo fatlabel -i /dev/sda1