6.5 Modifying Udev Rules

The order in which rules are evaluated is important. Udev processes rules in lexical order. If you want to add your own rules, you need udev to find and evaluate these rules before the default rules.

The following example illustrates how to implement a udev rules file that adds a symbolic link to the disk device /dev/sdb.

  1. Create a rule file under /etc/udev/rules.d with a file name such as 10-local.rules that udev will read before any other rules file.

    For example, the following rule in 10-local.rules creates the symbolic link /dev/my_disk, which points to /dev/sdb:

    KERNEL=="sdb", ACTION=="add", SYMLINK="my_disk"

    Listing the device files in /dev shows that udev has not yet applied the rule:

    # ls /dev/sd* /dev/my_disk
    ls: cannot access /dev/my_disk: No such file or directory
    /dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
  2. To simulate how udev applies its rules to create a device, you can use the udevadm test command with the device path of sdb listed under the /sys/class/block hierarchy, for example:

    # udevadm test /sys/class/block/sdb
    run_command: calling: test
    udevadm_test: version ...
    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.
    udev_rules_apply_to_event: LINK 'my_disk' /etc/udev/rules.d/10-local.rules:1
    link_update: creating link '/dev/my_disk' to '/dev/sdb'
    node_symlink: creating symlink '/dev/my_disk' to 'sdb'
    udevadm_test: DEVNAME=/dev/sdb
    udevadm_test: DEVTYPE=disk
    udevadm_test: ACTION=add
    udevadm_test: SUBSYSTEM=block
    udevadm_test: DEVLINKS=/dev/my_disk
  3. Restart udev, use the start_udev command:

    # start_udev
    Starting udev:                         [OK]

    After Udev processes the rules files, the symbolic link /dev/my_disk has been added:

    # ls -F /dev/sd* /dev/my_disk
    /dev/my_disk@  /dev/sda  /dev/sda1  /dev/sda2  /dev/sdb

To undo the changes, remove your rules file and run start_udev again.