为什么 PyUSB / libusb 在 Linux 上需要 root (sudo) 权限?
我最近一直在尝试 PyUSB,发现它在 Linux 上运行得很好(Ubuntu 有 libusb 0.1 和 1.0,以及 OpenUSB)...但前提是我以root权限运行该程序(当然,使用sudo
)。
谁能告诉我为什么它需要提升的权限,更重要的是,我是否可以以某种方式更改权限以使其适用于普通用户帐户?
I have been toying around with PyUSB lately, and found that it works beautifully on Linux (Ubuntu has libusb 0.1 and 1.0, as well as OpenUSB)... but only if I run the program with root privileges (with sudo
, of course).
Can anyone tell me why it requires elevated privileges and, more importantly, if I can change the permissions somehow to make it work for normal user accounts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过创建 udev 规则来更改 USB 设备节点的权限。
例如,我将以下行添加到
/etc/udev/rules.d/
中的文件中,这将设备节点的所有者设置为
root:usbusers
而不是root:root
将自己添加到
usbusers
组后,我可以访问该设备。You can change the permissions of your usb device node by creating a udev rule.
e.g. I added the following line to a file in
/etc/udev/rules.d/
This sets the owner of the device node to
root:usbusers
rather thanroot:root
After adding myself to the
usbusers
group, I can access the device.请参阅我在这里给出的答案:
如何使用 pyusb 与此设备通信?
即:
为您希望普通用户能够访问的特定设备设置 udev 规则文件。这将定义供应商 ID、产品 ID 和组。
可以使用 lsusb 命令找到供应商和产品 ID。
1.
创建一个 udev 规则文件
将其放入名为(例如)/lib/udev/rules.d/50-YourSoftwareName.rules 的文件中(在 man udev 中挖掘文件命名规则)
注意:旧的命名约定使用 /etc/udev/rules.d/filename.rules,但已更改。
2.
将用户名添加到plugdev组
adduser usernameplugdev
3.
强制 udev 系统查看您的更改
sudo udevadm control --reload
(即减去重新加载)sudo udevadm 触发器
4.
拔下并重新插入设备
或者
重新启动计算机
最终结果应该是 Plugdev 组的所有成员现在都能够访问该设备。
编辑:
请注意,在某些系统上,
plugdev
组可能不是您需要的组。根据我的经验,它也可以是组输入
,具体取决于您插入的内容。See the answer that I gave here:
How can I comunicate with this device using pyusb?
Namely:
Set up a udev rules file for the specific device that you want normal users to be able to access. This will define the vendor id, the product id and a group.
The vendor and product id's can be found using the
lsusb
command.1.
Create a udev rules file
Put this in a file called (for example) /lib/udev/rules.d/50-YourSoftwareName.rules (dig around in man udev for file naming rules)
NOTE: The old naming convention used /etc/udev/rules.d/filename.rules, that has changed.
2.
add the user names to the plugdev group
adduser username plugdev
3.
force the udev system to see your changes
sudo udevadm control --reload
(that is minus minus reload)sudo udevadm trigger
4.
unplug and replug the device
or
reboot your machine
The end result should be that all members of the group plugdev will now be able to access the device.
EDIT:
Note that on some systems the group
plugdev
may not be the group that you need. It can also be the groupinput
in my experience, depending on what you are plugging in.libusb
允许您以任意方式操作任意 USB 设备。例如,您可以格式化外部 USB 硬盘。一般来说,所有直接硬件访问都需要 root 权限,尽管我猜想实际上不需要完整的 root 权限,但您应该只需要 CAP_SYS_RAWIO 就可以了>。
libusb
allows you to manipulate arbitrary USB devices in arbitrary ways. You could format an external USB harddisk, for example.In general, all direct hardware access requires
root
privileges, although I guess that actually fullroot
privileges are not required, you should be fine with justCAP_SYS_RAWIO
.