Watch USB connections vendor id, product id and revision

17,039

Solution 1

This information appears in the kernel logs — typically in /var/log/kern.log, or /var/log/syslog, or some other file (it depends on your syslog configuration, different distributions have different defaults).

If you'd like something pre-filtered, you can add an udev rule. Create a file /etc/udev/rules.d/tkk-log-usb.rules containing something like:

SUBSYSTEM=="usb", RUN+="/usr/local/sbin/tkk-usb-event"

The environment of the program is populated with a lot of variables describing the device, including:

  • ACTION (add or remove)
  • DEVICE is a path to the device if you want to access it
  • ID_MODEL_ID and ID_VENDOR_ID contain the model and vendor ID, and ID_MODEL and ID_VENDOR contain the corresponding text
  • ID_SERIAL contains the serial number of the device (if available)

Solution 2

You can do that with udevadm:

udevadm monitor --subsystem-match=usb --property

or

udevadm monitor --subsystem-match=usb --property --udev

to filter only udev events. If you want to grep for a particular property you will have to un-buffer udevadm output (with tools like stdbuf, script, unbuffer...):

stdbuf -i 0 -o 0 -e 0 udevadm monitor --subsystem-match=usb --property --udev | grep DEVPATH

or

script -q /dev/null -c "udevadm monitor --subsystem-match=usb --property --udev" | grep PRODUCT

or

unbuffer udevadm monitor --subsystem-match=usb --property --udev | grep -E 'ID_VENDOR_ID|ID_MODEL_ID'
Share:
17,039
Naftuli Kay
Author by

Naftuli Kay

Updated on September 18, 2022

Comments

  • Naftuli Kay
    Naftuli Kay over 1 year

    I'd like to simply watch all devices added and removed from my system and view their USB vendor ID, product ID and revision and other relevant information. How can I do this in Linux? Is there a logfile that I can tail -f or does this require something else?

    • Admin
      Admin about 11 years
      Is dmesg detailed enough?
    • Admin
      Admin about 11 years
      I don't see any output from tailing /var/log/dmesg. Plug in/remove = nothing.
    • Admin
      Admin about 11 years
      @RanyAlbegWein dmesg does not display all USB events, only ones for which a handler exists and calls printk().
    • Admin
      Admin about 11 years
      So is there a way to tail udev for the information?