为什么 PyUSB / libusb 在 Linux 上需要 root (sudo) 权限?

发布于 2024-09-19 22:56:43 字数 370 浏览 4 评论 0原文

我最近一直在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别再吹冷风 2024-09-26 22:56:43

您可以通过创建 udev 规则来更改 USB 设备节点的权限。
例如,我将以下行添加到 /etc/udev/rules.d/ 中的文件中,

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0664", GROUP="usbusers"

这将设备节点的所有者设置为 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/

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0664", GROUP="usbusers"

This sets the owner of the device node to root:usbusers rather than root:root

After adding myself to the usbusers group, I can access the device.

哭了丶谁疼 2024-09-26 22:56:43

请参阅我在这里给出的答案:
如何使用 pyusb 与此设备通信?

即:
为您希望普通用户能够访问的特定设备设置 udev 规则文件。这将定义供应商 ID、产品 ID 和组。
可以使用 lsusb 命令找到供应商和产品 ID。

1.
创建一个 udev 规则文件

ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="171b", ATTRS{idProduct}=="2001", MODE="660", GROUP="plugdev"

将其放入名为(例如)/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

ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="171b", ATTRS{idProduct}=="2001", MODE="660", GROUP="plugdev"

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 group input in my experience, depending on what you are plugging in.

一瞬间的火花 2024-09-26 22:56:43

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 full root privileges are not required, you should be fine with just CAP_SYS_RAWIO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文