Harddisk serial number from terminal?

I have multiple harddisks which get connected to my server and I’m not sure which one is what in the view of sdXY . If I can see the serial numbers of my harddisks from terminal, I can easily identify them.

Is there any way I can see them from terminal?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Another solution which does not require root privileges:

udevadm info --query=all --name=/dev/sda | grep ID_SERIAL

This is actually the library that lsblk, mentioned by don_crissti, leverages, but my version of lsblk does not include the option for serial.

Method 2

In terminal type:

# hdparm -I /dev/sd? | grep 'Serial Number'

EDIT: You can also use lshw or smartctl

  • lshw

    # lshw -class disk

  • smartctl

    # smartctl -i /dev/sda

If you are missing those tools, just install following packages

# apt-get install hdparm
# apt-get install smartmontools
# apt-get install lshw

Method 3

Device1 name and corresponding serial number:

lsblk --nodeps -o name,serial

output:

NAME SERIAL
sda  0000000012400917BA30
sdb  0000000012400917BA96

add -n if you don’t want to print the header line:

lsblk --nodeps -no name,serial

output:

sda  0000000012400917BA30
sdb  0000000012400917BA96

Pass device as argument to get only the serial number of a specific device:

lsblk --nodeps -no serial /dev/sda

output:

0000000012400917BA30

Keep in mind lsblk lists information about all available (or the specified) block devices. Now, for those who do not know what that last term means:
In general, block devices are devices that store or hold data. Diskette drives, hard drives and CD-ROM drives are all block devices. But that’s not a problem when using lsblk as you can simply add more columns e.g type (device type) and/or tran (device transport type) etc:

lsblk --nodeps -no name,serial,type,tran
sda  0000000012400917BA30     disk sata
sdb  0000000012400917BA96     disk sata
sr0  4B583242334C453233353320 rom  usb

Method 4

By using hdparm
you can see your Harddisk serial number from terminal.

Open your terminal and type as

 hdparm -I /dev/sd?|grep -E "Number|/dev"

Method 5

$ ls -al /dev/disk/by-id/*sda*

This will show you the serial number against the familiar disk name.

Method 6

Easiest way I know (does not require root):

inxi -Dplxx

That outputs all disks, their serials, and any extra info. -p adds partitions. -l adds labels. -u adds UUID for the partitions.

Plus it’s a lot easier to remember, heh.

Sample:

inxi -Dxx
Drives:    HDD Total Size: 810.2GB (42.9% used)
           ID-1: /dev/sdc model: ST3160827AS size: 160.0GB serial: 5MT2HMH6
           ID-2: /dev/sdb model: WDC_WD3200JD size: 320.1GB serial: WD-WCAMR1302926
           ID-3: /dev/sda model: ST380817AS size: 80.0GB serial: 4MR2EWBE
           ID-4: /dev/sdd model: ST3250824AS size: 250.1GB serial: 9ND08GKX

Note that this filters out optical drives. To see optical data:

inxi -Dxxd 
Drives:    HDD Total Size: 810.2GB (42.9% used)
           ID-1: /dev/sdc model: ST3160827AS size: 160.0GB serial: 5MT2HMH6
           ID-2: /dev/sdb model: WDC_WD3200JD size: 320.1GB serial: WD-WCAMR1302926
           ID-3: /dev/sda model: ST380817AS size: 80.0GB serial: 4MR2EWBE
           ID-4: /dev/sdd model: ST3250824AS size: 250.1GB serial: 9ND08GKX
           Optical-1: /dev/sr0 model: LITE-ON DVDRW SOHW-1693S
           rev: KS09 dev-links: dvd,dvdrw
           Features: speed: 48x multisession: yes
           audio: yes dvd: yes rw: cd-r,cd-rw,dvd-r state: running
           Optical-2: /dev/sr1 model: LITE-ON LTR-52327S rev: QS0C dev-links: cdrom,cdrw
           Features: speed: 52x multisession: yes
           audio: yes dvd: no rw: cd-r,cd-rw state: running

Note that on my Debian system, lsblk does not show anything for serials, whether as root or user. Which is why inxi uses a much more reliable method to get that data.

lsblk --nodeps -o name,serial
NAME SERIAL
fd0  
sda  
sdb  
sdc  
sdd  
sr0  
sr1  

lsblk --version
lsblk from util-linux 2.25.2

As you can see, to lsblk, it thinks that an optical drive and floppy drive are also disks, which in a sense they are, though not really, since they don’t become disks until a disk is inserted. And it shows nothing for serial, it also by the way shows nothing for other values, like label. Definitely a bug since this data is available to the system, that’s where inxi gets it, direct.

Method 7

I also like using ls -l /dev/disk/by-id because it’ll show a disk’s WWN if available. The WWN is usually printed on the disk’s label, so it’s easy to identify.

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cfee3e3f8ccffe9fefae9fe">[email protected]</a> (16:27:58):~# ls -l /dev/disk/by-id
total 0
lrwxrwxrwx 1 root root  9 Dec 20 01:51 ata-Samsung_SSD_850_EVO_250GB_S3PZNF0JB57579N -> ../../sda
lrwxrwxrwx 1 root root 10 Dec 20 01:51 ata-Samsung_SSD_850_EVO_250GB_S3PZNF0JB57579N-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Dec 20 01:51 ata-Samsung_SSD_850_EVO_250GB_S3PZNF0JB57579N-part2 -> ../../sda2
...
lrwxrwxrwx 1 root root  9 Dec 20 01:51 wwn-0x50014ee25ffd0a5c -> ../../sdc
lrwxrwxrwx 1 root root  9 Dec 20 01:51 wwn-0x50014ee2b554c0b4 -> ../../sdb
lrwxrwxrwx 1 root root  9 Dec 20 01:51 wwn-0x5002538d427700f0 -> ../../sda

Method 8

ls -al /dev/disk/by-id/ | grep sdX | grep wwn | awk '{print $9'}

This will show the wwn-id for the disk. The awk filter may need to be adjusted depending on the OS distribution and version.
I needed a scripted solution to read the wwn-id, which is needed for Pacemaker disk fencing.
If partitions (/dev/sdX1 e.g.) have already been created another grep is needed to filter the output:

ls -al /dev/disk/by-id/ | grep sdX | grep wwn | grep -v sdX1 | awk '{print $9'}

Method 9

On FreeBSD, one could find that information in /var/run/dmesg.

On systems with ATA/SATA disks, look for ada devices:

$ grep ^ada /var/run/dmesg.boot  | grep -i serial
ada0: Serial Number WD-WMC1S5694795
ada1: Serial Number WD-WMC1S5688675

On other systems, disk devices may be found as da devices, so grep ^da .... instead.

Rather more comprehensive output can be found via diskinfo, but each disk device must be queried explicitly:

$ diskinfo -v ada0
ada0
    512             # sectorsize
    1000204886016   # mediasize in bytes (932G)
    1953525168      # mediasize in sectors
    4096            # stripesize
    0               # stripeoffset
    1938021         # Cylinders according to firmware.
    16              # Heads according to firmware.
    63              # Sectors according to firmware.
    WDC WD10EZEX-75ZF5A0    # Disk descr.
    WD-WMC1S5694795 # Disk ident.
    ahcich0         # Attachment
    id1,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41242f22012f72717770777977727778777477257271">[email protected]</a>/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30444940557000">[email protected]</a>/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3908f8c97a3d2">[email protected]</a>/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88d84858c8d9b8ba8bb84879c">[email protected]</a>_00 # Physical path
    No              # TRIM/UNMAP support
    Unknown         # Rotation rate in RPM
    Not_Zoned       # Zone Mode

Although each disk must be named explicitly, globbing is allowed, such as diskinfo ada{0,1}.

Method 10

strings /sys/block/${dev_name}/device/vpd_pg80 | tr -d '40111215'

Attach the ‘Device Identification’ VPD page (0x83) and the ‘Unit Serial Number’ VPD page (0x80) to a SCSI device structure. This information can be used to identify the device uniquely.

torvalds/linux/blob/master/drivers/scsi/scsi.c#L428


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x