Command method for viewing Linux device information using lshw

Although the lshw command (list hardware, read as "ls hardware") is far from one of the 50 Linux commands that everyone first learned, it can provide a lot of useful information about system hardware.

It extracts more information than you may know in a fairly easy to understand format. After seeing the description, (device) logical name, size, etc., you may understand how much information you can get.

This article will study the information given by lshw, but focus on disks and related hardware. The following is an example of lshw's output:

$ sudo lshw -C disk
  *-disk:0
       description: SCSI Disk
       product: Card Reader-1
       vendor: JIE LI
       physical id: 0.0.0
       bus info: scsi@4:0.0.0
       logical name: /dev/sdc
       version: 1.00
       capabilities: removable
       configuration: logicalsectorsize=512 sectorsize=512
     *-medium
          physical id: 0
          logical name: /dev/sdc

Note that you need to run the lshw command using sudo to make sure you get all the available information.

Although the above command requires the output of "disk" (it only contains one of the five entries in the original output), the output here is not a hard disk, but a card reader - a kind of disk. Note that the system names this device / dev/sdc.

Similar information is available on the main disk of the system:

*-disk
        description: ATA Disk
        product: SSD2SC120G1CS175
        physical id: 0
        bus info: scsi@0:0.0.0
         logical name: /dev/sda         <==here
        version: 1101
        serial: PNY20150000778410606
        size: 111GiB (120GB)
        capabilities: partitioned partitioned:dos
        configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=
           f63b5929

This hard disk is / dev/sda. The hard disks on this system are displayed as ATA disks. ATA is a disk drive implementation that integrates the controller and the disk body.

To get a brief list of "disk" devices, run the following command. Note that two of these devices are listed twice, so we still see five disk devices.

$ sudo lshw -short -C disk
H/W path               Device      Class          Description
=============================================================
/0/100/1d/1/1/0.0.0    /dev/sdc    disk           Card Reader-1
/0/100/1d/1/1/0.0.0/0  /dev/sdc    disk
/0/100/1d/1/1/0.0.1    /dev/sdd    disk           2
/0/100/1d/1/1/0.0.1/0  /dev/sdd    disk
/0/100/1f.2/0          /dev/sda    disk           120GB SSD2SC120G1CS175
/0/100/1f.2/1          /dev/cdrom  disk           DVD+-RW GSA-H73N
/0/100/1f.5/0.0.0      /dev/sdb    disk           500GB SAMSUNG HE502HJ

If you decide to view all the devices on the system, please sit down; You will get a list that contains much more things than you usually think of as "devices". Here is an example. This is a "short" (little information) list:

$ sudo lshw -short
[sudo] password for shs:
H/W path               Device      Class          Description
=============================================================
                                   system         Inspiron 530s
/0                                 bus            0RY007
/0/0                               memory         128KiB BIOS
/0/4                               processor      Intel(R) Core(TM)2 Duo CPU
/0/4/a                             memory         32KiB L1 cache
/0/4/b                             memory         6MiB L2 cache
/0/24                              memory         6GiB System Memory
/0/24/0                            memory         2GiB DIMM DDR2 Synchronous 667
/0/24/1                            memory         1GiB DIMM DDR2 Synchronous 667
/0/24/2                            memory         2GiB DIMM DDR2 Synchronous 667
/0/24/3                            memory         1GiB DIMM DDR2 Synchronous 667
/0/1                               generic
/0/10                              generic
/0/11                              generic
/0/12                              generic
/0/13                              generic
/0/14                              generic
/0/15                              generic
/0/17                              generic
/0/18                              generic
/0/19                              generic
/0/2                               generic
/0/20                              generic
/0/100                             bridge         82G33/G31/P35/P31 Express DRAM
/0/100/1                           bridge         82G33/G31/P35/P31 Express PCI
/0/100/1/0                         display        Caicos [Radeon HD 6450/7450/84
/0/100/1/0.1                       multimedia     Caicos HDMI Audio [Radeon HD 6
/0/100/19              enp0s25     network        82562V-2 10/100 Network Connec
/0/100/1a                          bus            82801I (ICH9 Family) USB UHCI
/0/100/1a/1            usb3        bus            UHCI Host Controller
/0/100/1a.1                        bus            82801I (ICH9 Family) USB UHCI
/0/100/1a.1/1          usb4        bus            UHCI Host Controller
/0/100/1a.1/1/2                    input          Rock Candy Wireless Keyboard
/0/100/1a.2                        bus            82801I (ICH9 Family) USB UHCI
/0/100/1a.2/1          usb5        bus            UHCI Host Controller
/0/100/1a.2/1/2                    input          USB OPTICAL MOUSE
/0/100/1a.7                        bus            82801I (ICH9 Family) USB2 EHCI
/0/100/1a.7/1          usb1        bus            EHCI Host Controller
/0/100/1b                          multimedia     82801I (ICH9 Family) HD Audio
/0/100/1d                          bus            82801I (ICH9 Family) USB UHCI
/0/100/1d/1            usb6        bus            UHCI Host Controller
/0/100/1d/1/1          scsi4       storage        CD04
/0/100/1d/1/1/0.0.0    /dev/sdc    disk           Card Reader-1
/0/100/1d/1/1/0.0.0/0  /dev/sdc    disk
/0/100/1d/1/1/0.0.1    /dev/sdd    disk           2
/0/100/1d/1/1/0.0.1/0  /dev/sdd    disk
/0/100/1d.1                        bus            82801I (ICH9 Family) USB UHCI
/0/100/1d.1/1          usb7        bus            UHCI Host Controller
/0/100/1d.2                        bus            82801I (ICH9 Family) USB UHCI
/0/100/1d.2/1          usb8        bus            UHCI Host Controller
/0/100/1d.7                        bus            82801I (ICH9 Family) USB2 EHCI
/0/100/1d.7/1          usb2        bus            EHCI Host Controller
/0/100/1d.7/1/2                    multimedia     USB  Live camera
/0/100/1e                          bridge         82801 PCI Bridge
/0/100/1e/1                        communication  HSF 56k Data/Fax Modem
/0/100/1f                          bridge         82801IR (ICH9R) LPC Interface
/0/100/1f.2            scsi0       storage        82801IR/IO/IH (ICH9R/DO/DH) 4
/0/100/1f.2/0          /dev/sda    disk           120GB SSD2SC120G1CS175
/0/100/1f.2/0/1        /dev/sda1   volume         111GiB EXT4 volume
/0/100/1f.2/1          /dev/cdrom  disk           DVD+-RW GSA-H73N
/0/100/1f.3                        bus            82801I (ICH9 Family) SMBus Con
/0/100/1f.5            scsi3       storage        82801I (ICH9 Family) 2 port SA
/0/100/1f.5/0.0.0      /dev/sdb    disk           500GB SAMSUNG HE502HJ
/0/100/1f.5/0.0.0/1    /dev/sdb1   volume         433GiB EXT4 volume
/0/3                               system         PnP device PNP0c02
/0/5                               system         PnP device PNP0b00
/0/6                               storage        PnP device PNP0700
/0/7                               system         PnP device PNP0c02
/0/8                               system         PnP device PNP0c02
/0/9                               system         PnP device PNP0c01

Run the following command to list the device categories and count the number of devices in each category.

$ sudo lshw -short | awk '{print substr($0,36,13)}' | tail -n +3 | sort | uniq -c
      4 bridge
     18 bus
      1 communication
      7 disk
      1 display
     12 generic
      2 input
      8 memory
      3 multimedia
      1 network
      1 processor
      4 storage
      6 system
      2 volume

Note: using the awk command above to select the Class column from the output of lshw is realized in this way: use $0 (select the complete line), but only take the substring starting from the correct position (the 36th character). Because no entry in the "category" is longer than 13 characters, the substring ends there. The tail -n +3 part of the command removes the title and the following = = = = =, so only the 14 device types are included in the final list.

(LCTT translation note: the awk part of the above command starts from the 36th character when selecting the substring. This number basically depends on the length of the longest device logical name, so it may be different in different system environments. For example, when your system has NVMe SSD, it may need to be changed to 41.)

You will find that when the - short option is not used, each disk device will have about 12 lines of output, including logical names such as / dev/sda, disk size and type, etc.

$ sudo lshw -C disk
[sudo] password for shs:
  *-disk:0
       description: SCSI Disk
       product: Card Reader-1           <== card reader? 
       vendor: JIE LI
       physical id: 0.0.0
       bus info: scsi@4:0.0.0
       logical name: /dev/sdc
       version: 1.00
       capabilities: removable
       configuration: logicalsectorsize=512 sectorsize=512
     *-medium
          physical id: 0
          logical name: /dev/sdc
  *-disk:1
       description: SCSI Disk
       product: 2
       vendor: AC4100 -
       physical id: 0.0.1
       bus info: scsi@4:0.0.1
       logical name: /dev/sdd
       capabilities: removable
       configuration: logicalsectorsize=512 sectorsize=512
     *-medium
          physical id: 0
          logical name: /dev/sdd
  *-disk
       description: ATA Disk
       product: SSD2SC120G1CS175
       physical id: 0
       bus info: scsi@0:0.0.0
       logical name: /dev/sda           <== Main disk
       version: 1101
       serial: PNY20150000778410606
       size: 111GiB (120GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=f63b5929
  *-cdrom                               <== Also called /dev/sr0
       description: DVD writer
       product: DVD+-RW GSA-H73N
       vendor: HL-DT-ST
       physical id: 1
       bus info: scsi@1:0.0.0
       logical name: /dev/cdrom
       logical name: /dev/cdrw
       logical name: /dev/dvd
       logical name: /dev/dvdrw
       logical name: /dev/sr0
       version: B103
       serial: [
       capabilities: removable audio cd-r cd-rw dvd dvd-r
       configuration: ansiversion=5 status=nodisc
  *-disk
       description: ATA Disk
       product: SAMSUNG HE502HJ
       physical id: 0.0.0
       bus info: scsi@3:0.0.0
       logical name: /dev/sdb           <== Secondary disk
       version: 0002
       serial: S2B6J90B501053
       size: 465GiB (500GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=7e67ccf3

summary

The lshw command provides some information that many of us don't normally handle, but even if you only use some of it, it's good to know how much information is available.

Added by mohvmark on Mon, 22 Nov 2021 20:47:32 +0200