9

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?

4
  • Is dmesg detailed enough? May 2, 2013 at 0:32
  • I don't see any output from tailing /var/log/dmesg. Plug in/remove = nothing. May 2, 2013 at 0:38
  • 1
    @RanyAlbegWein dmesg does not display all USB events, only ones for which a handler exists and calls printk().
    – Chris Down
    May 2, 2013 at 0:42
  • So is there a way to tail udev for the information? May 2, 2013 at 0:43

3 Answers 3

13

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'
7

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)
0

If you just want to monitor the current plugged USB devices watch --no-title lsusb could fit the bill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.