If you use USB devices on Linux, you might have noticed that the automatically assigned device names like /dev/ttyUSB0
or /dev/sdb
can change every time the device is connected. To ensure that a specific USB device always gets the same name, you can create udev rules. In this article, I will guide you step-by-step on how to do this.
Why udev Rules?
Linux uses the udev
subsystem to manage devices and their assignments. With custom udev rules, you can ensure that a USB device always gets a fixed name based on its attributes. This is particularly useful when working with multiple USB devices.
Prerequisites
- Root privileges: You need administrator rights to create or modify udev rules.
- Basic Terminal knowledge: Basic familiarity with the terminal will help you follow the steps below.
Step-by-Step Guide
1. Identify Device Details
Before you can create a udev rule, you need to identify your USB device’s attributes. Connect the device to your system and find its current name. You can do this with:
ls /dev
Look for an entry like ttyUSB0
, sdb
, or similar, which represents your USB device.
Next, use the following command to display the details of your device:
udevadm info --query=all --name=/dev/<current_device_name>
Replace <current_device_name>
with the name you found earlier, e.g., ttyUSB0
.
Example output:
E: ID_VENDOR=SanDisk
E: ID_MODEL=Ultra_Fit
E: ID_SERIAL=1234567890ABCDEF
E: ID_BUS=usb
Note down values like ID_VENDOR
, ID_MODEL
, and ID_SERIAL
, as these will be used in your udev rule.
Alternative: View Device Attributes with attribute-walk
If you want to see additional attributes of your USB device, you can use:
udevadm info --attribute-walk --name=/dev/<current_device_name>
This command shows a tree structure with all available attributes of the device.
2. Create the udev Rule
Now you can create the rule that assigns a fixed name to your device. Open a new file in the /etc/udev/rules.d/
directory:
sudo nano /etc/udev/rules.d/99-usb.rules
Add the following rule, customized with your device’s attributes:
SUBSYSTEM=="usb", ATTR{idVendor}=="0781", ATTR{idProduct}=="5591", ATTR{serial}=="1234567890ABCDEF", SYMLINK+="my_usb_device"
Explanation of the rule:
SUBSYSTEM=="usb"
: The rule applies only to USB devices.ATTR{idVendor}
: The device’s vendor ID (hexadecimal value).ATTR{idProduct}
: The device’s product ID (hexadecimal value).ATTR{serial}
: The device’s serial number.SYMLINK+="my_usb_device"
: Creates a symbolic link/dev/my_usb_device
.
3. Apply and Test the Rules
After saving the rule, reload the udev rules with:
sudo udevadm control --reload
sudo udevadm trigger
Disconnect and reconnect the device, then check if the new name works:
ls -l /dev/my_usb_device
If the symbolic link appears, you’ve done everything correctly!
Advanced Options
What if there is no Serial Number?
If your device doesn’t have a serial number, you can use other attributes like KERNELS
or DEVPATH
. These can also be found using the attribute-walk
command.
Troubleshooting
If it doesn’t work on the first try, here are some tips:
- Rule not applied: Check if the file is correctly named and located in the
/etc/udev/rules.d/
directory. - Incorrect attributes: Ensure the attributes match your device.
- Check logs: Use the following logs to identify errors:
journalctl -u systemd-udevd
Conclusion
With udev rules, you can assign fixed names to USB devices on Linux, bringing order to your device management. This is especially useful for setups involving multiple USB devices. Try out the steps yourself and enjoy the newfound control over your USB devices!