Harddisk serial number from terminal?
I have multiple hard disks which get connected to my server and I’m not sure which one is what in the view of sdXY. If I could see the serial numbers of my hard disks from terminal, I could easily identify them. Is there any way I can get the serial numbers from the terminal?
10 Answers 10
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 printing the serial number.
grep ID_SCSI_SERIAL may be what actually gives the serial number of the drive, as opposed to the world wide name (wwn) reported under ID_SERIAL .
only reference is my first hand experience with WD, Seagate, HGST hard drives comparing values at the terminal to what is written on the label. Hard drives are primarily enterprise/data center grade versus consumer desktop.
@ron Just tested this again. My version of udevadm (systemd 229) reports an ID_WWN field. For the drives in my system, there is also no ID_SCSI_SERIAL nor anything analogous (no ID_ATA_SERIAL ): E: ID_SERIAL=TOSHIBA-TR150_23SC51E8J2BI . E: ID_SERIAL_SHORT=23SC51E8J2BI . E: ID_WWN=0x5e83a97200463ff3 . E: ID_WWN_WITH_EXTENSION=0x5e83a97200463ff3
# hdparm -I /dev/sd? | grep 'Serial\ Number'
EDIT: You can also use lshw or smartctl
If you are missing those tools, just install following packages
# apt-get install hdparm # apt-get install smartmontools # apt-get install lshw
Thanks for the answer I have tested it. But its not giving the dev/sdXX . Please try to fix it. unless its good
Does not work if your hard disk has died completely and you’re looking for the serial number of the faulty unit. Use @Johann’s method instead.
Use hdparm -I /dev/sd? | grep —before-context=4 ‘Serial\ Number’ to correlate the serial number with the device. It looks like the original question requests that result.
Device 1 name and corresponding serial number:
lsblk --nodeps -o name,serial
NAME SERIAL sda 0000000012400917BA30 sdb 0000000012400917BA96
add -n if you don’t want to print the header line:
lsblk --nodeps -no name,serial
sda 0000000012400917BA30 sdb 0000000012400917BA96
Pass device as argument to get only the serial number of a specific device:
lsblk --nodeps -no serial /dev/sda
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
Note that this appears to require lsblk from util-linux version 2.24 or higher: github.com/karelzak/util-linux/commit/…
How to retrieve hard disk serial when I use a VM ubuntu? The above commands return nothing on this situation
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"
Well, but you need to be superuser to use the -I option in hdparm . I would not want that either and prefer a way how to read out the ser # without root permissions. This is why I’ve upvoted don_crissti’s solution only. – syntaxerror 57 secs ago
This will show you the serial number against the familiar disk name.
This is a clever approach but doesn’t work on my virtual box. It looks like the contents of the by-id dir are just symlinks, so ls -al /dev/disk/by-id/ will show you what you need anyway.
This also worked for me on a debian live boot system, while all the other tools are not available from scratch, without setting up internet and apt-getting them.
even this fails on my system as mentioned above, then using the basic, as @Wildcard mentioned you can see my SCSI card may just be having a bad day.. $ ls -al /dev/disk/by-id | grep sdh lrwxrwxrwx. 1 root root 9 Nov 14 22:21 scsi-350000c0f01e63ff0 -> ../../sdh lrwxrwxrwx. 1 root root 10 Nov 14 22:21 scsi-350000c0f01e63ff0-part1 -> ../../sdh1 lrwxrwxrwx. 1 root root 10 Nov 14 22:21 scsi-350000c0f01e63ff0-part9 -> ../../sdh9 lrwxrwxrwx. 1 root root 9 Nov 14 22:21 wwn-0x50000c0f01e63ff0 -> ../../sdh .
Easiest way I know (does not require root):
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.
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.
Узнать серийный номер HDD при помощи утилит Linux
Заметка по полезным утилитам Linux, которые могут раскрыть данные о железе. Конкретно в этом случае, мне нужно было узнать серийные номера жестких дисков, установленных в серверах.
Model=Hitachi HUA722020ALA330, FwRev=JKAOA3EA, SerialNo=JK11A8XXXXXX
Config=< HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs >
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=56
BuffType=DualPortCache, BuffSize=29999kB, MaxMultSect=16, MultSect=16
.
Эта утилита дает более общий вывод по всем шинам
*-disk
description: ATA Disk
product: Hitachi HUA72202
vendor: Hitachi
physical id: 0.0.0
bus info: scsi@1:0.0.0
logical name: /dev/sdb
version: A3EA
serial: JK11A8XXXXXX
size: 1863GiB (2TB)
capabilities: partitioned partitioned:dos
configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512
3. smartctl
Smartctl из пакета smartmontools в основном применял для контроля здоровья HDD, получить серийный номер, можно так, вместо ключа -a, используется -x.
-x, —xall — Show all information for device
=== START OF INFORMATION SECTION ===
Model Family: Hitachi Ultrastar A7K2000
Device Model: Hitachi HUA722020ALA330
Serial Number: JK11A8XXXXXXJK11A8XXXXXX
LU WWN Device Id: 5 000cca 221f48cb1
Firmware Version: JKAOA3EA
User Capacity: 2 000 398 934 016 bytes [2,00 TB]
Sector Size: 512 bytes logical/physical
Rotation Rate: 7200 rpm
Form Factor: 3.5 inches
Device is: In smartctl database [for details use: -P show]
ATA Version is: ATA8-ACS T13/1699-D revision 4
SATA Version is: SATA 2.6, 3.0 Gb/s
border / get_sn.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include |
#include |
#include |
#include |
#include |
#include |
#include |
#define SCSI_TIMEOUT 5000 /* ms */ |
static char * device = «/dev/sda» ; |
int scsi_io ( int fd , unsigned char * cdb , unsigned char cdb_size , int xfer_dir , |
unsigned char * data , unsigned int * data_size , |
unsigned char * sense , unsigned int * sense_len ) |
sg_io_hdr_t io_hdr ; |
memset ( & io_hdr , 0 , sizeof ( sg_io_hdr_t )); |
io_hdr . interface_id = ‘S’ ; |
/* CDB */ |
io_hdr . cmdp = cdb ; |
io_hdr . cmd_len = cdb_size ; |
/* Where to store the sense_data, if there was an error */ |
io_hdr . sbp = sense ; |
io_hdr . mx_sb_len = * sense_len ; |
* sense_len = 0 ; |
/* Transfer direction, either in or out. Linux does not yet |
support bidirectional SCSI transfers ? |
*/ |
io_hdr . dxfer_direction = xfer_dir ; |
/* Where to store the DATA IN/OUT from the device and how big the |
buffer is |
*/ |
io_hdr . dxferp = data ; |
io_hdr . dxfer_len = * data_size ; |
/* SCSI timeout in ms */ |
io_hdr . timeout = SCSI_TIMEOUT ; |
if ( ioctl ( fd , SG_IO , & io_hdr ) < 0 ) |
perror ( «SG_IO ioctl failed» ); |
return -1 ; |
> |
/* now for the error processing */ |
if (( io_hdr . info & SG_INFO_OK_MASK ) != SG_INFO_OK ) |
if ( io_hdr . sb_len_wr > 0 ) |
* sense_len = io_hdr . sb_len_wr ; |
return 0 ; |
> |
> |
if ( io_hdr . masked_status ) |
printf ( «status=0x%x\n» , io_hdr . status ); |
printf ( «masked_status=0x%x\n» , io_hdr . masked_status ); |
return -2 ; |
> |
if ( io_hdr . host_status ) |
printf ( «host_status=0x%x\n» , io_hdr . host_status ); |
return -3 ; |
> |
if ( io_hdr . driver_status ) |
printf ( «driver_status=0x%x\n» , io_hdr . driver_status ); |
return -4 ; |
> |
return 0 ; |
> |
int scsi_inquiry_unit_serial_number ( int fd ) |
unsigned char cdb [] = < 0x12 , 0x01 , 0x80 , 0 , 0 , 0 >; |
unsigned int data_size = 0x00ff ; |
unsigned char data [ data_size ]; |
unsigned int sense_len = 32 ; |
unsigned char sense [ sense_len ]; |
int res , pl , i ; |
cdb [ 3 ] = ( data_size >> 8 ) & 0xff ; |
cdb [ 4 ] = data_size & 0xff ; |
printf ( «INQUIRY Unit Serial Number:\n» ); |
res = scsi_io ( fd , cdb , sizeof ( cdb ), SG_DXFER_FROM_DEV , data , & data_size , sense , & sense_len ); |
if ( res ) |
printf ( «SCSI_IO failed\n» ); |
return -1 ; |
> |
if ( sense_len ) |
return -1 ; |
> |
/* Page Length */ |
pl = data [ 3 ]; |
/* Unit Serial Number */ |
printf ( «Unit Serial Number:» ); |
for ( i = 4 ; i < ( pl + 4 ); i ++ ) printf ( "%c" , data [ i ] & 0xff ); printf ( "\n" ); |
return 0 ; |
> |
int open_scsi_device ( const char * dev ) |
int fd , vers ; |
if (( fd = open ( dev , O_RDWR )) < 0 ) |
printf ( «ERROR could not open device %s\n» , dev ); |
return -1 ; |
> |
if (( ioctl ( fd , SG_GET_VERSION_NUM , & vers ) < 0 ) || ( vers < 30000 )) |
printf ( «/dev is not an sg device, or old sg driver\n» ); |
close ( fd ); |
return -1 ; |
> |
return fd ; |
> |
int main ( int argc , const char * argv []) |
int fd ; |
if ( argc > 1 ) |
device = argv [ 1 ]; |
> |
fd = open_scsi_device ( device ); |
if ( fd < 0 ) |
printf ( «Could not open SCSI device %s\n» , device ); |
_exit ( 10 ); |
> |
scsi_inquiry_unit_serial_number ( fd ); |
return 0 ; |
> |