How to Configure Device File owner/group with udev rules?

Udev is the mechanism used to create and name /dev device nodes corresponding to the devices that are present in the system. Udev uses matching information provided by sysfs with rules provided by the user to dynamically add the required device nodes.

 

There are no static files under /dev directory for devices, devices are created/removed dynamically by udev(or udevd) which is a daemon monitoring what devices are connected/disconnected on to the kernel, and run additional appropriate scripts accordingly. Manually creating files under /dev directory or changing its file attributions does no effect, manually modifications would be ignored or could be revised automatically.

 

This post describes how to configure attributions of device files under /dev directory via udev. The common configuration files for udev are under /etc/udev/rules.d directory, which describes rules for creating device files. The files must have .rules extension/suffix and unique names, others would be ignored. These files are referred in lexical order, thus 60-raw.rules is referred before 70-persistent-net.rules, and the later rules overwrite the previous ones unless you specify an exemption in the later rules file.

 

Note: The syntax of .rules files are described in udev man page.

 

Changing file owner/group using udev rules

 

Let us see an example to change the owner/group to oracle/oinstall of device /dev/sdx using udev rules.

 

1. Add a file with name /etc/udev/rules.d/99-perm.rules which includes the below line. The file name can be anything with prefix of appropriate number like 99.

# vi /etc/udev/rules.d/99-perm.rules
KERNEL=="sdx", OWNER="oracle", GROUP="oinstall"

 

2. Test the new rule with “udevadm” command. As shown in the output below the rule is applied for the device /dev/sdx.

# udevadm test /block/sdx 2>&1 | grep "OWNER\|GROUP"
udev_rules_apply_to_event: OWNER ## /etc/udev/rules.d/99-perm.rules
udev_rules_apply_to_event: GROUP ## /etc/udev/rules.d/99-perm.rules

 

where ## shows UID/GID of oracle/oinstall.

 

3. Run udevadm to activate the new rule and check the changes:

# udevadm control –reload-rules
# udevadm trigger /block/sdx

 

4. Verify the permissions of the /dev/sdx device.

# ls -al /dev/sdx
brw-rw----. 1 oracle oinstall 202, 0 Feb  9 14:20 /dev/sdx

 

Note: It is not required to reboot the system after configuring the udev rules, But its recommended

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)