udev Command Usage and Examples

Understanding udev: Command Usage and Examples

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:

Main Subcommands:

  1. info: Retrieve detailed information about devices.
  2. trigger: Simulate udev events for testing rules.
  3. monitor: Observe kernel events in real time.
  4. 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.

Output:

  • --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:

Output:

  • --udev: Displays only udev 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:

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:

This reloads the udev rules from the configuration directory (/etc/udev/rules.d/).

Stopping Event Handling:

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

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:

2. Write a rule in /etc/udev/rules.d/10-persistent-disk.rules:

3. Reload the rules:

4. Test the rule:

The disk /dev/sda will now have a persistent symlink /dev/mydisk.



Best Practices for Using udev

  1. Test Rules Carefully: Use udevadm test to ensure rules behave as expected.
  2. Backup Configuration Files: Always create backups before modifying rules.
  3. Monitor Events: Use udevadm monitor to debug event handling.

  1. Linux Man Pages: udevadm
  2. Arch Wiki: udev
  3. Documentation: Writing udev Rules

Leave a Reply

Your email address will not be published. Required fields are marked *