Press "Enter" to skip to content

How to Use lshw in Linux (With a Practical Example)

What if there was a command which would let you, in an easy-to-read format, view all of the hardware in your Linux system? Welcome to lshw, the great tool often overlooked even by seasoned Linux professionals.

What Is lshw?

lshw, A utility, or better tool, installed by default when you install Linux, provides you with a listing of hardware as found in your system. One can remember lshw by thinking about ls (the command which lets you list directory contents) and hw for hardware.

While the tool has a good number of formatting options, gives detailed output for the devices it finds and can detect a solid number of device types, even seasoned Linux professionals may not have heard of it. Perhaps other tools have taken too much of a front posting on search engines and similar.

Currently, the tool can detect DMI (x86 and IA-64), OpenFirmware device tree (PowerPC), PCI/AGP, CPUID (x86), IDE/ATA/ATAPI, PCMCIA, SCSI, and USB devices.

You can even install a graphical version of the same tool using a separate package, namely lshw-gtk. While lshw does not need to be installed and is included by Linux by default if you are interested in exploring your hardware in a GUI rather than at the command line (which this article will focus on), install lshw-gtk:

Installing lshw-gtk

To install lshw-gtk on your Debian/Apt based Linux distribution (Like Ubuntu and Mint), execute the following command in your terminal:

sudo apt install lshw-gtk

Advertisement

To install lshw-gtk on your RedHat/Yum based Linux distribution (Like RHEL, Centos and Fedora), execute the following command in your terminal:

sudo yum install lshw-gtk

If you end up installing lshw-gtk, please note that there are a few oddities with this tool that may not be expected. For starters, it is recommend that you use sudo lshw-gtk at your terminal prompt to start lshw-gtk as otherwise not all hardware info may be available.

Next, when you have started the tool, you will want to hit the Refresh button near the top as lshw-gtk does not scan hardware by default, but only on demand.

Finally, when your hardware is scanned a few seconds later, the output seems to be missing except for a single Desktop Computer in the leftmost column. Double click this, then double click, for example Motherboard in the second column, and so on. In my own experience, I have found this particular GUI tool to be, while working, a bit limited.

lshw-gtk main dialog window after scanning

Do you know the difference between a crash and error and an assert? Our Asserts, Errors, and Crashes article explains the difference.

Exploring lshw at the Linux Terminal

When you execute lshw at your Linux Terminal (which can usually be started by pressing the Windows Key + T on your keyboard, or by pressing the operating system’s main icon (like the green circle in Linux Mint) and typing terminal) you will see a long and verbose output of hardware. lshw did what it was instructed to do; list all hardware.

Advertisement

Let’s make this work a bit better by limiting the output to what we are interested in. Firstly we should explore the hardware classes of output available to us.

We can do this with lshw -short, which at the same time will render a shorter, much more readable overview of our hardware. Still, the output list can be 1-2 pages long, depending on your terminal settings.

We should also at this point remember to always use lshw with sudo. This will ensure that the output will not be incomplete or inaccurate. Running lshw as sudo should be safe (famous last words!) as lshw is included by default in the Linux operating system core distribution files; one would hope it has been thoroughly evaluated.

sudo lshw -short | head -n6

lshw output in short format (using the -short option) also showing device classes, fist 6 lines of output

Here we requested a short list of all hardware using lshw -short, executed as the root superuser using sudo, and limited to the first six lines of output using head -n6. We piped the output from lshw to head by using a Linux pipe | which is a very handy method of post-parsing information rendered by a tool into some almost any other format required.

Running this in VirtualBox, we see an immediately interesting bit of information: lshw is aware that the system and bus is VirtualBox based. We also see that 8GiB of memory was assigned to this VM (Virtual Machine).

Advertisement

Also importantly, we can see the Class of devices being listed, which is what we will use in our next lshw example. The device Class names used in lshw are straightforward and easy to remember; there is system, bus, memory, bridge, generic, multimedia, disk, input, volume, display, network, processor and potentially a few others.

Using a Device Class to Query with lshw

Now that we know the device Class names, let us use the same in combination with the -class option (which can be abbreviated to -C) to query for only specific devices which we are interested in. For example, what if we had an easy way to detect if the system we were using is a virtualized machine running under VirtualBox specifically?

sudo lshw -short -C system
sudo lshw -short -C system 2>/dev/null | grep -o 'VirtualBox'
if [ "$(sudo lshw -short -C system 2>/dev/null | grep -o 'VirtualBox')" == 'VirtualBox' ]; then echo 'We are using VirtualBox!'; else echo 'This is not a VirtualBox VM!'; fi

Using lshw to establish and test whether we are running inside VirtualBox

Here we executed multiple commands, each time building the command into more complexity. In the first command we simply query lshw in the same way as before, though this time with the extra -C system option to only list devices from the system class.

After briefly scanning (not shown in the screenshot, as the output disappears once the scanning is complete), the system class devices are shown, which in this case is the VirtualBox system.

In the next command, we grep (select) for only (thanks to the -o option) the word ‘VirtualBox’. If the output does not contain ‘VirtualBox’, the command will not yield any output.

We also added and stderr (standard error output) redirection to /dev/null using 2>/dev/null for the odd case where sudo lshw would render an error. This is a commonly used scripting technique when piping information from one command to another, though perhaps here there is little possibility of an error presenting itself (but not impossible).

Advertisement

n the final command, we insert this code into a subshell ($(...)) and then into an if statement. Basically, we are asking the Bash interpreter to check if the output of the second command matches ‘VirtualBox’.

If so, we can quite confidently say We are using VirtualBox!. If you execute this code on your machine, assuming you are using Bash, as your command line interpreter/shell, and that you are not using VirtualBox, the output should be This is not a VirtualBox VM! instead.

Wrapping up

In this article, we explored lshw and lshw-gtk, the Linux tools for obtaining detailed hardware and system information from the command line and from within a GUI on your desktop. After exploring lshw device classes, we also looked at directly using lshw information to establish whether we are running inside VirtualBox or not.

If you enjoyed reading this article, you may like to read How to Use strace to Trace System Calls and Signals next!

This post was originally published on this site

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *