Using UDEV rules to ensure a USB serial device always links to the same location in /dev/

If you're using several USB serial adapters on a linux machine, you may have found that their /dev/ names change after a reboot. This can be frustrating if each serial port is plugged into a specific device and you want to always access it using the same /dev/ name.

Fortunately, udev rules exist that allow you to symlink a serial device to a name, persistently. Udev rules are very powerful and support lots of tweaking, but for now I will cover the basics of assigning a USB serial device a static name.

First of all you will need to identify your USB serial adapters vendor id, and its product id. Fortunately the lsusb command makes this nice and easy.

First of all, run lsusb-
sudo lsusb

You should see a list of all your USB devices, each showing the VendorID:ProductID. Lookout for the USB-Serial adapter-
Bus 001 Device 005: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Then create a new udev rule file for your usb serial devices-
sudo nano /etc/udev/rules.d/99-usb-serial.rules

Add something like the following, ensuring your Vendor and Product ID's match those shown in lsusb. The SYMLINK section allows you to set the permanent name. I've chosen to call mine NextionDisplay-
SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="NextionDisplay"

Reboot your machine, and you should see your device's name in the /dev/ directory-
pi@mmdvm(rw):~$ ls /dev/ | grep Nextion

I hope that helps!

1 comment:

  1. If you have more than one of a device, use the serial number to differentiate - unless it's a cheap RTL-SDR in which case they all seem to report a serial of 0000000001 :-/