Persistent names for USB-serial devices in Linux (/dev/ttyUSBx -> /dev/custom-name)

Motivation

Did you get bored for each time you re-connect the USB-serial device you have to check if it is the right one or not? This can be solved now with the command, let’s see in the upcoming paragraphs.

What is `udev`?

Udev is the device manager for the Linux kernel. Udev dynamically creates or removes device node files at boot time in the directory for all types of devices.

` Rules File

Udev rules are defined into files with the extension. There are two main locations in which those files can be placed: it’s the directory used for system-installed rules, is reserved for custom “user” made rules.

The files in which the rules are defined are conventionally named with a number as prefix (e.g 50-udev-default.rules) and are processed in lexical order independently of the directory they are in. Files installed in , however, override those with the same name installed in the system default path.

Using `udevadm` to extract information from the USB-serial devices

Using the following command you will get all the info you need for the device name , where x is the device number

$ udevadm info --name=/dev/ttyUSBx --attribute-walk

This will give you a lot of info, you can use any of these for your rules next in the rules file. I recommend you to save the log of this command to a file and use a comparing tool to find where the devices diverge and differ in attributes.

Writing the rules file

Use any text editor to create a new file in this path , note that any file will get parsed in lexical order. I will use vim (with sudo).

$ sudo vim /etc/udev/rules.d/99-usb-serial.rules

I’ve populated my file with the following

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{serial}=="FTAK3IN7", SYMLINK+="rah-m4"SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTAK3AE8", SYMLINK+="rah-a35"SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="rah-ctrl"

These attributes I have extracted it as mentioned before using , I will highlight some points:

  • I have 3 USB-serial cables connected
  • I have 2 identical cables from the same vendor but I could differentiate between them using the as this attribute should be unique for each cable
  • this will command to assign this name for this device and you can access it using this device node

Apply these changes without restart

Just run this command

$ sudo udevadm control --reload-rules && sudo udevadm trigger

And that is it. You will not run again in this problem, even if the machine has started or even if you have re-connected the cables in different ports.

I hope this article is helpful for you, please share if you like it.

If you have any questions, comment down below and I will try my best to answer them.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Islam Negm (Mr.Bit)

Software Engineer (Developing , Electronics, Embedded, Linux , Git, js, python).