libusb cannot open USB device, permission isse. NetBeans/Ubuntu

raff5184 picture raff5184 · Mar 28, 2014 · Viewed 37.4k times · Source

I'm writing a C/C++ application in NetBeans based on libusb-1.0 on Ubuntu 12.04. I can get basic information from the USB device (for example, the interface description) but I am not able to open the device. The function libusb_open gives me the error:

libusb:error [op_open] libusb couldn't open USB device /dev/bus/usb/002/003: Permission denied.
libusb:error [op_open] libusb requires write access to USB device nodes.

I understand that I need to change the permissions but I don't know how (I am a very basic Linux-user). Thank you!

Answer

Preston picture Preston · Mar 28, 2014

I think the best way to do this is to create a udev rules file for your devices. Simply create a text file names something like myVendor.rules and put the following text in it (where 1234 is your vendor ID:

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", MODE="0666"
SUBSYSTEM=="usb_device", ATTRS{idVendor}=="1234", MODE="0666"

Put this udev file in your /etc/udev/rules.d/ directory. This udev file will grant read and write access to ALL users, include non-privileged users, for ALL USB devices that have a matching Vendor ID. This means your device is accessible to non-root users even without modifying your executable or running it with sudo.

This udev example is specific to the idVendor, but you can restrict it to a VID and PID to be more strict. Check this article for writing udev rules for more information.