Linux creates a device file under /dev for each hardware device when it is attached.
By default these files are owned by root and belong to a system group (for example root or dialout).
You can use udev rules to set a custom owner, group, and permissions so that non-root users can access the device.
Prerequisites
Before you begin:
- A Linux system with
udevinstalled. - Root or sudo access.
- Basic familiarity with the command line.
1. Identify the Device Attributes
First find the device’s attributes that udev can match:
udevadm info -a -n /dev/ttyUSB0
Look for lines under ATTR{idVendor} and ATTR{idProduct} or other unique identifiers.
2. Choose Owner, Group, and Mode
Decide which user and group should own the device file and what permission bits it needs. For example:
- Owner: your Linux username
- Group:
plugdev(or create a custom group) - Mode:
0660(read and write for owner and group)
Make sure your user is in the chosen group:
sudo usermod -aG plugdev your_username
3. Create a udev Rule
Create a new rule file in /etc/udev/rules.d/. Use a high number so it loads late, for example 99-device-perms.rules:
sudo tee /etc/udev/rules.d/99-device-perms.rules <<EOF
# Set owner and group for USB device
SUBSYSTEM=="usb", ATTR{idVendor}=="abcd", ATTR{idProduct}=="1234", OWNER="your_username", GROUP="plugdev", MODE="0660"
EOF
Replace abcd and 1234 with your device’s vendor and product IDs, and your_username with your Linux user name.
4. Reload udev Rules and Trigger
Tell udev to reload its rules and apply them to existing devices:
sudo udevadm control --reload
sudo udevadm trigger
5. Verify the New Owner and Group
Check the device file’s permissions:
ls -l /dev/ttyUSB0
You should see your user and the plugdev group as owner and group.
Conclusion
You have now set a custom owner, group, and permissions for your device file using udev rules. This method ensures that every time the device is plugged in, udev applies the correct settings automatically.
