Basics of udevadm command in Linux with examples

The udevadm command is a device management tool in Linux which manages all the device events and controls the udevd daemon. udev rules are defined with the .rules file in /usr/lib/udev/rules.d

In this article, we’ll learn about the basics of udevadm in Linux with the help of examples.

Syntax of udevadm command

The syntax for the udevadm command:

udevadm [--debug] [--version] [--help]

udevadm info options
udevadm trigger [options]
udevadm settle [options]
udevadm control command
udevadm monitor [options]
udevadm test [options] devpath

How to monitor devices with the udevadm command?

udevadm monitors a device from the moment it’s plugged into the system until it’s plugged out of the system.

Syntax to monitor all the events of a device:

udevadm monitor
Executing Udevadm Command
Executing udevadm command

In the above image, you can see the udevadm tool is monitoring all the events related to the devices. Now if we plugin in a device, you can see it outputting those events on your terminal session.

Events Occured While Mounting Optical Drive In Virtualbox
Events occurred while mounting Optical Drive in VirtualBox

How to list all the attributes of a device with udevadm?

udevadm info command is used to list all the device attributes.

Syntax to list all the device attributes of the device sr0:

udevadm info -a -p /block/sr0

The -a option is used to print all the sysfs properties, and the -p option is used to define the dev path.

All The Device Attributes With The Udevadm Command 1
All the device attributes with the udevadm command

Writing udev rules and applying them without rebooting using udevadm

First, we need to find ENV{PRODUCT} by executing:

udevadm monitor --kernel --property --subsystem-match=usb

and, then connect your device.

Finding ENVPORODUCT
Finding ENV{PRODUCT} using udevadm command

Now let’s write a new udev rule in /usr/lib/udev/rules.d called 91-keyboard.rules to echo in terminal whenever the keyboard is connected, using a text editor.

sudo nano /usr/lib/udev/rules.d/91-keyboard.rules

and, then add the following code to echo the message:

ACTION=="add", SUBSYSTEM=="usb", ENV{PRODUCT}=="1a2c/4c5e/110", RUN+="/bin/sh /home/suryansh/keyboard.sh"

Then, add the sh script at the above location using your favorite text editor:

echo "Keyboard connected!" > /home/suryansh/keyboard.log

Give execution permissions with chmod command:

sudo chmod +x /home/suryansh/keyboard.sh

The syntax for the udevadm control to apply changes.:

sudo udevadm control --reload

udevadm control is used to modify the rules in the udevd daemon.

Whenever now you’ll connect that device, it will run the sh keyboard.sh and write keyboard.log in the same directory.

So, when you use the cat command:

cat keyboard.log

You should see the following output:

suryansh@journaldev:~$ cat keyboard.log
Keyboard connected!

How to test udev rules with udevadm?

The udevadm test command is used to test the udev rules in the terminal.

Syntax to test udev rules for a device:

udevadm test $(udevadm info -q path -n <device>)
Testing Udev Rules With Udevadm
Testing udev rules with udevadm

Conclusion

udevadm is a very handy tool to managing udev rules in Linux and it is available in all Linux distributions. You can find out a more in-depth overview of the udevadm commands in here.

Thank you for reading!