Linux › Section C · Devices › Topic C.4
udev
When you plug in a USB stick, something must set up its device file, give it sensible permissions and tell the desktop it has arrived. On Linux that something is udev, a user-space service driven by kernel events and a set of rules you can read, test and extend.
Why device management moved to user space
Older systems shipped a /dev directory full of pre-made device files, most for hardware the machine never had, and every new kind of device meant updating it. A kernel-only replacement, devfs, proved inflexible. The current design splits the work. The kernel itself creates the basic device nodes in /dev, which is a devtmpfs filesystem held in memory, so they exist from the earliest moments of boot. Everything that needs policy, such as permissions, extra names and start-up actions, is handled by a user-space daemon, systemd-udevd, usually just called udev. Keeping that policy out of the kernel keeps the kernel simpler and lets administrators change it with plain text files.
What happens when a device appears
When the kernel detects, removes or changes a device, it sends a notification called a uevent to user space over an internal network link. A uevent is a list of properties, such as ACTION=add, DEVNAME=sdb, SUBSYSTEM=block, DEVTYPE=disk and the device's path under /sys. systemd-udevd receives it, reads the device's attributes from sysfs, and runs the event through its rules. The rules can set the owner, group and mode of the device node, create symbolic links to it, gather more information by running helper programs, load drivers, and record properties in udev's database. Other programs, such as desktop environments and udisks, are then told the device is ready.
Where rules live
Rules are text files ending in .rules; files with any other suffix are ignored. Packages install the defaults in /usr/lib/udev/rules.d (or /lib/udev/rules.d on older layouts). Local rules belong in /etc/udev/rules.d, and runtime rules can be placed in /run/udev/rules.d. udev gathers the files from all these directories and processes them together in lexical order of their names, which is why names start with numbers, such as 60-persistent-storage.rules or 99-local.rules. A file in /etc with the same name as a default file replaces it, and a link to /dev/null with that name disables the default entirely.
How a rule is written
Each rule is one line of comma-separated key, operator and value triples. Match keys use == or !=: KERNEL matches the kernel's device name, SUBSYSTEM the subsystem, ACTION the event type, ATTR a sysfs attribute of the device, ATTRS an attribute of the device or any parent, and ENV a property. Values may contain shell-style patterns such as * and ?. If every match succeeds, the assignments run: = sets a value, += adds to a list, -= removes from one, and := sets a value that later rules cannot change. Common assignment keys are SYMLINK, OWNER, GROUP, MODE and RUN, which runs a short program. udev never renames device nodes; NAME applies only to network interfaces.
How the stable disk names are made
The /dev/disk/by-id links described in Device name summary come from rules in a default file named 60-persistent-storage.rules. The pattern is worth knowing because many rules follow it. An early rule matches disks that do not yet have a serial number property and imports properties from a helper program that asks the drive for its model and serial number. Once ENV{ID_SERIAL} is set, a later rule in the same file matches it and adds a link with SYMLINK+=, building the link name from those properties. Rules are read in order, and a rule can rely on properties that earlier rules added to the event.
Inspecting devices with udevadm
udevadm is udev's administration tool. udevadm info --query=all --name=/dev/sdb prints what udev knows about a device: P: the sysfs path, N: the node name, S: each symbolic link, and E: each property. For writing rules, udevadm info --attribute-walk --name=/dev/ttyUSB0 (short form -a) prints the attributes of the device and of every parent device up the tree, in the syntax rules use. Attributes from the device itself can be matched with ATTR, but those from parents need ATTRS. A single rule may only use ATTRS values from one parent, so pick them all from the same block of output.
Worked example: a fixed name for a serial adapter
A weather station's USB serial adapter appears as /dev/ttyUSB0 or /dev/ttyUSB1, depending on plugging order. 1. Find its identifiers: udevadm info -a -n /dev/ttyUSB0 shows a parent with ATTRS{idVendor}=="1a86" and ATTRS{idProduct}=="7523", a common adapter chip. 2. As root, create /etc/udev/rules.d/99-weather.rules containing one line: SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="weather", GROUP="dialout", MODE="0660". 3. Reload the rules: sudo udevadm control --reload. 4. Unplug and replug the adapter. 5. ls -l /dev/weather now shows a link to the right ttyUSB device. If two identical adapters are attached, add a serial-number match.
Watching events with udevadm monitor
udevadm monitor prints events as they happen, which is the quickest way to see what the kernel and udev do when hardware changes. By default each event appears twice: a line marked KERNEL when the uevent arrives, and a line marked UDEV when udev has finished processing it. --kernel or --udev shows only one kind, --property adds each event's full list of properties, and --subsystem-match=block limits the output to one subsystem. After editing rules, udevadm control --reload loads them, and udevadm trigger replays events for existing devices, as change events unless you add --action=add; udevadm test simulates one device's event and shows which rules apply.
Worked example: watching a USB stick arrive
1. Run udevadm monitor --subsystem-match=block and insert a USB stick. 2. Illustrative output shows pairs such as KERNEL[2201.55] add /devices/.../block/sdb (block) and then UDEV [2201.60] add /devices/.../block/sdb (block), followed by the same for sdb1. 3. Each pair shows the kernel announcing the device and udev finishing its rules a few hundredths of a second later. 4. Press Ctrl+C, then rerun with --property and insert the stick again to see values such as ID_SERIAL and DEVLINKS, the links udev made. 5. If a rule you wrote is not working, this tells you whether the event arrives at all and which properties you can match.
Common mistakes to avoid
First, editing files in /usr/lib/udev/rules.d, which package updates overwrite; put local rules in /etc/udev/rules.d. Second, saving a rule file without the .rules suffix, so it is ignored. Third, using = where == was meant, turning a test into an assignment. Fourth, mixing ATTRS values from different parent devices in one rule, so it never matches. Fifth, trying to rename a disk node with NAME, when only SYMLINK works for disks. Sixth, forgetting to reload with udevadm control --reload and then replug or trigger the device.
Key takeaways
The kernel creates basic nodes in devtmpfs and sends a uevent for every device change; systemd-udevd applies rules that set ownership and modes, add symbolic links such as /dev/disk/by-id, run helpers and notify other programs. Rules are .rules files, defaults in /usr/lib/udev/rules.d and local ones in /etc/udev/rules.d, read in lexical order. == and != match; =, +=, -= and := assign. udevadm info shows a device's data, with -a for rule writing; udevadm monitor shows events live; udevadm control --reload, trigger and test apply and check rules.
Flashcards
Flashcard
In-memory filesystem on /dev where the kernel creates basic nodes
Test your understanding
Question 1 of 5
You want to change how udev handles a device. Where should your rule file go?
Question 2 of 5
What does SYMLINK+="sensor" do in a rule whose match keys all succeed?
Question 3 of 5
A rule reads SUBSYSTEM="tty", ATTRS{idVendor}=="1a86", SYMLINK+="radio" and never seems to work as a filter. What is wrong?
Question 4 of 5
In udevadm monitor output, why does each event usually appear twice?
Question 5 of 5
You have just edited a rule file. What should you run so it applies to a device that is already plugged in?
