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
How to get drive SERIAL number from mountpoint?
What is a simple console/CLI command I can use to get the serial number ( X55MM4827123 in this case) when I provide the mountpoint ( /run/media/main/mydrive in this case)? The answer doesn’t necessarily have to use lsblk , but it should hopefully be concise and ideally use just simple bash scripting.
2 Answers 2
These options will print your disk serial without headings: lsblk -n -o SERIAL /dev/sda
Options explained, and that can be found at the manpages:
-n, --noheadings Do not print a header line.
This option will remove headers like Serial: from the command output.
-o, --output list Specify which output columns to print. Use --help to get a list of all supported columns.
Select what information to print about disks.
This will be enough to bring you only the serial. Unfortunately, lsblk does not work with mountpoints, since the serial is an attribute of the disk, not the partition. Taking a look at the synopsis :
Synopsis lsblk [options] lsblk [options] device.
You will have to make a huge effort to extract from the mountpoint the partition, trim the disk information and then, supply it as parameter to lsblk . This will probably do the trick:
mount| grep /run/media/main/mydrive | awk 'NR==1'|sed 's/8*//g'
mount will list all mountpoints, grep will get the line of your mountpoint, awk will get the partition column and sed will remove numbers, that are the representation of partitions. Summing all:
lsblk -n -o SERIAL `mount| grep /run/media/main/mydrive | awk 'NR==1'|sed 's/9*//g'`
The sed is replacing the numbers, so the mount/grep/awk command results in /dev/mapper/luks-ddc-aa-d—bdf , even though luks should have a uuid after it.
Oh boy, Forgot that you have a disk encryption based on luks . I’ve tested with LVM and plain devices but not with those. Give me some time and i will fix this. But now, we have another problem that is: Nested devices. You have a Luks device, created on top of a disk device. This will be hard to fix, since we will have to probe all the possibilities( mdraid , sda , lvm , luks . )
As far as I understand, the goal is to give the command input as mountpoint and output serial number. Well, it’s possible with a small script that uses a little bit of awk magic and smartmontools package.
$ ./mount2serial.sh / [sudo] password for xieerqi: Serial Number: 4G7AA3Q1HSZ4HH4YN $ ./mount2serial.sh /mnt/HDD [sudo] password for xieerqi: Serial Number: 4O75CEXFLML9M
#!/usr/bin/env bash if [ "$1" = "/" ] then dev=$(awk -v mount="$1" '$2 == mount' /proc/self/mounts) else dev=$(awk -v mount="$1" '$0 ~ mount' /proc/self/mounts) fi sudo smartctl -i "$dev" | grep 'Serial Number:'
Since you do get serial number in lsblk , and GNU version 1 of lsblk can output JSON data, I’ve put together a Python script that works as so 2 :
$ ./mount2serial.py '/mnt/ubuntu' 4O75CEXFLML9M xie@xie-PC:~$ ./mount2serial.py '/' B4VOM8OEIZIHF
#!/usr/bin/env python3 import json import subprocess import sys lsblk = subprocess.run(['lsblk','-J','-o','NAME,SERIAL,MOUNTPOINT'],stdout=subprocess.PIPE) for dev in json.loads(lsblk.stdout.decode())['blockdevices']: serial = '' # find serial number of current block device for key,value in dev.items(): if key == 'serial': serial = value break # we don't need to iterate through everything in dev.items() for child in dev['children']: if child['mountpoint'] == sys.argv[1]: print(serial) sys.exit(0) # if nothing is found we end up here with exit status 1 and nothing printed sys.exit(1)
1. I’ve no idea if non-GNU versions of lsblk exist, but if they do — leave a comment
2. Serial numbers shown are random strings generated from /dev/urandom for demo purposes