Post

Write basic udev rules in linux

Write basic udev rules in linux

Description: Understanding the base concepts behind udev, and learn how to write simple rules


Introduction

In a GNU/Linux system, while devices low level support is handled at the kernel level, the management of events related to them is managed in userspace by udev, and more precisely by the udevd daemon. Learning how to write rules to be applied on the occurring of those events can be really useful to modify the behavior of the system and adapt it to our needs.

How rules are organized

Udev rules are defined into files with the .rules extension. There are two main locations in which those files can be placed: /usr/lib/udev/rules.d is the directory used for system-installed rules, /etc/udev/rules.d/ is reserved for custom made rules.

The files in which the rules are defined are conventionally named with a number as prefix (e.g. 50-udev-default.rules) and are processed in lexical order independently of the directory they are in. Files installed in /etc/udev/rules.d, however, override those with the same name installed in the system default path.

The rules syntax

A udev rule consists of one or more key-value pairs, separated by commas, and written as a single line in the .rules file. Each rule has two main sections:

  • Match section: Contains keys and values that must match device properties or attributes (such as KERNEL, SUBSYSTEM, ATTR{}).
  • Action/Assignment section: Specifies what udev should do when the match conditions are met (such as SYMLINK, RUN, OWNER, GROUP, MODE).

The basic syntax is:

1
KEY1=="value1", KEY2=="value2", ... ACTION_KEY+="action_value"
  • Keys and values in the match section use == (equals) or != (not equals) for comparison.
  • Assignment keys use =, +=, :=, or -= depending on the desired effect.

Example:

1
KERNEL=="ttyUSB*", SUBSYSTEM=="tty", SYMLINK+="myserial"

This rule matches all devices with kernel names starting with ttyUSB and belonging to the tty subsystem, and creates a symlink called myserial.

Operators

== and != operators
The == is the equality operator and the != is the inequality operator. By using them we establish that for the rule to be applied the defined keys must match, or not match the defined value respectively.

The assignment operators: = and :=
The = assignment operator is used to assign a value to keys that accept one. The := operator is used when you want to assign a value and ensure it is not overridden by later rules; values assigned with this operator cannot be altered by other rules.

The += and -= operators
The += and -= operators are used respectively to add or to remove a value from the list of values defined for a specific key.

Common match keys

Some commonly used match keys in udev rules are:

  • ACTION: The type of event (e.g., “add”, “remove”, “change”).
  • KERNEL: The kernel device name or pattern (e.g., “sda”, “ttyUSB”).
  • SUBSYSTEM: The subsystem the device belongs to (e.g., “usb”, “block”).
  • ATTR{key}: Match on device attributes (e.g., ATTR{idVendor}=="1234").
  • ENV{key}: Match on environment variables.

Example match clause:

1
KERNEL=="ttyUSB*", SUBSYSTEM=="tty"

Actions and assignments

After matching, you can specify actions or assignments such as:

  • NAME: Assign a name to the device node.
  • SYMLINK: Create a symbolic link to the device node.
  • RUN: Execute an external program or script.
  • OWNER, GROUP, MODE: Set permissions and ownership.

Example assignment:

1
SYMLINK+="myserial"

Example rule

Here’s a simple example rule that creates a symlink for a USB-to-serial device:

1
KERNEL=="ttyUSB[0-9]*", ATTR{idVendor}=="0403", ATTR{idProduct}=="6001", SYMLINK+="ftdi_serial"

This rule matches any USB serial device with a specific vendor and product ID, and creates a symlink /dev/ftdi_serial when the device is added.

Testing and applying rules

  1. Place your rule file in /etc/udev/rules.d/, for example:
    /etc/udev/rules.d/99-usb-serial.rules
  2. Reload udev rules:
    1
    
    sudo udevadm control --reload-rules
    
  3. Trigger the rules manually (optional):
    1
    
    sudo udevadm trigger
    
  4. Unplug and replug your device to test.

Debugging rules

If your rules aren’t working as expected, increase udev logging:

  • Check the system log (journalctl -u systemd-udevd or dmesg).
  • Use udevadm test to simulate rule application:
    1
    
    sudo udevadm test /sys/class/tty/ttyUSB0
    

Conclusion

Writing udev rules gives you fine-grained control over how your Linux system recognizes and interacts with hardware devices. By understanding the matching keys, operators, and action assignments, you can automate device management and customize your environment to meet specific needs.


References:

This post is licensed under CC BY 4.0 by the author.