How to show block device UUID → blkid
How to Show Block Device UUID Using the blkid Command
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding UUIDs and Block Devices](#understanding-uuids-and-block-devices)
4. [Basic blkid Command Usage](#basic-blkid-command-usage)
5. [Advanced blkid Options and Examples](#advanced-blkid-options-and-examples)
6. [Practical Use Cases](#practical-use-cases)
7. [Alternative Methods to Find UUIDs](#alternative-methods-to-find-uuids)
8. [Troubleshooting Common Issues](#troubleshooting-common-issues)
9. [Best Practices and Professional Tips](#best-practices-and-professional-tips)
10. [Conclusion](#conclusion)
Introduction
The `blkid` command is an essential utility for Linux system administrators and users who need to identify and manage block devices. This comprehensive guide will teach you how to effectively use the `blkid` command to display Universally Unique Identifiers (UUIDs) and other important information about block devices on your Linux system.
Understanding how to retrieve and work with block device UUIDs is crucial for various system administration tasks, including mounting filesystems, configuring `/etc/fstab`, setting up RAID arrays, and managing storage devices. By the end of this article, you'll have mastered the `blkid` command and understand when and how to apply it in real-world scenarios.
Prerequisites
Before diving into the `blkid` command, ensure you have the following:
- Operating System: A Linux distribution (Ubuntu, CentOS, RHEL, Debian, Fedora, etc.)
- User Privileges: Basic user access (root privileges required for some operations)
- Terminal Access: Command-line interface or SSH access to the system
- Basic Knowledge: Fundamental understanding of Linux file systems and storage concepts
- Package Requirements: The `util-linux` package (usually installed by default)
Verifying blkid Installation
Most Linux distributions include `blkid` as part of the `util-linux` package. To verify it's installed:
```bash
which blkid
Output: /sbin/blkid or /usr/bin/blkid
blkid --version
Output: blkid from util-linux 2.37.2 (or similar)
```
If `blkid` is not installed, install it using your distribution's package manager:
```bash
Ubuntu/Debian
sudo apt update && sudo apt install util-linux
CentOS/RHEL/Fedora
sudo yum install util-linux
or
sudo dnf install util-linux
```
Understanding UUIDs and Block Devices
What is a UUID?
A Universally Unique Identifier (UUID) is a 128-bit identifier that provides a reliable way to identify storage devices and filesystems. Unlike device names (such as `/dev/sda1`) which can change based on hardware configuration or boot order, UUIDs remain constant regardless of how the system detects the device.
UUID Format
UUIDs typically appear in the following format:
```
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
```
Example: `f47ac10b-58cc-4372-a567-0e02b2c3d479`
Why Use UUIDs?
1. Persistence: UUIDs don't change when hardware configuration changes
2. Reliability: More reliable than device names for mounting filesystems
3. Uniqueness: Virtually guaranteed to be unique across all systems
4. Portability: Useful when moving drives between systems
Basic blkid Command Usage
Simple blkid Execution
The most basic usage of `blkid` displays information about all block devices:
```bash
blkid
```
Sample Output:
```
/dev/sda1: UUID="f47ac10b-58cc-4372-a567-0e02b2c3d479" TYPE="ext4" PARTUUID="12345678-01"
/dev/sda2: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="swap" PARTUUID="12345678-02"
/dev/sdb1: UUID="9876543a-bcde-f012-3456-789abcdef012" TYPE="ext4" PARTUUID="87654321-01"
```
Understanding the Output
Each line contains several key pieces of information:
- Device Path: The device file (e.g., `/dev/sda1`)
- UUID: The unique identifier for the filesystem
- TYPE: The filesystem type (ext4, swap, ntfs, etc.)
- PARTUUID: The partition's unique identifier
- LABEL: The filesystem label (if set)
Displaying Information for a Specific Device
To show information for a specific block device:
```bash
blkid /dev/sda1
```
Output:
```
/dev/sda1: UUID="f47ac10b-58cc-4372-a567-0e02b2c3d479" TYPE="ext4" PARTUUID="12345678-01"
```
Showing Only UUIDs
To display only the UUID without additional information:
```bash
blkid -s UUID -o value /dev/sda1
```
Output:
```
f47ac10b-58cc-4372-a567-0e02b2c3d479
```
Advanced blkid Options and Examples
Common Command-Line Options
| Option | Description | Example |
|--------|-------------|---------|
| `-s TAG` | Show only specified tag | `blkid -s UUID` |
| `-o FORMAT` | Output format (value, device, list, udev, export, full) | `blkid -o list` |
| `-t TAG=value` | Search for devices with specific tag | `blkid -t TYPE=ext4` |
| `-L LABEL` | Find device by filesystem label | `blkid -L "MyDisk"` |
| `-U UUID` | Find device by UUID | `blkid -U f47ac10b-58cc-4372-a567-0e02b2c3d479` |
| `-c FILE` | Use alternative cache file | `blkid -c /tmp/blkid.tab` |
| `-g` | Perform garbage collection on cache | `blkid -g` |
| `-p` | Low-level superblock probing | `blkid -p /dev/sda1` |
Detailed Examples
1. List All Devices with Specific Filesystem Type
```bash
blkid -t TYPE=ext4
```
Output:
```
/dev/sda1: UUID="f47ac10b-58cc-4372-a567-0e02b2c3d479" TYPE="ext4" PARTUUID="12345678-01"
/dev/sdb1: UUID="9876543a-bcde-f012-3456-789abcdef012" TYPE="ext4" PARTUUID="87654321-01"
```
2. Display Information in List Format
```bash
blkid -o list
```
Output:
```
device fs_type label mount point UUID
/dev/sda1 ext4 root / f47ac10b-58cc-4372-a567-0e02b2c3d479
/dev/sda2 swap [SWAP] a1b2c3d4-e5f6-7890-abcd-ef1234567890
/dev/sdb1 ext4 data /home/data 9876543a-bcde-f012-3456-789abcdef012
```
3. Find Device by UUID
```bash
blkid -U f47ac10b-58cc-4372-a567-0e02b2c3d479
```
Output:
```
/dev/sda1
```
4. Find Device by Label
```bash
blkid -L "root"
```
Output:
```
/dev/sda1
```
5. Export Format for Scripting
```bash
blkid -o export /dev/sda1
```
Output:
```
DEVNAME=/dev/sda1
UUID=f47ac10b-58cc-4372-a567-0e02b2c3d479
TYPE=ext4
PARTUUID=12345678-01
```
6. Low-Level Probing
```bash
sudo blkid -p /dev/sda1
```
Output:
```
/dev/sda1: UUID="f47ac10b-58cc-4372-a567-0e02b2c3d479" VERSION="1.0" TYPE="ext4" USAGE="filesystem"
```
Practical Use Cases
1. Configuring /etc/fstab
One of the most common uses for UUIDs is in the `/etc/fstab` file for reliable filesystem mounting:
```bash
Get the UUID
UUID=$(blkid -s UUID -o value /dev/sdb1)
Add to /etc/fstab
echo "UUID=$UUID /mnt/data ext4 defaults 0 2" | sudo tee -a /etc/fstab
```
2. Scripting and Automation
Create a script to check if a specific UUID exists:
```bash
#!/bin/bash
check_uuid() {
local uuid="$1"
local device
device=$(blkid -U "$uuid" 2>/dev/null)
if [ -n "$device" ]; then
echo "UUID $uuid found on device: $device"
return 0
else
echo "UUID $uuid not found"
return 1
fi
}
Usage
check_uuid "f47ac10b-58cc-4372-a567-0e02b2c3d479"
```
3. System Inventory and Documentation
Generate a comprehensive report of all storage devices:
```bash
#!/bin/bash
echo "=== System Storage Inventory ==="
echo "Generated on: $(date)"
echo ""
blkid -o list | while IFS= read -r line; do
if [[ $line =~ ^device ]]; then
echo "$line"
echo "----------------------------------------"
else
echo "$line"
fi
done
```
4. Backup and Recovery Operations
Before performing backup operations, verify the target device:
```bash
#!/bin/bash
BACKUP_UUID="9876543a-bcde-f012-3456-789abcdef012"
BACKUP_DEVICE=$(blkid -U "$BACKUP_UUID")
if [ -n "$BACKUP_DEVICE" ]; then
echo "Backup device found: $BACKUP_DEVICE"
# Proceed with backup
rsync -av /home/user/ "/mnt/backup/"
else
echo "Error: Backup device not found!"
exit 1
fi
```
5. Monitoring and Alerting
Create a monitoring script to alert when expected devices are missing:
```bash
#!/bin/bash
EXPECTED_UUIDS=(
"f47ac10b-58cc-4372-a567-0e02b2c3d479"
"a1b2c3d4-e5f6-7890-abcd-ef1234567890"
"9876543a-bcde-f012-3456-789abcdef012"
)
missing_devices=()
for uuid in "${EXPECTED_UUIDS[@]}"; do
if ! blkid -U "$uuid" >/dev/null 2>&1; then
missing_devices+=("$uuid")
fi
done
if [ ${#missing_devices[@]} -gt 0 ]; then
echo "WARNING: Missing devices detected!"
printf '%s\n' "${missing_devices[@]}"
# Send alert (email, log, etc.)
fi
```
Alternative Methods to Find UUIDs
While `blkid` is the primary tool for finding UUIDs, several alternative methods exist:
1. Using /dev/disk/by-uuid/
```bash
ls -la /dev/disk/by-uuid/
```
Output:
```
lrwxrwxrwx 1 root root 10 Nov 15 10:30 f47ac10b-58cc-4372-a567-0e02b2c3d479 -> ../../sda1
lrwxrwxrwx 1 root root 10 Nov 15 10:30 a1b2c3d4-e5f6-7890-abcd-ef1234567890 -> ../../sda2
```
2. Using lsblk Command
```bash
lsblk -f
```
Output:
```
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 root f47ac10b-58cc-4372-a567-0e02b2c3d479 /
└─sda2 swap a1b2c3d4-e5f6-7890-abcd-ef1234567890 [SWAP]
```
3. Using udevadm
```bash
udevadm info --query=property --name=/dev/sda1 | grep UUID
```
Output:
```
ID_FS_UUID=f47ac10b-58cc-4372-a567-0e02b2c3d479
ID_FS_UUID_ENC=f47ac10b-58cc-4372-a567-0e02b2c3d479
```
4. Reading Filesystem Superblock
For ext2/3/4 filesystems:
```bash
sudo tune2fs -l /dev/sda1 | grep UUID
```
Output:
```
Filesystem UUID: f47ac10b-58cc-4372-a567-0e02b2c3d479
```
Troubleshooting Common Issues
Issue 1: Permission Denied
Problem:
```bash
$ blkid /dev/sda1
blkid: /dev/sda1: Permission denied
```
Solution:
Use sudo for accessing raw block devices:
```bash
sudo blkid /dev/sda1
```
Issue 2: Device Not Found
Problem:
```bash
$ blkid /dev/sdc1
blkid: cannot open /dev/sdc1: No such file or directory
```
Solutions:
1. Verify the device exists:
```bash
ls -la /dev/sd*
lsblk
```
2. Check if it's a different device name:
```bash
blkid | grep -v loop
```
Issue 3: No UUID Displayed
Problem:
Some devices may not show UUIDs, particularly:
- Unformatted partitions
- Certain filesystem types
- Corrupted filesystems
Solutions:
1. Check if the device has a filesystem:
```bash
sudo blkid -p /dev/sda1
```
2. Format the device if needed:
```bash
sudo mkfs.ext4 /dev/sda1
```
Issue 4: Outdated Cache Information
Problem:
`blkid` may show outdated information due to caching.
Solution:
Clear the cache and rescan:
```bash
sudo blkid -g # Garbage collect cache
sudo blkid # Rescan devices
```
Issue 5: UUID Not Found After Device Changes
Problem:
After cloning or moving drives, UUIDs might conflict or not be found.
Solutions:
1. Generate new UUIDs:
```bash
sudo tune2fs -U random /dev/sda1 # For ext2/3/4
sudo swaplabel -U clear /dev/sda2 # For swap
```
2. Update system configuration files accordingly.
Issue 6: Script Failures
Problem:
Scripts using `blkid` fail unexpectedly.
Best Practices:
```bash
#!/bin/bash
Always check return codes
if ! uuid=$(blkid -s UUID -o value /dev/sda1 2>/dev/null); then
echo "Error: Could not retrieve UUID for /dev/sda1"
exit 1
fi
Validate UUID format
if [[ ! $uuid =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ ]]; then
echo "Error: Invalid UUID format: $uuid"
exit 1
fi
```
Best Practices and Professional Tips
1. Security Considerations
- Always use `sudo` when accessing raw block devices
- Be cautious when scripting with `blkid` in production environments
- Validate UUIDs before using them in critical operations
2. Performance Optimization
- Use specific device paths when possible to avoid scanning all devices
- Cache results in scripts when performing multiple operations
- Use the `-c` option to specify alternative cache locations for better performance
3. Scripting Best Practices
```bash
#!/bin/bash
Professional script example
set -euo pipefail # Exit on error, undefined vars, pipe failures
readonly SCRIPT_NAME="$(basename "$0")"
readonly LOG_FILE="/var/log/${SCRIPT_NAME}.log"
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
get_uuid_safely() {
local device="$1"
local uuid
if [[ ! -b "$device" ]]; then
log "ERROR: $device is not a valid block device"
return 1
fi
if uuid=$(blkid -s UUID -o value "$device" 2>/dev/null); then
echo "$uuid"
return 0
else
log "WARNING: Could not retrieve UUID for $device"
return 1
fi
}
```
4. Documentation and Inventory
Maintain an inventory of your system's UUIDs:
```bash
#!/bin/bash
Generate system storage inventory
{
echo "# System Storage Inventory"
echo "# Generated: $(date)"
echo "# Hostname: $(hostname)"
echo ""
blkid -o export | while IFS= read -r line; do
if [[ $line =~ ^DEVNAME= ]]; then
echo ""
echo "## Device: ${line#DEVNAME=}"
else
echo "- $line"
fi
done
} > "/etc/storage-inventory.md"
```
5. Monitoring and Maintenance
Set up regular checks for filesystem health:
```bash
#!/bin/bash
Check for filesystem issues
blkid -o device | while read -r device; do
if [[ $device =~ /dev/loop ]]; then
continue # Skip loop devices
fi
fstype=$(blkid -s TYPE -o value "$device" 2>/dev/null)
case "$fstype" in
ext[234])
if tune2fs -l "$device" 2>/dev/null | grep -q "has_journal"; then
echo "✓ $device ($fstype) appears healthy"
else
echo "⚠ $device ($fstype) may have issues"
fi
;;
*)
echo "ℹ $device ($fstype) - manual check recommended"
;;
esac
done
```
6. Integration with Configuration Management
For systems using configuration management tools:
```yaml
Ansible example
- name: Get root filesystem UUID
command: blkid -s UUID -o value /dev/sda1
register: root_uuid
changed_when: false
- name: Update fstab with UUID
lineinfile:
path: /etc/fstab
regexp: '^/dev/sda1'
line: "UUID={{ root_uuid.stdout }} / ext4 defaults 0 1"
backup: yes
```
Conclusion
The `blkid` command is an indispensable tool for Linux system administrators and users who need to manage block devices and filesystems effectively. Throughout this comprehensive guide, we've explored:
1. Fundamental concepts of UUIDs and their importance in modern Linux systems
2. Basic and advanced usage of the `blkid` command with practical examples
3. Real-world applications including system configuration, scripting, and automation
4. Alternative methods for finding device information
5. Troubleshooting techniques for common issues
6. Professional best practices for production environments
Key Takeaways
- UUIDs provide persistent, reliable device identification that doesn't change with hardware configuration
- The `blkid` command offers flexible options for retrieving device information in various formats
- Proper error handling and validation are crucial when using `blkid` in scripts
- Regular monitoring and maintenance of storage devices helps prevent issues
- Understanding alternative methods provides backup options when `blkid` isn't available
Next Steps
To further enhance your Linux storage management skills, consider exploring:
1. Advanced filesystem management with tools like `tune2fs`, `resize2fs`, and `fsck`
2. LVM (Logical Volume Management) for flexible storage allocation
3. RAID configuration and management
4. Backup and recovery strategies using UUIDs for device identification
5. Monitoring solutions for storage health and performance
By mastering the `blkid` command and following the best practices outlined in this guide, you'll be well-equipped to handle storage device identification and management tasks in any Linux environment. Remember to always test commands in non-production environments first, and maintain proper documentation of your system's storage configuration.
The skills and knowledge gained from this guide will serve as a solid foundation for more advanced storage management tasks and help ensure the reliability and maintainability of your Linux systems.