udev rules allow Linux users to customize how the system handles USB devices when they are plugged in or removed. This listicle covers practical examples of udev rules specifically for USB devices, providing real-world use cases for system administrators, developers, and power users who want to automate device management, assign consistent device names, or trigger scripts upon device events.
1. Assigning a Persistent Symlink to a USB Serial Device
When multiple USB serial devices (like USB-to-serial adapters) are connected, device names such as /dev/ttyUSB0 or /dev/ttyUSB1 can change between reboots or reconnections. A udev rule can create a persistent symlink based on unique device attributes like the serial number or vendor/product ID. For example, using ATTRS{serial}=="123456" to identify the device and then creating a symlink like /dev/ttyUSB_mydevice ensures consistent referencing in scripts or applications.
2. Automatically Mounting a USB Storage Device
udev rules can trigger scripts that mount USB storage devices automatically when inserted. By matching devices with a specific vendor and product ID, a rule can call a shell script that mounts the device to a predefined directory. This is useful for kiosks or embedded systems where manual mounting is not feasible. The rule uses ENV{ID_FS_TYPE}=="vfat" or similar to identify filesystem types before mounting.
3. Changing Permissions on a USB Device
Some USB devices require special permissions to be accessible by non-root users or specific groups. A udev rule can set the group ownership and permissions for devices matching certain attributes, such as vendor and product ID. For example, setting GROUP="plugdev" and MODE="0660" on a USB webcam device allows members of the plugdev group to access it without root privileges.
4. Running a Script When a USB Device Is Plugged In
udev can execute arbitrary scripts or commands in response to USB device events. For example, a rule can run a backup script whenever a specific USB external hard drive is connected. This is done by matching the device’s vendor and product IDs and using the RUN+="/path/to/script.sh" directive. It’s important that scripts triggered by udev run quickly and do not block udev’s event handling.
5. Disabling Automount for a Specific USB Device
In some environments, automounting certain USB storage devices is undesired for security or operational reasons. A udev rule can mark these devices with ENV{UDISKS_IGNORE}="1", which prevents desktop environments and automount daemons like udisks from mounting them automatically. This is useful for devices used only for data transfer via manual mounting.
6. Custom Naming for USB Audio Devices
USB audio interfaces often show up as generic sound cards (e.g., hw:1,0), which can change order if multiple devices are connected. A udev rule can assign persistent names or create symlinks in /dev/snd/ based on device properties like ATTRS{idVendor} and ATTRS{idProduct}. This makes it easier for audio applications or scripts to select the correct device consistently.
7. Disabling USB Power Management for Specific Devices
Some USB devices, such as certain webcams or network adapters, malfunction or disconnect due to aggressive USB autosuspend power management. A udev rule can disable power management selectively by writing on or auto to the device’s power control attribute. For example, setting ATTR{idVendor}=="046d" (Logitech) and then ATTR{power/control}="on" prevents autosuspend for that device.
8. Limiting USB Devices to Specific Users
On multi-user systems, you may want to restrict access to certain USB devices to a particular user or group. By setting ownership and permissions in a udev rule, devices can be made accessible only to designated users. For example, setting OWNER="username" and MODE="0600" ensures only that user can read or write to the device node.
9. Triggering Network Configuration on USB Tethering
When a USB phone is connected for tethering, a new USB network interface (usb0 or similar) appears. A udev rule can detect this interface by matching its driver or interface type and then trigger a DHCP client or custom script to configure networking automatically. This automates internet sharing setups without manual intervention.
10. Logging USB Device Connections for Auditing
udev rules can log USB device events to a specific file for auditing or debugging purposes. By running a logging script on add or remove events and passing device environment variables, administrators can maintain a history of connected USB devices with timestamps. This is useful for security monitoring in sensitive environments.
How to Choose
Choosing the right udev rule depends on your specific use case and environment. Key factors include the device’s unique identifiers (vendor ID, product ID, serial number), the desired action (naming, permission changes, script execution), and system constraints like performance and security. Always test rules in a controlled setting to avoid conflicts or unintended device behavior. Additionally, consider whether your rule should apply globally or only to specific users or groups to maintain system integrity.
Frequently Asked Questions
Q: How do I find the attributes needed for a udev rule?
Use the udevadm info --attribute-walk --name=/dev/yourdevice command to list all device attributes that can be matched in rules.
Q: Can udev rules run long-running scripts?
No, udev expects scripts to finish quickly. For longer tasks, trigger a background process or use systemd services.
Q: Will udev rules persist across reboots?
Yes, rules saved in /etc/udev/rules.d/ or /lib/udev/rules.d/ are loaded on boot and apply automatically.
Q: How do I debug udev rules if they don’t work?
Use udevadm test /sys/path/to/device and check journalctl logs for errors or rule application details.
Q: Can I write udev rules for non-USB devices?
Yes, udev manages all device types, but the matching attributes differ depending on the device class.
Final Thoughts
udev rules provide powerful, flexible control over how Linux systems handle USB devices. Whether you need consistent device naming, automated mounting, permission adjustments, or custom event handling, crafting effective udev rules can simplify device management and enhance system automation. Understanding device attributes and carefully testing your rules will ensure reliable and secure operation tailored to your needs.
Related
- Fuzzy Black Caterpillars Without Stingers: 10 Common Species Explained
- Copycat Enchilada Sauce Low Sodium Versions You Can Make at Home
- Shrubs and Bushes Without Thorns Perfect for Kids’ Gardens