[Howto] Find matches for udev rules

Tux
Recently I bought a new keyboard and wondered if I can trigger actions via udev when the keyboard is plugged in. For that, however, it must be known how to create udev rules matching the keyboard in the first place.

To write udev rules the most important part is to match the device you want to handle with the rules. While hardware fixed in the computer might be easy, the rest usually isn’t. For example, you can trigger something when keyboard xyz of manufacturer abc is plugged in – but for that you must now where to find which information and how to present them to udev.

This can be done with the program udevmonitor. It informs you of newly plugged in hardware detected by udev:

# udevmonitor
...
UEVENT[1217720129.155386] add      /devices/pci0000:00/0000:00:1d.0/usb5/5-1 (usb)
UEVENT[1217720129.155440] add      /class/usb_endpoint/usbdev5.9_ep00 (usb_endpoint)
UEVENT[1217720129.157973] add      /devices/pci0000:00/0000:00:1d.0/usb5/5-1/5-1:1.0 (usb)
UDEV  [1217720129.166782] add      /devices/pci0000:00/0000:00:1d.0/usb5/5-1 (usb)
UDEV  [1217720129.171948] add      /class/usb_endpoint/usbdev5.9_ep00 (usb_endpoint)
...

The next step is to use these info to gather even more information to actually create a rule: the devices path is interesting an contains more information. So we have a look at it with yet another udev tool: udevinfo

# udevinfo -a -p /sys/devices/pci0000:00/0000:00:1d.0/usb5/5-1
...
  looking at device '/devices/pci0000:00/0000:00:1d.0/usb5/5-1':
    KERNEL=="5-1"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{dev}=="189:522"
    ATTR{configuration}==""
    ATTR{bNumInterfaces}==" 2"
    ATTR{bConfigurationValue}=="1"
    ATTR{bmAttributes}=="a0"
    ATTR{bMaxPower}=="100mA"
    ATTR{urbnum}=="1541"
    ATTR{idVendor}=="046a"
    ATTR{idProduct}=="0023"
    ATTR{bcdDevice}=="0032"
...

This shows us several attributes we can use in the udev-rules. The data also match with the data given by lsusb, btw.:

# lsusb
...
Bus 006 Device 001: ID 1d6b:0001
Bus 005 Device 011: ID 046a:0023 Cherry GmbH Cymotion Master Linux Keyboard
Bus 005 Device 001: ID 1d6b:0001
...

So now a udev rule can be created with the data above. In case of the keyboard the rule could for example be:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="046a",ATTRS{idProduct}=="0023",ACTION=="add",RUN+="/usr/local/bin/mykeyboard.sh"

In this case udev launches the program mykeyboard.sh when a the usb subsystem recognizes a device from the vendor 046a with the product id 0023. This can of course be further fine grained, depending on the needs.

The rule itself must afterwards be copied to the right folder and must be given a number according to the priority regarding other rules. On a Fedora system the udev folder is /etc/udev/rules.d, and a typical udev rule name could be 62-keyboard.rules.

4 thoughts on “[Howto] Find matches for udev rules”

  1. The last time I tried to make a udev rule for my webcam (I wanted the webcam to be identified as video1 so my PVR would be consistently identified as video0) I made a mistake. I’m still not sure what I did, but my primary harddisk was identified as video0, rendering my computer unbootable. The mistake was easily corrected with a bootdisk once I had identified what was wrong.

    Anyway, thanks for this post, udev is great for doing all sorts of things, but it’s complicated. So we need lots of recipes.

    Filip

  2. Filip, udev comes with a lot of tools to manually check all the entries before rebooting the system. So next time you better check with these. But since it is a core part of the system things can go pretty wrong unfortunately. :/

  3. Hey can you please tell me how to use ENV{} variable with udev rules???
    I am trying to block all the mouse.
    SUBSYSTEMS==”usb”, ACTION==”Add’, ENV{ID_INPUT_MOUSE}==”1″, ATTR{authorized}=”0″

    The value of ENV{ID_INPUT_ID} should be “1” when mouse is inserted but it taking null value.
    so, ENV{ID_INPUT_MOUSE} is not matching.
    can you help me??
    feel free to mail me also

    1. Hi Mayank, the best is to ask over at stackoverflow or similar forums, those offer much better ways to ask questions and discuss answers.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.