118

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?

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
Raja G
  • 5,749
  • 12
  • 44
  • 67
  • expanding on [this answer](https://unix.stackexchange.com/a/207386/198636) (I may not comment yet), you can even do it without grep and listing only the property value with ``` udevadm info --query=property --property=ID_SERIAL_SHORT --value --name=/dev/sda ``` – j-hap Jul 20 '23 at 04:37

10 Answers10

128

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.

See the man page of udevadm for more.

Pablo A
  • 2,307
  • 1
  • 22
  • 34
Johann
  • 1,395
  • 1
  • 9
  • 8
  • 2
    Best solution if your hard disk has died completely. Other methods don't work. – niieani Mar 30 '16 at 16:35
  • 2
    `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`. – ron May 12 '17 at 18:03
  • @ron Interesting! Do you have any references that define wwn? – Johann May 12 '17 at 18:13
  • 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 May 15 '17 at 17:14
  • I had recently asked this: https://unix.stackexchange.com/questions/364456/getting-hard-disk-drive-serial-number-at-the-terminal-not-the-wwn – ron May 15 '17 at 17:26
  • 1
    @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 ` – Johann Jun 19 '17 at 05:25
  • my RHEL7 is insistant to report the WWN only!! NOt sure whats up there... `$ udevadm info --query=all --name=/dev/sda | grep ID_SERIAL E: ID_SERIAL=350000c0f01e5dabc E: ID_SERIAL_SHORT=50000c0f01e5dabc ` I just realized it must be my PCIe3 SCSI card doing that!! Putting the WWN in a serial placeholder or something... – Brian Thomas Nov 16 '21 at 21:07
  • Can u do this based on the uuid of a hard drive? – Jono Jul 08 '22 at 15:35
77

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
Robert Jonczy
  • 1,053
  • 1
  • 6
  • 8
  • Thanks for the answer I have tested it. But its not giving the dev/sdXX . Please try to fix it. unless its good – Raja G Mar 27 '14 at 12:49
  • if you take out the 'grep' part, you will get the full info as in `hdparm -I /dev/sd?` – Loopo Jan 20 '16 at 13:46
  • 3
    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. – niieani Mar 30 '16 at 16:40
  • I had to install the (apt-ly named) `hdparm` package on ARM (Raspbian on Raspberry Pi). – EthernetCable Dec 13 '16 at 10:07
  • 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. – s.co.tt May 28 '19 at 15:09
  • `lshw` returns all disks. You can filter the output conveniently using `jq`, like this: `lshw -C disk -json | jq 'select(.logicalname="/dev/sda")'`. – DustWolf Sep 25 '20 at 20:16
  • something hosed on my end, reports this for all 9 drives.. `$ hdparm -I /dev/sda | grep 'Serial\ Number' SG_IO: bad/missing sense data, sb[]: 70 00 05 00 00 00 00 16 00 00 00 00 20 00 00 00 00 00 80 0a 11 01 00 00 00 00 00 00 00 00 00 00 SG_IO: bad/missing sense data, sb[]: 70 00 05 00 00 00 00 16 00 00 00 00 20 00 00 00 00 00 80 0a 11 01 00 00 00 00 00 00 00 00 00 00 HDIO_DRIVE_CMD(identify) failed: Input/output error ` – Brian Thomas Nov 16 '21 at 21:10
61

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
don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • 10
    Note that this appears to require `lsblk` from util-linux version 2.24 or higher: https://github.com/karelzak/util-linux/commit/460c7afb79075bd5b39e7d4bc153aa41c939bab3 – Johann Jun 03 '15 at 21:32
  • How to retrieve hard disk serial when I use a VM ubuntu? The above commands return nothing on this situation – Benyamin Jafari Aug 07 '19 at 12:20
  • @BenyaminJafari the whole point of a VM is to separate the OS from the host hardware. – localhost Jan 06 '21 at 00:21
  • 1
    this also works if you're using software raid, thanks. – Flo Jan 04 '23 at 07:52
12

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"
Raja G
  • 5,749
  • 12
  • 44
  • 67
  • 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 – syntaxerror Mar 19 '15 at 11:09
10
$ ls -al /dev/disk/by-id/*sda*

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

Ed Neville
  • 1,330
  • 10
  • 11
  • 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. – Wildcard Nov 15 '15 at 18:59
  • 1
    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. – hoijui Sep 30 '17 at 08:58
  • 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 ... ` – Brian Thomas Nov 16 '21 at 21:14
4

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.

Lizardx
  • 2,990
  • 16
  • 18
  • 3
    The last part of your post is wrong, `lsblk` does not think they're disks, it simply lists all _block devices_. See my updated post for a clarification. As to not showing information - this is because you're using debian which is notorius for their `lsblk` behaviour. It works absolutely fine on archlinux so _definitely not a bug_. Also, `inxi` is just a bash script that uses other commands to get that info; it doesn't get anything "directly". – don_crissti Nov 15 '15 at 19:11
  • Technically correct, but in the realm of normal speech, disks is a decent way to communicate this concept. A behavior failing is of course a bug, it's irrelevant what causes is, so your comment that a buggy lsblk isn't a bug makes basically no sense. Whether the bug is debian created or not doesn't alter the fact it's a bug. Directly means without mediation, ie, from the file system, which is where inxi gets serial information. An answer should reflect the overall gnu/linux landscape, so saying for example lsblk works except in debian/buntu has no value since most users will see it not work. – Lizardx Nov 15 '15 at 20:59
  • 1
    This part of the answer is incorrect: *lsblk, it thinks that an optical drive and floppy drive are also disks*. In fact, lsblk lists block devices (which include hard disks, SSDs, floppy disk drives, optical disk drives, LVM logical volumes etc.), and lsblk doesn't distinguish between disks and non-disks. – pts Apr 23 '18 at 22:31
  • 1
    FYI on Debian buster, `lsblk --nodeps -o name,serial` does display the serial numbers, I can't reproduce the bug. – pts Apr 23 '18 at 22:34
  • lsblk is improving, but there are still some subtle issues, I'm still not going to use it as a primary data source for tools I make, but I am using it now as a secondary source, but we've already encountered inexplicable bugs using it, working on one now. – Lizardx Apr 23 '18 at 23:58
  • This is awesome answer. Unfortunately i think my SCSI card is hosed. this works for my WD SATA drives, but not the SAS drives ` ID-2: /dev/sdb vendor: Seagate model: ST3000NM0035 E size: 2.73 TiB speed: serial: N/A temp: 25 C ID-3: /dev/sdc vendor: Seagate model: ST3000NM0035 E size: 2.73 TiB speed: serial: N/A temp: 29 C ID-4: /dev/sdd vendor: Seagate model: ST3000NM0043 E size: 2.73 TiB speed: serial: N/A temp: 25 C ...` – Brian Thomas Nov 16 '21 at 21:27
3

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.

root@server (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
user208145
  • 2,437
  • 2
  • 20
  • 21
  • this was best for me as it helped ID the drives within my RAID array so when one fails I know which one to pull – sdjuan Mar 26 '23 at 18:17
2
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'}
kemotep
  • 5,140
  • 7
  • 19
  • 35
tschakka
  • 21
  • 1
  • maybe issue with my SCSI card? `$ ls -al /dev/disk/by-id/ | grep sdh | grep wwn | awk '{print $9'} 56 } wwn-0x50000c0f01e63ff0 57 wwn-0x50000c0f01e63ff0-part1 58 #adata wwn-0x50000c0f01e63ff0-part9 ` – Brian Thomas Nov 16 '21 at 21:16
1
strings /sys/block/${dev_name}/device/vpd_pg80 | tr -d '\040\011\012\015'

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

user164485
  • 11
  • 2
1

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,enc@n3061686369656d30/type@0/slot@1/elmdesc@Slot_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}.

Jim L.
  • 7,188
  • 1
  • 13
  • 25