udev rules: stable USB device names when ttyUSB0 becomes ttyUSB1
Atlasbot · 20/8/2026 · 11 min read
The previous article ended with a practical problem: the robot has a LiDAR and an Arduino plugged into the USB together, launching the pre-configured ROS2 file /dev/ttyUSB0 for the LiDAR — but if plugged back in in a different order, the LiDAR may change to /dev/ttyUSB1, causing the node to misread data from the Arduino. udev rules solves this problem properly: assign a fixed name to each specific device, regardless of the order of insertion.
What is udev?
udev is a Linux daemon (background program) that listens to kernel events every time a device is plugged in/unplugged, then applies predefined rules to decide: what permissions to set for the device, what scripts to run, and — most importantly for robots — whether to create an additional device. symlink (symbolic link) with fixed name or not.
Core idea: /dev/ttyUSB0 can still be changed to the same number, but udev will create an additional constant subname (eg: /dev/lidar_front) pointing to the correct device — ROS2 configuration uses this subname instead of the original name.
Find device identification information
Before writing a rule, you need to know what the device has to recognize only — not just vendor/product ID (2 devices of the same type will have identical IDs) but preferably serial number unique to each physical device.
# Determine which ttyUSB device is
$ ls /dev/ttyUSB*
/dev/ttyUSB0
# View all detailed identification information
$ udevadm info -a -n /dev/ttyUSB0 | grep -E "idVendor|idProduct|serial"
ATTRS{idVendor}=="10c4"
ATTRS{idProduct}=="ea60"
ATTRS{serial}=="0001"
idVendor/idProduct identifies type device (eg: all boards using CP2102 chip have the same 2 numbers); serial — if the chip supports it — determines the correct physical device, which is different between 2 boards of the same type. Not all devices have a serial number (many cheap boards use the same fake serial number for all products of the same batch) — in this case, you need to rely on the physical USB port (KERNELS) instead of just the serial number.
Write rule file
Rules are saved as files .rules in /etc/udev/rules.d/, file names should start with numbers to control the order of application (convention: 99- for user custom rules):
$ sudo nano /etc/udev/rules.d/99-robot-devices.rules
# Assign a fixed name /dev/lidar_front to LiDAR with serial "0001"
SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{serial}=="0001", SYMLINK+="lidar_front"
# Assign a fixed name /dev/arduino_motor to Arduino
SUBSYSTEM=="tty", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0043", SYMLINK+="arduino_motor"
Each line is a rule: the conditions == must match all to apply, SYMLINK+= is the action — create an additional name pointing to the original device (do not replace /dev/ttyUSB0, both names exist and point to the same device).
Reload rules without restarting the computer
$ sudo udevadm control --reload-rules
$ sudo udevadm trigger
# Check if the symlink has been created
$ ls -l /dev/lidar_front
lrwxrwxrwx 1 root root 7 Aug 20 15:30 /dev/lidar_front -> ttyUSB0
If you still don't see the symlink, unplug the device and plug it back in — some rule changes only apply to the next time you plug in, not retroactively to the device that is already plugged in.
Technical details
Parameters
SYMLINK+= isn't the only thing a rule can do — several other useful fields are often combined in the same rule:
| School | Effects |
|---|---|
SYMLINK+="ten_co_dinh" | Create an additional name pointing to the original device |
MODE="0666" | Set read/write permissions directly for all users, no need to add to group dialout |
GROUP="dialout" | Assign specific group ownership to this device |
RUN+="/path/to/script.sh" | Automatically run a script every time the device matches a rule (eg: log when plugged in) |
Practical applications
Combining MODE="0666" right in the rule is a more thorough solution than adding a user to group dialout (discussed in the basic Linux article) in case the robot software runs as systemd service with a separate system user, no interactive login (no "log out then log in again" session for the new group to take effect) — setting access rights directly in the rule ensures that service is always available. You can read/write the device right from the first boot, regardless of whether the user running the service belongs to the correct group or not.
Practical example: use a fixed name in the launch file ROS2
After having /dev/lidar_front fixed, the launch file no longer depends on the insertion order:
# Before: fragile when reinstalled in a different order
Node(package="ydlidar_ros2_driver", parameters=[{"port": "/dev/ttyUSB0"}])
# After: always correct device regardless of insertion order
Node(package="ydlidar_ros2_driver", parameters=[{"port": "/dev/lidar_front"}])
Common mistakes
- Rule has no effect — syntax error in file
.rules(usually missing==or,), check withsudo udevadm test $(udevadm info -q path -n /dev/ttyUSB0)to see detailed log of rule matching process. - Forgot to reload after editing files — always run
udevadm control --reload-rules && udevadm triggeragain after each edit, or unplug/replug the device. - 2 devices of the same type share the same fake serial — the serial-based rule will apply to both, creating duplicate symlinks. Solution: use additional condition
KERNELS=="1-1.2"(specific physical USB port) if the device is always permanently plugged into a port on the robot. - Rule is syntactically correct but still does not match — check that the exact value
idVendor/idProduct/serialis equal toudevadm info, one mistyped character (eg0andO) is enough for the rule to never match.
Conclusion
udev rules is the right solution — instead of guessing what the serial number /dev/ttyUSBx will be, assign a fixed name to each specific physical device right from the operating system layer. The next article in the column turns to Docker — another way to solve the "heterogeneous software environment" problem when sharing robotics code across multiple machines.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.