1. Introduction
When working with a Linux machine, it’s common to use USB devices such as external hard drives, flash drives, and other peripherals to transfer data, install software, or backup important files. However, before we can begin using a USB device, we need to ensure that it’s properly connected to our system.
In this tutorial, we’ll explore various ways to check whether a USB device is present on a Linux machine. To that end, we’ll be using several command-line tools and techniques. After finishing this tutorial, we’ll be able to troubleshoot USB connectivity issues and ensure that our system correctly recognizes the devices we want to connect.
2. Using lsusb
lsusb is a command-line utility that displays information about USB buses and connected devices on a Linux system. It’s quite useful for troubleshooting USB connectivity issues and identifying unrecognized devices.
First, let’s install the usbutils package, which contains the tool, using apt:
$ sudo apt install usbutils
Now, we can see what the lsusb command returns:
$ lsusb
Bus 001 Device 002: ID 346d:5678 USB Disk 2.0
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
We can observe that the command properly recognized the connected USB flash drive named USB Disk 2.0 in the system.
If we know the vendor and product identifier numbers of a device, we can also specifically check, using the -d option, whether that particular device is present:
$ lsusb -d 346d:5678
Bus 001 Device 002: ID 346d:5678 USB Disk 2.0
As we can see above, lsusb successfully returned the USB device we specified.
3. Utilizing usb-devices
usb-devices is a Bash shell script that displays details about USB buses and connected devices on a Linux system. Its output is similar to the contents of the usb/devices file under /sys/kernel/debug, which we’ll analyze in the following sections, but it only lists active interfaces.
This tool is again part of usbutils:
$ sudo apt install usbutils
Let’s get the connected USB devices with usb-devices:
$ usb-devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh=12
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev=05.15
S: Manufacturer=Linux 5.15.0-67-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:0b.0
C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA
I: If#=0x0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=346d ProdID=5678 Rev=02.00
S: Manufacturer=USB
S: Product=Disk 2.0
S: SerialNumber=6773681095176743746
C: #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
I: If#=0x0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
...
From the above, we can immediately say that it outputs many details by default. When we take a closer look, we can see the USB Disk 2.0 as we did previously.
Similarly, we can also filter a specific device with vendor and product id numbers using grep:
$ usb-devices | grep -C 3 'Vendor=346d ProdID=5678'
T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=346d ProdID=5678 Rev=02.00
S: Manufacturer=USB
S: Product=Disk 2.0
S: SerialNumber=6773681095176743746
In the above snippet, using the -C option prints the specified number of surrounding lines in addition to the matched line, allowing us to obtain more context.
4. Using usbview
Another way to see the connected USB devices is with the usbview tool. This tool provides a graphical overview of recognized devices in a tree display. It’s probably not pre-installed on our system by default, so we can use a package manager to initiate the installation:
$ sudo apt install usbview
After the installation, let’s try usbview with superuser privileges to check if our device is present:
$ sudo usbview
Accordingly, we get a graphical user interface. Moreover, when we click on a device we want, we can also get additional information:
As we can observe from the image, this graphical tool is also helpful to find the presence of a device in a Linux machine.
5. Filtering the Output of dmesg
Furthermore, we can filter out the output of dmesg to obtain the information we seek. dmesg is a command-line tool that displays the kernel ring buffer, providing a detailed log of system messages and events that have occurred since the system was booted up.
Let’s try to find whether the USB device of interest is present by filtering the output with grep:
$ dmesg | grep -A 4 'idVendor=346d, idProduct=5678'
[ 3.729128] usb 1-1: New USB device found, idVendor=346d, idProduct=5678, bcdDevice= 2.00
[ 3.729132] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.729133] usb 1-1: Product: Disk 2.0
[ 3.729135] usb 1-1: Manufacturer: USB
[ 3.729135] usb 1-1: SerialNumber: 6773681095176743746
As we can see above, we can observe that the USB storage device is recognized successfully as expected. Besides, the -A option after grep prints the specified number of trailing lines in addition to the matched line.
6. Inspecting /usb/devices
In some cases, it may be preferable to avoid relying on additional tools as we did previously. Consequently, we can inspect /usb/devices under the /sys/kernel/debug directory of the sysfs pseudo-filesystem. This directory is only accessible to root users, and it provides access to several kernel debugging interfaces which could come in handy for troubleshooting issues.
Let’s directly inspect what the file contains with cat:
$ sudo cat /sys/kernel/debug/usb/devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh=12
B: Alloc= 0/800 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev= 5.15
S: Manufacturer=Linux 5.15.0-69-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:0b.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 4 Ivl=256ms
T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=346d ProdID=5678 Rev=02.00
S: Manufacturer=USB
S: Product=Disk 2.0
S: SerialNumber=6773681095176743746
C: #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
I: If#=0x0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
...
As we expected, it returned numerous pieces of information about connected USB devices, very similar to the output of usb-devices from before.
Again, we can filter the output with grep:
$ sudo cat /sys/kernel/debug/usb/devices | grep -C 3 'Vendor=346d ProdID=5678'
T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=346d ProdID=5678 Rev=02.00
S: Manufacturer=USB
S: Product=Disk 2.0
S: SerialNumber=6773681095176743746
We got the USB device information for the specified ID numbers as expected.
Additionally, we can also inspect /usb/devices under the /sys/bus directory:
$ cat /sys/bus/usb/devices/*/product
Disk 2.0
USB Tablet
EHCI Host Controller
OHCI PCI host controller
Unlike the previous one, this command doesn’t require superuser privileges and it’s more clear when we simply want to get the names of the recognized USB devices.
If we need to get additional information, we can use other files under the directory:
$ cat /sys/bus/usb/devices/*/manufacturer
USB
VirtualBox
Linux 5.15.0-67-generic ehci_hcd
Linux 5.15.0-67-generic ohci_hcd
In this example, we fetch the manufacturer information.
7. Conclusion
In this article, we’ve explored several different methods and tools for checking whether a USB device is present on a Linux machine.
From the command-line tools such as lsusb, usb-devices, and dmesg, to the usbview utility which provides a graphical interface, we’ve discussed a range of options for viewing information about connected USB devices. Additionally, we’ve learned how we can directly inspect system files like /usb/devices.