Skip to content

Linux Foundations / Understand the system underneath ยท Step 17 of 38

Lesson: Managing devices with udevSwitch
โ† Linux Foundations

System administration

Managing devices with udev

See how udev reacts to hardware events and write a simple rule for a device.

Plug in a USB drive and a node appears in /dev with sensible permissions and extra links. The service behind this is udev, which on systemd based distributions runs as systemd-udevd.

How it works

When a device is added, changed or removed, the kernel sends an event called a uevent. udev receives it, checks its rules, and acts: it can set the owner, group and mode of a node, add symbolic links, or run a short program.

Watch events live, then plug something in:

bash
udevadm monitor
KERNEL[5120.104312] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2 (usb)
UDEV  [5120.131587] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2 (usb)

KERNEL lines are the raw uevents. UDEV lines appear after udev has processed the rules. Press Ctrl+C to stop.

Where rules live

  • /usr/lib/udev/rules.d/ holds rules shipped by packages (shown as /lib/udev/rules.d/ on some older layouts).
  • /etc/udev/rules.d/ holds your own rules.

Rule files must end in .rules. They are processed in name order across all directories, which is why names start with numbers. A file in /etc replaces a packaged file with the same name. Put your changes there, because package updates can overwrite files under /usr/lib.

Find device properties

A rule needs something to match on. Ask udev:

bash
udevadm info --query=property --name=/dev/ttyUSB0
udevadm info --attribute-walk --name=/dev/ttyUSB0
  looking at parent device '/devices/.../1-2':
    SUBSYSTEMS=="usb"
    ATTRS{idVendor}=="0403"
    ATTRS{idProduct}=="6001"

The output is shortened.

Write a rule

Create /etc/udev/rules.d/99-serial.rules with one line:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="myserial", MODE="0660"

== is a match test. = assigns a value, and += adds to a list. This rule creates /dev/myserial for that adapter and sets its mode. ATTRS searches parent devices, and all ATTRS matches in one rule must succeed on the same parent.

Apply it

bash
sudo udevadm control --reload
sudo udevadm trigger --subsystem-match=tty

The first command reloads the rules. The second replays events for existing devices. Unplugging and plugging in again also works.

To debug, udevadm test /sys/class/tty/ttyUSB0 simulates an event and shows which rules are applied. Service logs are available with journalctl -u systemd-udevd on systemd systems. Learn more about file modes in the permissions in depth lab.

Try it

  1. List /etc/udev/rules.d and /usr/lib/udev/rules.d.
  2. Pick a block device and run udevadm info --query=property --name=/dev/sda, using a name from lsblk.
  3. Run udevadm monitor --udev and plug in a USB device if you have one.

Containers often have no running udev, so use a real machine or virtual machine.

Check your understanding

Managing devices with udev

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.