When working with Linux systems, you might need to retrieve hardware information like hard drive serial numbers for inventory tracking, system identification, or software licensing purposes. The challenge arises when you need to do this without root privileges, which is common in shared hosting environments or corporate systems where users don't have sudo access.
Before exploring non-root solutions, let's look at the standard methods that typically require root access:
sudo hdparm -I /dev/sda | grep Serial
sudo smartctl -i /dev/sda | grep Serial
Fortunately, there are several ways to access this information without root privileges:
1. Using udevadm (Most Reliable)
The udevadm command can often access this information without requiring root:
udevadm info --query=property --name=/dev/sda | grep ID_SERIAL
2. Checking sysfs
The Linux kernel exposes some hardware information through sysfs:
cat /sys/block/sda/device/serial
Note: This might not work on all distributions or might return a truncated serial.
3. Using lsblk
The lsblk command can sometimes display serial numbers:
lsblk -o NAME,SERIAL
For maximum compatibility across different Linux distributions, I recommend this combination:
{
udevadm info --query=property --name=/dev/sda 2>/dev/null | grep -i serial ||
lsblk -o serial /dev/sda 2>/dev/null | tail -n +2 ||
cat /sys/block/sda/device/serial 2>/dev/null
}
To get serial numbers for all drives in the system:
for drive in /dev/sd?; do
echo -n "$drive: "
udevadm info --query=property --name=$drive | grep ID_SERIAL_SHORT | cut -d= -f2
done
Be aware that:
- Some virtualization environments may not expose real hardware serial numbers
- Certain filesystem permissions might still block access to these methods
- USB drives might behave differently than internal drives
Here's a more robust bash function that tries multiple methods:
get_drive_serial() {
local drive=$1
local serial
# Try udevadm first
serial=$(udevadm info --query=property --name=$drive 2>/dev/null | grep -i ID_SERIAL_SHORT | cut -d= -f2)
# Fall back to other methods
[ -z "$serial" ] && serial=$(lsblk -o serial $drive 2>/dev/null | tail -n +2)
[ -z "$serial" ] && serial=$(cat /sys/block/${drive#/dev/}/device/serial 2>/dev/null)
echo "$serial"
}
# Usage example:
get_drive_serial /dev/sda
When working with Linux systems in enterprise environments or multi-user setups, you often need to retrieve hardware information without root privileges. The hard disk serial number is particularly useful for:
- System identification
- License validation
- Inventory management
- Asset tracking
Most traditional methods require elevated permissions:
sudo hdparm -I /dev/sda | grep 'Serial Number'
sudo smartctl -i /dev/sda | grep 'Serial'
Method 1: Using udevadm
This is often the most reliable non-root approach:
udevadm info --query=property --name=/dev/sda | grep 'ID_SERIAL'
Method 2: Checking sysfs
For systems with proper permissions:
cat /sys/block/sda/device/serial
Method 3: Using lsblk
Modern systems often support this:
lsblk -o NAME,SERIAL
If these methods fail due to permission restrictions:
- Check if your user is part of the 'disk' group
- Verify udev rules haven't been modified to restrict access
- Consider asking the sysadmin to adjust permissions for specific devices
The most portable solution would be a script that tries multiple methods:
#!/bin/bash
get_serial() {
# Try udevadm first
local serial=$(udevadm info --query=property --name=/dev/sda 2>/dev/null | grep -i 'ID_SERIAL=' | cut -d'=' -f2)
if [ -z "$serial" ]; then
# Fallback to sysfs
serial=$(cat /sys/block/sda/device/serial 2>/dev/null)
fi
if [ -z "$serial" ]; then
# Final fallback to lsblk
serial=$(lsblk -o SERIAL -d -n /dev/sda 2>/dev/null)
fi
echo "${serial:-UNKNOWN}"
}
get_serial
Be aware that:
- Virtual machines may not expose real hardware serials
- Some enterprise storage solutions mask serial numbers
- NVMe devices use different paths (/dev/nvme*)
- USB-connected drives might have different permission requirements