在Linux*问USB设备(libusb-1.0)?

时间:2022-05-31 16:13:34

I am writing a small program to communicate with a specific USB HID product (identified by vendor and product IDs), using libusb-1.0 on Linux. Right now, I have to run the program as root because "libusb requires write access to USB device nodes". Is there a way to change the permissions on the device I need so that I don't need to run my program as root?

我正在编写一个小程序,使用Linux上的libusb-1.0与特定的USB HID产品(由供应商和产品ID标识)进行通信。现在,我必须以root身份运行程序,因为“libusb需要对USB设备节点进行写访问”。有没有办法更改我需要的设备的权限,以便我不需要以root身份运行我的程序?

1 个解决方案

#1


10  

On modern Linux systems, udevd (man 7 udev) creates the device nodes for USB devices when they're plugged in. Add a udev rule that matches your device (eg. you could match by USB Vendor and Product IDs), and sets the OWNER / GROUP / MODE of the device node.

在现代Linux系统上,udevd(man 7 udev)在插入USB设备时为其创建设备节点。添加与您的设备匹配的udev规则(例如,您可以通过USB供应商和产品ID进行匹配),并设置设备节点的所有者/组/模式。

The best approach is probably to create a new group for users who should be able to access the device, then set that as the group owner in the udev rule. You may also need to use MODE to ensure that it has group read/write permissions. Eg. your rule will probably look something like:

最好的方法可能是为应该能够访问设备的用户创建一个新组,然后将其设置为udev规则中的组所有者。您可能还需要使用MODE来确保它具有组读/写权限。例如。你的规则可能看起来像:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffee", ATTRS{idProduct}=="5a5a", MODE="0660", GROUP="foobar"

#1


10  

On modern Linux systems, udevd (man 7 udev) creates the device nodes for USB devices when they're plugged in. Add a udev rule that matches your device (eg. you could match by USB Vendor and Product IDs), and sets the OWNER / GROUP / MODE of the device node.

在现代Linux系统上,udevd(man 7 udev)在插入USB设备时为其创建设备节点。添加与您的设备匹配的udev规则(例如,您可以通过USB供应商和产品ID进行匹配),并设置设备节点的所有者/组/模式。

The best approach is probably to create a new group for users who should be able to access the device, then set that as the group owner in the udev rule. You may also need to use MODE to ensure that it has group read/write permissions. Eg. your rule will probably look something like:

最好的方法可能是为应该能够访问设备的用户创建一个新组,然后将其设置为udev规则中的组所有者。您可能还需要使用MODE来确保它具有组读/写权限。例如。你的规则可能看起来像:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffee", ATTRS{idProduct}=="5a5a", MODE="0660", GROUP="foobar"