udev
is a device manager for the Linux kernel, responsible for managing device nodes in the /dev
directory. It operates in user space and provides dynamic creation and removal of device files as hardware is connected or disconnected. This guide explains the purpose of udev, how to use its commands, and provides examples to understand its functionality better.
What is udev?
udev
is a part of the systemd
system and service manager. It listens to kernel events (such as adding or removing devices) and performs appropriate actions, such as creating device nodes, loading drivers, or running user-defined scripts.
Key Features:
- Dynamic device node management in
/dev
. - Triggering actions based on hardware events using rules.
- Supporting persistent device naming for stable identification.
How to Use udev
udev works in conjunction with the following utilities to manage and monitor device events:
udevadm
The main command-line interface for interacting with udev
.
Syntax:
udevadm [command] [options]
Main Subcommands:
info
: Retrieve detailed information about devices.trigger
: Simulate udev events for testing rules.monitor
: Observe kernel events in real time.control
: Manage the state of the udev daemon.
Examples of udev Command Usage
1. Viewing Device Information with info
The udevadm info
command retrieves detailed information about a device.
udevadm info --query=all --name=/dev/sda
Output:
P: /devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
N: sda
S: disk/by-id/ata-VBOX_HARDDISK_VB12345678
S: disk/by-path/pci-0000:00:1f.2-ata-1
E: DEVNAME=/dev/sda
E: DEVTYPE=disk
E: ID_BUS=ata
E: ID_MODEL=VBOX_HARDDISK
...
--query=all
: Displays all available information.--name=/dev/sda
: Specifies the device to query.
2. Monitoring Events in Real Time
The udevadm monitor
command allows monitoring of kernel and udev
events in real time.
Example:
udevadm monitor --udev --property
Output:
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
UDEV [12345.67890] add /devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sdb (block)
ACTION=add
DEVNAME=/dev/sdb
DEVTYPE=disk
...
--udev
: Displays onlyudev
events.--property
: Displays event properties.
3. Testing and Triggering Events with trigger
Use the udevadm trigger
command to test udev
rules by simulating events.
Example:
udevadm trigger --action=add --subsystem-match=block
This simulates an “add” action for all devices in the block
subsystem, causing udev
to process rules for those devices.
4. Managing the udev
Daemon with control
The udevadm control
command can modify the behavior of the udev
daemon.
Reloading Rules:
udevadm control --reload
This reloads the udev
rules from the configuration directory (/etc/udev/rules.d/
).
Stopping Event Handling:
udevadm control --stop-exec-queue
This stops the execution of queued events, useful for maintenance.
Writing udev
Rules
udev
rules are stored in the /etc/udev/rules.d/
directory. Each rule specifies conditions and actions for device events.
Example Rule
Rule File: /etc/udev/rules.d/99-usb.rules
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="1234", ATTR{idProduct}=="5678", RUN+="/usr/local/bin/custom-script.sh"
Explanation:
ACTION=="add"
: Matches device addition events.SUBSYSTEM=="usb"
: Targets USB devices.ATTR{idVendor}
: Matches the vendor ID of the device.ATTR{idProduct}
: Matches the product ID of the device.RUN+="/usr/local/bin/custom-script.sh"
: Executes a script when the rule matches.
Example: Creating a Persistent Device Name
1. Identify the device information:
udevadm info --query=all --name=/dev/sda
2. Write a rule in /etc/udev/rules.d/10-persistent-disk.rules
:
KERNEL=="sda", SUBSYSTEM=="block", SYMLINK+="mydisk"
3. Reload the rules:
udevadm control --reload
4. Test the rule:
udevadm trigger --action=add --name=/dev/sda
The disk /dev/sda
will now have a persistent symlink /dev/mydisk
.
Best Practices for Using udev
- Test Rules Carefully: Use
udevadm test
to ensure rules behave as expected. - Backup Configuration Files: Always create backups before modifying rules.
- Monitor Events: Use
udevadm monitor
to debug event handling.