17

I'm making a custom USB HID device, and a desktop app to go with it, using HIDAPI on Lubuntu 16.04.3 LTS. We'll call it myapp for now.

Apparently, if I do $ ./myapp, libusb_open() fails with LIBUSB_ERROR_ACCESS. (shown in my debugger as -3; it took a while to figure that out because the enum doesn't seem to be documented very well)
But if I do $ sudo ./myapp, it succeeds.
I really don't want myapp to have root permissions, so how can I talk to my USB device without them?


I was hoping for an answer here, but it seems to have been abandoned at exactly this point. It says something about user permissions, but I can't seem to find that on my system. I thought I might find a group called usb or libusb that I could add myself to, but no such luck.


This question on a different SE site has an answer that uses some austere text files to change the permissions either globally (probably a bad idea) or for a specific device, but:

  1. It's not the "muggle's tweak" that I was looking for.
  2. Most of those files have a warning against modifying directly because they're auto-generated. So how can I be sure that a random package update isn't going to wipe out my changes and thus break myapp?
  3. It doesn't seem to do anything anyway. I followed those instructions and rebooted, and myapp with user permissions still can't talk to USB.
2

2 Answers 2

15

It's still not the "muggle's tweak" that I'm really looking for, but at least this works:

Apparently there are two directories for udev (I have no idea why):

  • /etc/udev/rules.d
  • /lib/udev/rules.d

I'd been messing with the /lib one and getting nowhere. I found the /etc one here, and it does work:


Put SUBSYSTEM=="usb", ATTRS{idVendor}=="VID", ATTRS{idProduct}=="PID", MODE="0666"

  • VID is the USB-IF-assigned Vendor ID of the device in question *

  • PID is the Vendor-assigned Product ID of the device in question *

  • 0666 gives universal read/write access to whatever matches this line

    * $ lsusb to see all attached USB devices and their ID's.

In /etc/udev/rules.d/xx-my-rule.rules (may need root/sudo permissions)

  • xx is any number > 50 (the defaults are in 50, and higher numbers take priority)
  • my-rule is whatever you want to call it
  • must end in .rules

Then udevadm control --reload-rules (may also need root/sudo permissions), and it should "just work" for that specific VID/PID pair.


Another option, to tighten the permissions a little bit more, is to use TAG+="uaccess" in place of MODE="0666". This limits access to the currently-logged-in (physical) user instead of all users. Thanks @Lekensteyn!

2
  • 3
    As an alternative to the overly permissive MODE="0666", consider using TAG+="uaccess" as used here. It does not allow any local user to access the USB device, just the person who is logged in (physically). There is no need for udevadm control --reload-rules, it is read automatically. You just have to remove/reinsert the USB device after creating/changing that file.
    – Lekensteyn
    Nov 27, 2017 at 23:52
  • That works too. Changed the file, ran the possibly unnecessary update command, rebooted (rather do all that than get frustrated for hours because I didn't tell it to update; some things do need that), and it still works. Thanks!
    – AaronD
    Nov 28, 2017 at 0:03
4

For anyone else struggling with this - i needed to add GROUP="plugdev" to my udev rule under Ubuntu 18.04 to make it work.

so for my BTD-400 adapter the file /etc/udev/rules.d/51-usb-device.rules reads:

SUBSYSTEM=="usb", ATTRS{idVendor}=="0a5c", ATTRS{idProduct}=="21e8", GROUP="plugdev", TAG+="uaccess"
2
  • I simply added the user to the plugdev group and it worked. I tried setting the group in the udev rule to one assigned to the user, but that failed. The udev rule may be unnecessary.
    – zymhan
    Jul 28, 2019 at 17:16
  • I just struggled with this to get Respeaker 'tuning.py' to work without sudo. I had to perform the procedure in the accepted answer (change udev rules), then I had to add myself to plugdev group (sudo usermod -a -G plugdev paul). After adding, plugdev does not show up in groups - need to reboot. BOTH plugdev membership AND udev rule modifications were necessary. May 21, 2021 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.