Static names for serial USB devices on Linux
Friday, September 10, 2021
Today’s BLOG entry is about a simple hack that can make your life easier when working with serial USB devices under Linux (Ubuntu/Debian). Every time a device is attached, an automatically generated device name is assigned to it by the OS (e.g. /dev/ttyUSB1). When you work a lot with embedded devices, using their serial USB devices for developing and testing, it could be a real pain to always change their names in a terminal program or some config files. Especially when using multiple devices concurrent, connecting and disconnecting them in different order, creates a device name lottery and very quickly you no longer know which device is connected to which device name.
But there is a solution to this dilemma: udev! Udev (userspace /dev) allows to define rules which are executed based on connected devices properties. These rules can do many things and are defined in text files normally found at /etc/udev/rules.d. For this problem we will define a rule which links a fixed name to the OS generated device name for a serial port on a FPGA development board. The following steps describe the process and work also with other devices.
- disconnect the device
- connect the device
- identify device
dmesg | grep ttyUSB
[178170.685775] usb 2-2.4.2: FTDI USB Serial Device converter now attached to ttyUSB0 [178170.688577] usb 2-2.4.2: FTDI USB Serial Device converter now attached to ttyUSB2
- The device in question is /dev/ttyUSB2. Now get further information about this device.
udevadm info --name=/dev/ttyUSB2 --attribute-walk
This gives a lot of information. Look for the ‘idProduct’ and ‘idVendor’ attributes. As some devices can register multiple serial devices on one USB device be careful to get the attributes of the correct serial device.
ATTRS{idProduct}=="0608" ATTRS{idVendor}=="05e3"
- Using this attribute values we can define an udev rule which is triggered as soon as a device with matching attribute values is connected. Note that if you want to use two identical devices which would have the same vendor and product attribute values, you must include an additional attribute like a serial number which should be unique to distinguish them. This udev rule creates a symlink named ‘arty’ to the OS selected device name thus, by using the symlink name in a terminal program or config file, you never have to think of using the correct device name.
SUBSYSTEM=="tty", ATTRS{idVendor}=="05e3", ATTRS{idProduct}=="0608", SYMLINK+="arty"
- As last step the rule needs to be activated with:
sudo udevadm trigger
- To check that that the symlink was created:
ls -la /dev/a*
lrwxrwxrwx 1 root root 7 Sep 10 12:18 /dev/arty -> ttyUSB2
Problem solved!