How to use the command 'udevadm' (with examples)
- Linux
- December 25, 2023
‘udevadm’ is a Linux management tool used for managing various aspects of the udev
device manager. It allows users to monitor device events, print uevents
sent by the kernel, view device attributes, reload udev
rules, trigger rule execution, and test event runs. In this article, we will explore each of these use cases with examples.
Use case 1: Monitor all device events
Code:
sudo udevadm monitor
Motivation:
The ‘udevadm monitor’ command is used to monitor all device events in real-time. This is useful for understanding the behavior of the system when devices are connected, disconnected, or modified.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.monitor
: Subcommand to monitor device events.
Example output:
KERNEL[47828.534689] add /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
KERNEL[47828.535993] add /devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0 (usb)
UDEV [47828.565971] add /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
UDEV [47828.572520] add /devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0 (usb)
Use case 2: Print ‘uevents’ sent out by the kernel
Code:
sudo udevadm monitor --kernel
Motivation:
Printing ‘uevents’ sent out by the kernel helps in understanding the low-level device events triggered by the system. This can be useful for debugging or monitoring purposes.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.monitor
: Subcommand to monitor device events.--kernel
: Print ‘uevents’ sent by the kernel.
Example output:
KERNEL[47828.534689] add /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
KERNEL[47829.535993] remove /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
KERNEL[47830.123456] change /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
Use case 3: Print device events after being processed by ‘udev’
Code:
sudo udevadm monitor --udev
Motivation:
Printing device events after they have been processed by ‘udev’ provides insights into how ‘udev’ deals with device events and performs various actions based on rules. This can be helpful for understanding the behavior of the ‘udev’ device manager.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.monitor
: Subcommand to monitor device events.--udev
: Print device events after being processed by ‘udev’.
Example output:
UDEV [47828.565971] add /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
UDEV [47829.566329] remove /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
UDEV [47830.566398] change /devices/pci0000:00/0000:00:1d.7/usb1/1-3 (usb)
Use case 4: List attributes of device ‘/dev/sda’
Code:
sudo udevadm info --attribute-walk /dev/sda
Motivation:
Listing attributes of a device provides detailed information about the device and its corresponding ‘udev’ rules. This can be beneficial for troubleshooting or understanding the configuration of a specific device.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.info
: Subcommand to retrieve information about devices.--attribute-walk
: List attributes of the given device./dev/sda
: Device path to retrieve information for.
Example output:
looking at /devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
ATTR{alignment_offset}=="0"
ATTR{bdi/0/slaves/sda}=="8:0"
ATTR{capability}=="50"
[...]
ATTRS{queue/scheduler}=="[none] noop deadline cfq"
ATTRS{removable}=="0"
ATTRS{size}=="234441648"
[...]
Use case 5: Reload all ‘udev’ rules
Code:
sudo udevadm control --reload-rules
Motivation:
Reloading ‘udev’ rules is necessary when changes are made to rule files. This ensures that the latest rules are applied without the need for a system restart.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.control
: Subcommand to control ‘udev’ behavior.--reload-rules
: Reload all ‘udev’ rules.
Example output:
No output (if successful)
Use case 6: Trigger all ‘udev’ rules to run
Code:
sudo udevadm trigger
Motivation:
Triggering ‘udev’ rules to run manually can be helpful after making changes to udev rules or when troubleshooting device-related issues. It ensures that the rules are executed promptly without waiting for device events.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.trigger
: Subcommand to trigger ‘udev’ rules to run.
Example output:
No output (if successful)
Use case 7: Test an event run by simulating loading of ‘/dev/sda’
Code:
sudo udevadm test /dev/sda
Motivation:
Testing an event run allows users to simulate the loading of a specific device and observe how ‘udev’ processes the event. This can be useful for debugging or verifying the behavior of a specific device during various scenarios.
Explanation:
sudo
: Execute the command as the superuser.udevadm
: Command for managingudev
.test
: Subcommand to test events run by ‘udev’./dev/sda
: Device path to simulate loading.
Example output:
calling: test
version 248
This program is for debugging only, it does not run any program specified by a RUN key. It may show incorrect results, because some values may be different, or not available at a simulation run.
run: 'socket:@/org/freedesktop/hal/udev_event'
udevadm_test: version 248