Awesome Linux Tips: Rename USB-Serial devices in Linux to be More Usable

Awesome Linux Tips: Rename USB-Serial devices in Linux to be More Usable

Renaming USB-Serial devices to be presistent in Linux

example: (/dev/ttyUSBx -> /dev/custom-name)


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 udev 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 /udev directory for all types of devices.

udev Rules File

Udev rules are defined into files with the .rules extension. There are two main locations in which those files can be placed: /usr/lib/udev/rules.d it’s the directory used for system-installed rules, /etc/udev/rules.d/ 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 /etc/udev/rules.d/, 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 /dev/ttyUSBx , 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 /etc/udev/rules.d/99-usb-serial.rules , note that any .rules 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}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="rah-ctrl"

These attributes I have extracted it as mentioned before using udevadm info , 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 ATTRS{serial} as this attribute should be unique for each cable
  • SYSLINK+="name" this will command udev to assign this name for this device and you can access it using this device node /dev/name

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.

Ask me

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.