Ubuntu

How to use udevadm on Ubuntu

Since kernel version 2.6, udev has been a Linux subsystem for dynamic device administration and detection. It is considered as a substitute for “hotplug” and “devfs”. At boot time, or when you add or remove any device from your Ubuntu system, it dynamically builds or removes device nodes. These nodes provide interfaces to device drivers found in file systems. The device’s information or status changes are propagated to userspace by using “udevadm“. It is also known as “device manager” in Linux-based systems like Ubuntu.

What are the functions of udevadm on Ubuntu

Some of the key functions of udevadm are:

  • It manages device node permissions.
  • It provides device events to system applications.
  • It creates useful symbolic links in the “/dev” directory to access any device or network interface.

Now, we will demonstrate to you how to use udevadm on your Ubuntu. So let’s start!

How to use udevadm on Ubuntu

The udevadm keeps track of a device from the second it is attached to the system until it is plugged out. If you want to monitor or track your device events, then execute the below-given commands in your terminal:

$ udevadm monitor

The output is declaring that udevadm is tracking all of the devices events. In this process, if you attach any device to your system, you will see that udevadm will also take that device in its monitoring system:

How to print out a device’s attribution using udevadm

In the Ubuntu terminal, “udevadm info” is the command used to print any specific device’s attributes. For instance, to print the set of attributes of our “sr0” device and its related information, we will write the below-given command in our terminal:

$ udevadm info -a -p /block/sr0

Here, the “-a” option is added for listing the attributes of the “sr0” device, whereas “-p” will define the “dev” path:

How to add udev rules using udevadm

First of all, we will find the ENV{PRODUCT} by running this command in the terminal and attach your device to the system:

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

Here,

  • –kernel” makes sure the name of the event device is correct
  • –property” will show the device properties
  • subsystem-match” option will match the subsystem of the event“usb” device

For instance, we have attached a keyboard to our system. Now, we will utilize the “/usr/lib/udev/rules.d/91-keyboard.rules” file for adding a new “udev” rule called “keyboard.rules”. For that, open up the “/usr/lib/udev/rules.d/91-keyboard.rules” file in your “nano” editor:

Now, add the following code in the opened “/usr/lib/udev/rules.d/91-keyboard.rules” file:

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

This code “add” the “ACTION” or a rule that whenever a “USB” type device having “PRODUCT=1a2c/4c5e/110” is attached to the system, it will execute the “/home/linuxhint/keyboard.sh” script:

Press “CTRL+O” to save the “/usr/lib/udev/rules.d/91-keyboard.rules” file. After that, we will create a “keyboard.sh” script and write the below-given “echo” command in it:

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

Now, make the “keyboard.sh” file executable by using the “chmod” command:

$ sudo chmod +x /home/linuxhint/keyboard.sh

How to apply udev rules using udevadm

To apply the added rules, utilize the “udevadm control” command in your terminal:

$ sudo udevadm control --reload

Here, the “–reload” option will reload the udev rules:

Now, whenever you attach the keyboard, the system will automatically execute the “keyboard.sh” script and write “keyboard connected” in the log file. You can view the content of “keyboard.log” by using the content command:

$ cat keyboard.log

How to test udev rules using udevadm

To test the added rules of any device, “udevadm test” command is utilized in the terminal. Here is the syntax of “udevadm test” command:

udevadm test $(udevadm info -q path -n [device name])

Here, the “-q” option is added to query the device information in the database, specified by its path. Next, option “-n” is added for the device name.

Now, for our “sr0” (CD-ROM) device, we will check test the udev rules by writing the below-given command in the terminal:

$ udevadm test $(udevadm info -q path -n sr0)

That was all about udevadm, you can visit the udevadm manual page for more information.

Conclusion

The udevadm is the Linux kernel’s device manager. For all devices, udev dynamically builds or removes device node files in the “/dev” directory at startup time. Management of node permission is also one of the duties of udev. This article showed you how to use udevadm on Ubuntu. Moreover, the procedures of adding, applying, and testingudev rules are also provided. If you want to work with a device manager, then feel free to try it out!

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.