The purpose of a udev rule

udev

I'm being asked to add a udev rule for a new USB device I purchased. There doesn't seem to be a dirt simple explanation for why this is necessary or valuable to do. To always map a device to the same path? E.g. Always mount a device as /dev/ttyUSB1 or /dev/sda4?

In /etc/udev/rules.d/99-totalphase.rules:

# This file causes the mode of all Total Phase usb devices to be made
# writable for any user.

# Aardvark I2C/SPI Host Adapter
ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="0403", SYSFS{idProduct}=="e0d0", MODE="0666"
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0403", ATTR{idProduct}=="e0d0", MODE="0666"

# Beagle Protocol Analyzers
ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="1679", SYSFS{idProduct}=="2001", MODE="0666"
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="1679", ATTR{idProduct}=="2001", MODE="0666"

How to write udev rules

Best Answer

Always mapping a given device to the same location is one of the common uses of udev, indeed. Devices can even have multiple locations, for example a disk partition might be reachable via automated numbering (e.g. /dev/sda1) but also via the label on its filesystem (/dev/disk/by-label/*), via the UUID on its filesystem (/dev/disk/by-uuid/*), and by the serial number of the disk device (/dev/disk/by-id/*). If you want to access a particular device without having to care when it was plugged it or on what port, the clearest way to do it is to add a udev rule that matches this particular hardware device and creates an entry in /dev with a meaningful name, e.g.

ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="1679", ATTR{idProduct}=="2001", \
  SERIAL=="123456", MODE="0666", SYMLINK+="analyzer-foo"
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="1679", ATTR{idProduct}=="2001", \
  SERIAL=="123789", MODE="0666", SYMLINK+="analyzer-bar"

Another common purpose of udev rules is to control the permissions on the device node, generally to allow a particular daemon to access it. This is what the OWNER, GROUP, MODE and SECLABEL directives are for.

A third class of reasons is to trigger certain actions when a device is plugged in. For example, you may need to upload firmware to the device, or to choose in which mode it will be used, or notify some parts of the system that a new network connection or printer is available, etc.

Related Question