Create symbolic links to your devices (Linux, udev)

Last modified by henrikma@helsinki_fi on 2024/02/07 06:21

Devices (USB, serial, etc.) connected to the computer are usually found under /dev/ with names such as ttyACM0, ttyACM1, ttyS1, ttyUSB1. The name the device gets at boot can be random and depend on the order in which they are plugged in. This can cause problems in programs which use these device identifiers (such as spec). With the help of the udev device manager and known device properties such as vendor ID, device ID and serial number, symbolic links with a specified name can be created to always link to the correct devices.

Find out what device is where (e.g. if virtual serial/USB device is on ttyACM1):

  • You can search logs for connected devices (e.g.): dmesg | grep ttyACM*
  • You can also navigate to /dev/serial/by-id/ and type: ls -l

Find attributes that can be used to identify a specific device:

  • List all attributes of a device (e.g. ttyACM1): udevadm info --name=/dev/ttyACM1 --attribute-walk
  • idVendor and idProduct can also be found with the following command (shown as ID idVendor:idProduct): lsusb

Create udev rules file under /etc/udev/rules.d/XX-rulename.rules, where XX is a number that specifies the order in which the rule files are read in (XX=99 is read last) and rulename is a name of your choice:

  • The following line identifies a tty device by the attributes idVendor, idProduct and serial, and creates a symbolic link named ttyDevicename

    SUBSYSTEM=="tty", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", ATTRS{serial}=="9101112", SYMLINK+="ttyDevicename" 


Reboot the computer or run:

  • sudo udevadm control --reload-rules; sudo udevadm trigger

Verify that the symbolic link points to the correct device:

  • ls -l /dev/ttyDevicename
  • If you have multiple devices of same kind (e.g. same vendor ID, product ID, serial number), you will need to find attributes that are different for the devices
  • After the symbolic link was created successfully, you can use the link name (e.g. ttyDevicename) in your programs

Relevant links:

https://unix.stackexchange.com/a/183492

https://unix.stackexchange.com/a/445454