如何让 libusb 以非 root 身份工作?

Aar*_*onD 14 permissions usb

我正在制作一个自定义 USB HID 设备,以及一个与之配套的桌面应用程序,在 Lubuntu 16.04.3 LTS 上使用 HIDAPI。我们myapp现在就调用它。

显然,如果我这样做$ ./myapplibusb_open()失败了LIBUSB_ERROR_ACCESS(在我的调试器中显示为-3; 花了一段时间才弄清楚,因为枚举似乎没有很好地记录)
但是如果我这样做了$ sudo ./myapp,它就会成功。
我真的不想myapp拥有 root 权限,那么没有它们我怎么能与我的 USB 设备通信呢?


我希望在这里得到答案,但似乎在这一点上已经被放弃了。它说明了一些关于用户权限的内容,但我似乎在我的系统上找不到它。我想我可能会找到一个叫做的组usb或者libusb我可以将自己添加到,但没有这样的运气。


另一个 SE 站点上的这个问题有一个答案,它使用一些简单的文本文件来更改全局(可能是个坏主意)或特定设备的权限,但是:

  1. 这不是我要找的“麻瓜的调整”。
  2. 这些文件中的大多数都警告不要直接修改,因为它们是自动生成的。那么我如何确定随机包更新不会消除我的更改并因此中断myapp
  3. 无论如何它似乎没有任何作用。我按照这些说明重新启动,但myapp在用户权限下仍然无法与 USB 通话。

Aar*_*onD 13

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 对与此行匹配的任何内容进行通用读/写访问

    *$ lsusb查看所有连接的 USB 设备及其 ID。

/etc/udev/rules.d/xx-my-rule.rules(可能需要 root/sudo 权限)

  • xx 是任何大于 50 的数字(默认值为 50,数字越大优先)
  • my-rule 随便你怎么称呼它
  • 必须以 .rules

然后udevadm control --reload-rules(可能还需要 root/sudo 权限),它应该“适用于”特定的 VID/PID 对。


另一个选项,稍微收紧权限,是使用TAG+="uaccess"代替MODE="0666". 这限制了对当前登录(物理)用户而不是所有用户的访问。谢谢@Lekensteyn!

  • 作为过度宽松的 `MODE="0666"` 的替代方案,考虑使用 [此处](https://github.com/pwr/Solaar/blob/master/rules.d/ 42-logitech-unify-permissions.rules#L44)。它不允许任何本地用户访问 USB 设备,只允许(物理)登录的人。不需要`udevadm control --reload-rules`,它会自动读取。创建/更改该文件后,您只需移除/重新插入 USB 设备。 (3认同)