15

First of all, I would like to apologies for the title for not including hardware details. This is because I am looking for a command that will work in all Linux distribution.

Question:

I am very new to Linux and don't know much, however, is it possible to retrieve the hard drive serial number running as non-root. I am aware there are commands available to retrieve this information running using sudo. However, I need a way to retrieve this information without running as sudo.

Any suggestions would be highly appreciated.

Kind Regards,

Mustafa

5 Answers 5

19

On most current distributions HDD serial numbers are stored in the udev database, which could be queried without root permissions:

/sbin/udevadm info --query=property --name=sda

(look for ID_SERIAL, ID_SERIAL_SHORT; there are also /dev/disk/by-id/ata-* symlinks for ATA drives based on those values).

udisks provides a higher-level interface to those data and more (it also gives access to SMART attributes without requiring root privileges, which would be needed for calling, e.g., smartctl directly).

1
  • You're right, just tried udisks --show-info .__But__ it must be noted that when used in batch mode or generally in scripts, udisks is WAY slower than udevadm. May 31, 2015 at 17:22
6

Another way that usually works is:

ls -l /dev/disk/by-id/

Here's a one liner that gives you a quick enumeration of drive and model/serial number:

ls -l /dev/disk/by-id/ | grep scsi- | grep -v part | awk '{print $NF " " $(NF-2)}' | sed 's|../../||g' | sed 's/scsi-...._//g'  

Or for remote machines:

ssh $host "ls -l /dev/disk/by-id/ | grep scsi- | grep -v part | awk '{print \$NF \" \" \$(NF-2)}' | sed 's|../../||g' | sed 's/scsi-...._//g'"

You can then further process this output. For most drive types, the second string per line is MODEL_SERIAL.

2

Like womble said, without root access or granting root-like permissions to some process, you can't achieve this. However, since you say you are a newbie, let me note you about a handy command called lshw.

1
  • Thank you to both Womble and yourself, very much appreciated. I shall investigate the command you mentioned.
    – user92971
    Aug 28, 2011 at 10:40
1

Short of granting root-like permissions to some process, you can't achieve this. Even as root you can't do it on some hardware configurations.

1
  • Thanks for your quick response Womble. I will give it a go.
    – mustafa
    Aug 26, 2011 at 10:18
0

The links under /dev/disk/by-id is created by udev. In /etc/udev/rules.d/50-udev.rules such lines can be found:

KERNEL=="sd*[!0-9]|sr*", SYSFS{ieee1394_id}=="*", ENV{ID_SERIAL}="$sysfs{ieee1394_id}", ENV{ID_BUS}="ieee1394"
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}=="", IMPORT{program}="/lib/udev/usb_id -x"
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}=="", IMPORT{program}="/lib/udev/scsi_id -g -x -s %p -d $tempnode"
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}=="", IMPORT{program}="/lib/udev/scsi_id -g -x -a -s %p -d $tempnode"
KERNEL=="nst[0-9]*|st*|sd*[!0-9]|sr*|dasd*[!0-9]|cciss?c", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}"

Unfortunately it is not trivial to find out this by-id symlink from the name of the device. But there is at least one direct way:

$ udevinfo -q path -n /dev/sda
/block/sda
$ udevinfo -q symlink -p /block/sda
disk/by-id/scsi-SATA_KINGSTON_SVP100_31JY100MY5SK disk/by-path/pci-0000:00:11.0-scsi-0:0:0:0

A space separated list is returned. So the by-id path can be found.

Or You can directly get the "composite" serial number:

$ udevinfo -q env -p /block/sda
ID_VENDOR=ATA
ID_MODEL=KINGSTON_SVP100S
ID_REVISION=CJR1
ID_SERIAL=SATA_KINGSTON_SVP100_31JY100MY5SK
ID_TYPE=disk
ID_BUS=scsi
ID_PATH=pci-0000:00:11.0-scsi-0:0:0:0

In the ID_SERIAL after the last underscore You can get the serial number.

As I see a serial number can contain alphanumerical characters and '-' (I have such SCSI HDDs). Does anybody know any limitation about the serial number? It if can contain '_' then this method is not bullet proof.

The ID_MODEL key could be used to localise more precisely the beginning of the serial number, as (it seems to me) its first 15 characters are added to the ID_SERIAL (spaces replaced by '_').

1
  • "udevinfo" did not work for me here on Ubuntu. However, the following one-liner did exactly what I wanted: udevadm info -q property -xn sda | awk -F\' '/ID_SERIAL_SHORT/{print $2}' Of course, the -q property can be left out, but I always think it's a waste of resources to let udevadm output additional lines we don't need at all. May 31, 2015 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .