System insights with command-line tools: lscpu and lsusb

Photo by Hunter Harritt on Unsplash

Introduction

Fedora (and other common Linux setups out there) offers you an array of tools for managing, monitoring, and understanding the system. Among these tools are a series of commands that begin with ls (for “list”).

They provide easy insights into various aspects of the system’s hardware and resources. This article series gives you an intro and overview over many of them, starting with the simpler ones. The post will cover lscpu and lsusb.

lscpu – Display CPU information

The lscpu command gathers and displays information about the CPU architecture. It is provided by the util-linux package. The command gathers CPU information from multiple sources like /proc/cpuinfo and architecture-specific libraries (e.g. librtas on PowerPC):

$ lscpu

This command outputs information like the number of CPUs, threads per core, cores per socket, and the CPU family and model.

If asked, it outputs detailed CPU information in JSON format. This provides a structured view that is particularly useful for scripting and automation:

$ lscpu --extended --json

Advanced usage example

With the machine readable JSON output, you can extract information using jq (a powerful command-line tool that allows users to parse, filter, and manipulate JSON data efficiently and worth an article of its own). For example, the following command will extract the current MHz for each CPU:

export LANG=en_US.UTF-8
export LC_ALL="en_US.UTF-8"
lscpu --json --extended \
| jq '.cpus[] | {cpu: .cpu, mhz: .mhz}'

Let’s look at the single parts of the command:

  • export LANG=en_US.UTF-8 and export LC_ALL=”en_US.UTF-8″ are making sure that the output is not using localized numbers. For example, a German language setting can result in broken JSON output because of the use of commas in place of periods as floating point separators.
  • lscpu –json –extended generates the detailed CPU information in JSON format.
  • jq ‘.cpus[] | will iterate over each entry in the cpus array. The {cpu: .cpu, mhz: .mhz}‘ part constructs a new JSON object for each CPU entry showing the CPU number (cpu) and its current frequency in MHz (mhz).

Example output from a laptop operating in performance mode:

$ lscpu --json --extended \
| jq '.cpus[] | {cpu: .cpu, mhz: .mhz}'
{
"cpu": 0,
"mhz": 3700.0171
}
{
"cpu": 1,
"mhz": 3700.2241
}
{
"cpu": 2,
"mhz": 3700.1121
}
{
"cpu": 3,
"mhz": 3884.2539
}

and later in power saver mode:

$ lscpu --json --extended \
| jq '.cpus[] | {cpu: .cpu, mhz: .mhz}'
{
"cpu": 0,
"mhz": 1200.0580
}
{
"cpu": 1,
"mhz": 1200.0070
}
{
"cpu": 2,
"mhz": 1200.5450
}
{
"cpu": 3,
"mhz": 1200.0010
}

lsusb – Display USB Devices Information

The lsusb command displays detailed information about the USB buses in the system and the devices connected to them. It is provided by the usbutils package and helps users and system administrators easily view the configuration and the devices attached to their USB interfaces:

$ lsusb

This produces a list of all USB buses, devices connected to them, and brief information about each device, such as ID and manufacturer. This is particularly useful for a quick check of what devices are connected to the system and if you need the USB device ID for udev rules or the like.

Usage example and output

For those needing more detailed information about the USB devices, lsusb allows listing more detailed information:

$ lsusb | grep Fibocom
Bus 001 Device 013: ID 2cb7:0210 Fibocom L830-EB-00 LTE WWAN Modem

$ sudo lsusb -d 2cb7:0210 -v
Bus 001 Device 013: ID 2cb7:0210 Fibocom L830-EB-00 LTE WWAN Modem
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 239 Miscellaneous Device
bDeviceSubClass 2 [unknown]
bDeviceProtocol 1 Interface Association
bMaxPacketSize0 64
idVendor 0x2cb7 Fibocom
idProduct 0x0210 L830-EB-00 LTE WWAN Modem
bcdDevice 3.33
iManufacturer 1 FIBOCOM
iProduct 2 L830-EB-00
iSerial 3 004999010640000
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x00a1
bNumInterfaces 4
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xe0
Self Powered
Remote Wakeup
MaxPower 100mA
[ more output omitted for readability ]

Using the -v and -t options will tell lsusb to dump the physical USB device hierarchy as a tree including IDs. The following shows a detailed tree of all USB devices (here using a ThinkPad T480S), their types, speeds, and device classes. This is particularly useful for troubleshooting USB device issues:

$ lsusb -t -v
/:  Bus 001.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/12p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 001: Dev 002, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        ID 046d:c069 Logitech, Inc. M-U0007 [Corded Mouse M500]
    |__ Port 002: Dev 003, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        ID 046a:c098 CHERRY 
    |__ Port 002: Dev 003, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
        ID 046a:c098 CHERRY 
    |__ Port 003: Dev 004, If 0, Class=Chip/SmartCard, Driver=usbfs, 12M
        ID 058f:9540 Alcor Micro Corp. AU9540 Smartcard Reader
    |__ Port 005: Dev 005, If 0, Class=Video, Driver=uvcvideo, 480M
        ID 5986:2123 Bison Electronics Inc. 
    |__ Port 005: Dev 005, If 1, Class=Video, Driver=uvcvideo, 480M
        ID 5986:2123 Bison Electronics Inc. 
    |__ Port 006: Dev 013, If 0, Class=Communications, Driver=cdc_mbim, 480M
        ID 2cb7:0210 Fibocom L830-EB-00 LTE WWAN Modem
    |__ Port 006: Dev 013, If 1, Class=CDC Data, Driver=cdc_mbim, 480M
        ID 2cb7:0210 Fibocom L830-EB-00 LTE WWAN Modem
    |__ Port 006: Dev 013, If 2, Class=Communications, Driver=cdc_acm, 480M
        ID 2cb7:0210 Fibocom L830-EB-00 LTE WWAN Modem
    |__ Port 006: Dev 013, If 3, Class=CDC Data, Driver=cdc_acm, 480M
        ID 2cb7:0210 Fibocom L830-EB-00 LTE WWAN Modem
    |__ Port 007: Dev 007, If 0, Class=Wireless, Driver=btusb, 12M
        ID 8087:0a2b Intel Corp. Bluetooth wireless interface
    |__ Port 007: Dev 007, If 1, Class=Wireless, Driver=btusb, 12M
        ID 8087:0a2b Intel Corp. Bluetooth wireless interface
    |__ Port 008: Dev 008, If 0, Class=Video, Driver=uvcvideo, 480M
        ID 5986:2115 Bison Electronics Inc. 
    |__ Port 008: Dev 008, If 1, Class=Video, Driver=uvcvideo, 480M
        ID 5986:2115 Bison Electronics Inc. 
    |__ Port 009: Dev 009, If 0, Class=Vendor Specific Class, Driver=[none], 12M
        ID 06cb:009a Synaptics, Inc. Metallica MIS Touch Fingerprint Reader
/:  Bus 002.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/6p, 5000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub
    |__ Port 003: Dev 002, If 0, Class=Mass Storage, Driver=usb-storage, 5000M
        ID 0bda:0316 Realtek Semiconductor Corp. Card Reader
/:  Bus 003.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/2p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 001: Dev 002, If 0, Class=Hub, Driver=hub/5p, 480M
        ID 0451:8442 Texas Instruments, Inc. 
        |__ Port 001: Dev 003, If 0, Class=Hub, Driver=hub/7p, 480M
            ID 0424:2137 Microchip Technology, Inc. (formerly SMSC) 
        |__ Port 003: Dev 005, If 0, Class=Hub, Driver=hub/3p, 480M
            ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
            |__ Port 001: Dev 006, If 0, Class=Vendor Specific Class, Driver=r8152, 480M
                ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
        |__ Port 004: Dev 004, If 0, Class=Human Interface Device, Driver=usbhid, 480M
            ID 0451:82ff Texas Instruments, Inc. 
/:  Bus 004.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/2p, 10000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub

Conclusion

Even though they are simple, both commands offer insights into the system’s configuration and status. Whether you’re troubleshooting, optimizing, or simply curious, these tools provide valuable data that can help you better understand and manage your Linux environment. See you next time when we will have a look at more useful listing and information command line tools and how to use them.

Fedora Project community

11 Comments

  1. There is a pipe symbol missing in the first example under Advanced usage example.

    • Maybe it has been fixed already, but there appears to be a pipe symbol in that example between lscpu and jq. It is at the start of the line, but that is OK because the previous line ended with an escaped newline (\).

      • Phoenix

        Not sure if this formats properly in this comment, but if I copy the first example verbatim (just in a single line instead of using the “\” notation), I get:

        $ lscpu --json --extended  | jq '.cpus[] {cpu: .cpu, mhz: .mhz}'
        jq: error: syntax error, unexpected '{', expecting end of file (Unix shell quoting issues?) at <top-level>, line 1:
        .cpus[] {cpu: .cpu, mhz: .mhz}        
        jq: 1 compile error

        Since I do not understand JSON sufficiently, I leave the troubleshooting to whoever is more professional.

      • Phoenix

        I see now that the second instance of this example (including the output) works. The first instance (under the first paragraph of “Advanced usage example”) misses the pipe as mentioned by Jan Vlug.

    • You are correct. The first example is missing the jq pipe (not the shell pipe). So

      jq '.cpus[] {cpu: .cpu, mhz: .mhz}'

      should be

      jq ‘.cpus[] | {cpu: .cpu, mhz: .mhz}’

      Sorry!

  2. You made me go lsTABTAB, nice article

  3. Darvond

    Very good utilities; I like the entire line of ls utilities.

    lsblk is useful for “at a glance” doings of drives.

    I had to use lspci while troubleshooting a Nvidia install, but that was solved.

    I wonder if there’s an article in the works for switching from Grub to Systemd-Boot? Some of us don’t need 20 years of legacy code hanging off our ankles. I’ve done it, but putting the pieces together wasn’t easy.

    • There are no articles in the works for switching to systemd-boot. I prefer sd-boot personally and I use it on all my PCs, but there is too much “risk” with too little gain to recommend that users switch to sd-boot or provide a guide on how to do so. People who understand the concepts and how to get themselves out of trouble in case something goes wrong are welcome to use sd-boot. But if Fedora Magazine were to recommend/suggest that people try it out, it might result in several users “bricking” their systems.

      • Darvond

        Okay true. I was just thinking that instead of having to cross reference multiple documentation points and the forums, it’d be a dandy thing to have it all collated into a single guide.

        Though I admit I had ReFind running (and still do) as a “Break glass in case of emergency”; so there was less risk with snipping out the dotted outline that was Grub.

        It’s indeed a bit of a thought that gives one pause now that I think of it.

  4. Deepika

    I really really liked the article. Its useful , informative and easy to grasp. Will be looking forward to more such articles. will note down the important points.

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions