25

I'm having problems to detect which one of my block devices is the hard drive. My system has a cd-rom drive, USB drives, and a single hard drive of unknown vendor/type.

How can I identify the hard drive with a linux command, script, or C application?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Allan
  • 4,562
  • 8
  • 38
  • 59
  • Relevant: http://unix.stackexchange.com/questions/4561/how-do-i-find-out-what-hard-disks-are-in-the-system – shuttle87 Dec 30 '14 at 16:32

3 Answers3

26
sudo lshw -class disk

will show you the available disks in the system

Daniel Fekete
  • 4,988
  • 3
  • 23
  • 23
  • Interesting... I haven't heard about this lshw program. Is it part of a standard install? My Debian server doesn't have it. Neither does my FreeBSD installation (but he's requesting linux, so BSD doesn't count) – Aleks G Aug 18 '11 at 12:52
  • @Aleks G: Perhaps you Debian installation is out of date then: http://packages.debian.org/search?keywords=lshw – janneb Aug 18 '11 at 13:37
  • My debian is definitely out of date - it was a standard install provided by a hosting company on a dedicated server about a year ago. The issue is, though, that if a standard install didn't include it, then he can't rely on it. – Aleks G Aug 18 '11 at 13:39
  • 3
    `bash: lshw: command not found` – Mr_and_Mrs_D Mar 09 '14 at 15:41
  • @Mr_and_Mrs_D: You may have to install it. I don't know about other distros, but it is packaged in Debian. – cgt Apr 29 '14 at 20:04
  • 1
    Please edit your answer with the last 2 comments – Mr_and_Mrs_D Apr 29 '14 at 20:15
  • For those googling years later, I had to do `sudo lshw -class storage` instead of `disk`. Possibly because I'm using SSDs and not spinning drives. – Samee Dec 24 '19 at 16:33
12

As shuttle87 pointed out, there are several other posts that answer this question. The solution that I prefer is:

root# lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL

NAME    TYPE    SIZE MOUNTPOINT FSTYPE            MODEL
sdb     disk    2.7T                              WDC WD30EZRX-00D
`-sdb1  part    2.7T            linux_raid_member 
  `-md0 raid1   2.7T /home      xfs               
sda     disk    1.8T                              ST2000DL003-9VT1
|-sda1  part  196.1M /boot      ext3              
|-sda2  part  980.5M [SWAP]     swap              
|-sda3  part    8.8G /          ext3            
|-sda4  part      1K                              
`-sda5  part    1.8T /samba     xfs               
sdc     disk    2.7T                              WDC WD30EZRX-00D
`-sdc1  part    2.7T            linux_raid_member 
  `-md0 raid1   2.7T /home      xfs               
sr0     rom    1024M                              CDRWDVD DH-48C2S

References:

Community
  • 1
  • 1
Lars Nordin
  • 2,785
  • 1
  • 22
  • 25
1

If you have a list of the plausible block devices, then the file

/sys/block/[blockdevname]/removable

will contain "1" if the device is removable, "0" if not removable.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • An exteranl USB hard drive is removable, but you can still boot a system from it, so in this case checking for 'removable' may not be sufficient. – Aleks G Aug 18 '11 at 12:06
  • Yes, I know. Reading between the lines a bit, I guess the OP wants the installation to go onto the (internal, non-removable) hard drive and not to reformat the USB stick that happened to be plugged in (perhaps because that's where the installation program resides).. – janneb Aug 18 '11 at 12:41
  • There'a difference between USB stick and USB harddrive. For example, on Windows, they will even be shown with different icons. If he's concerned with reformatting his installation USB stick, he can probably explicitly remove that particular device from the list. He knows where the path of his own executing program, so just check the /etc/mtab for the corresponding device and exclude it from the list. – Aleks G Aug 18 '11 at 12:49
  • Uhuh. Anyway, to get back on topic, my point is that for a system that has, per the OP's specification, cd-rom drives, usb keys, and a single hard drive, then checking the removable attribute as I explained is likely one way to solve the problem. – janneb Aug 18 '11 at 13:32