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

纵然是瞬间 提交于 2019-12-03 08:19:27

Well you can run your application in root user mode and get rid with the permission issue. However you should be aware about the consequences of running the program in root user and should not be do until you fully understand the user/group permission on UNIX based system. However if it is your test machine, i think you can do the following(for program name ./a.out):

  1. Open the terminal
  2. Go to the directory where your program executable is present(check your netbeans creates the project and type cd "completepath).
  3. sudo ./a.out
  4. Now command prompt would ask to enter root password

Now you should be able to run the program successfully.

However if you want to provide/change the permission of read/write/execute for a particular user you should try to understand about chmod command. You would have to change the permission to directory(/dev/bus/usb/002/) and any particular file residing under this directory. Hope this information would be useful.

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.

JaeJun LEE

I think this might be a temporary solution for the problem while Preston's solution would work consistently.

1. Check which usb port is assigned for your device

You can figure out which usb port is assigned to your device by invoking ls command two times(first with device disconnected and second with device connected).

$ ls -l /dev/bus/usb/00*
/dev/bus/usb/001:
total 0
crw-rw-r-- 1 root root 189, 0  1월 10 12:08 001
crw-rw-r-- 1 root root 189, 1  1월 10 12:08 002

/dev/bus/usb/002:
total 0
crw-rw-r-- 1 root root 189, 128  1월 10 12:08 001
crw-rw-r-- 1 root root 189, 129  1월 10 12:08 002

/dev/bus/usb/003:
total 0
crw-rw-r-- 1 root root 189, 256  1월 10 12:08 001
crw-rw-r-- 1 root root 189, 257  1월 10 12:08 002
crw-rw-r-- 1 root root 189, 258  1월 10 12:08 003
crw-rw-r-- 1 root root 189, 259  1월 10 12:08 004
crw-rw-r-- 1 root root 189, 260  1월 10 12:08 005
crw-rw-r-- 1 root root 189, 263  1월 10 15:42 008 <-- this is your device

Let's say /dev/bus/usb/003/008 is your device.

2. Giving write permission for everyone(other)

According to the output of ls -l command, root user(group) has read/write permission on 003/008 port while other user has only read permission.

crw-rw-r-- 1 root root 189, 263  1월 10 15:42 008

You can allow every user to write on specific device using chmod command. While using chmod command, you will need sudo permission.

$ sudo chmod o+w /dev/bus/usb/003/008

or

$ sudo chmod a+w /dev/bus/usb/003/008

Now if you check the permission of usb, you have to see this output

$ ls -l /dev/bus/usb/003/008
crw-rw-rw- 1 root root 189, 263  1월 10 15:42 /dev/bus/usb/003/008

3. Everytime plugging it out or shutting down the system repeat step 1,2

If you plug the device out from usb port or shut down the system, what you did for your usb port will reset.

You have to repeat step 1,2 again.

This is why I'm saying my solution is temporary(volatile).

Further readings

I find these two blog articles would be helpful to your understanding.

Andrei Emeltchenko

After adding rule to /etc/udev/rules.d/ something like:

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="1234", MODE="0666", OWNER="YOU_USER_ID"

do not forget to reload rules with:

$ sudo udevadm control --reload-rules
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!