为啥 Python pyusb usb.core 访问由于权限而被拒绝,为啥 rules.d 不能修复它?
Posted
技术标签:
【中文标题】为啥 Python pyusb usb.core 访问由于权限而被拒绝,为啥 rules.d 不能修复它?【英文标题】:Why is Python pyusb usb.core access denied due to permissions and why won't the rules.d fix it?为什么 Python pyusb usb.core 访问由于权限而被拒绝,为什么 rules.d 不能修复它? 【发布时间】:2019-04-07 02:01:50 【问题描述】:我有一个 USB 设备,我想在 linux 上使用 pyusb 1.0.2 与之交谈(Linux tpad 4.15.0-38-generic #41~16.04.1-Ubuntu SMP Wed Oct 10 20:16:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux)。运行 python 3.5 我得到以下错误(这篇文章底部的完整跟踪):
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
安装 USB 并使用 lsusb 然后检查它:
udevadm info -a -p $(udevadm info -q path -n /dev/bus/usb/006/011)
表演
looking at device '/devices/pci0000:00/0000:00:10.0/usb6/6-1': KERNEL=="6-1" SUBSYSTEM=="usb" DRIVER=="usb" ATTRauthorized=="1" ATTRavoid_reset_quirk=="0" ATTRbConfigurationValue=="1" ATTRbDeviceClass=="00" ATTRbDeviceProtocol=="00" ATTRbDeviceSubClass=="00" ATTRbMaxPacketSize0=="8" ATTRbMaxPower=="98mA" ATTRbNumConfigurations=="1" ATTRbNumInterfaces==" 1" ATTRbcdDevice=="0111" ATTRbmAttributes=="80" ATTRbusnum=="6" ATTRconfiguration=="" ATTRdevnum=="11" ATTRdevpath=="1" ATTRidProduct=="0001" ATTRidVendor=="17a4" ATTRltm_capable=="no" ATTRmanufacturer=="Concept2" ATTRmaxchild=="0" ATTRproduct=="Concept2 Performance Monitor 3 (PM3)" ATTRquirks=="0x0" ATTRremovable=="unknown" ATTRserial=="300118412" ATTRspeed=="12" ATTRurbnum=="12" ATTRversion==" 1.10"
所以我在 /etc/udev/rules.d/10-local.rules 中写了一条这样的规则(仅供参考——我也试过“user1”,这是用户 python 显示它正在运行,我' ":=" 和 "=") 都试过了:
SUBSYSTEMS=="usb", ATTRSidVendor=="17a4", ATTRSidProduct=="0001", GROUP:="users", MODE="0777"
然后我运行了 udevadm test /devices/pci0000:00/0000:00:10.0/usb6/6-1
其中显示:
calling: test
version 229
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.
=== trie on-disk ===
tool version: 229
file size: 7049340 bytes
header size 80 bytes
strings 1759644 bytes
nodes 5289616 bytes
Load module index
timestamp of '/etc/systemd/network' changed
timestamp of '/lib/systemd/network' changed
Parsed configuration file /lib/systemd/network/99-default.link
Created link configuration context.
timestamp of '/etc/udev/rules.d' changed
Reading rules file: /etc/udev/rules.d/10-local.rules
Reading rules file: /lib/udev/rules.d/40-crda.rules
[removed long list of rule files...]
Skipping empty file: /etc/udev/rules.d/99-usbftdi.rules
rules contain 393216 bytes tokens (32768 * 12 bytes), 33403 bytes strings
25688 strings (211409 bytes), 22263 de-duplicated (181432 bytes), 3426 trie nodes used
GROUP 100 /etc/udev/rules.d/10-local.rules:1
MODE 0777 /etc/udev/rules.d/10-local.rules:1
value '[dmi/id]sys_vendor' is 'LENOVO'
value '[dmi/id]sys_vendor' is 'LENOVO'
IMPORT builtin 'usb_id' /lib/udev/rules.d/50-udev-default.rules:13
IMPORT builtin 'hwdb' /lib/udev/rules.d/50-udev-default.rules:13
MODE 0664 /lib/udev/rules.d/50-udev-default.rules:41
PROGRAM 'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11' /lib/udev/rules.d/69-libmtp.rules:1923
starting 'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11'
'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11'(out) '0'
Process 'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11' succeeded.
handling device node '/dev/bus/usb/006/011', devnum=c189:650, mode=0664, uid=0, gid=100
set permissions /dev/bus/usb/006/011, 020664, uid=0, gid=100
setting mode of /dev/bus/usb/006/011 to 020664 failed: Operation not permitted
setting owner of /dev/bus/usb/006/011 to uid=0, gid=100 failed: Operation not permitted
ACTION=add
BUSNUM=006
DEVNAME=/dev/bus/usb/006/011
DEVNUM=011
DEVPATH=/devices/pci0000:00/0000:00:10.0/usb6/6-1
DEVTYPE=usb_device
DRIVER=usb
ID_BUS=usb
ID_MODEL=Concept2_Performance_Monitor_3__PM3_
ID_MODEL_ENC=Concept2\x20Performance\x20Monitor\x203\x20\x28PM3\x29
ID_MODEL_FROM_DATABASE=Performance Monitor 3
ID_MODEL_ID=0001
ID_REVISION=0111
ID_SERIAL=Concept2_Concept2_Performance_Monitor_3__PM3__300118412
ID_SERIAL_SHORT=300118412
ID_USB_INTERFACES=:030000:
ID_VENDOR=Concept2
ID_VENDOR_ENC=Concept2
ID_VENDOR_FROM_DATABASE=Concept2
ID_VENDOR_ID=17a4
MAJOR=189
MINOR=650
PRODUCT=17a4/1/111
SUBSYSTEM=usb
TYPE=0/0/0
USEC_INITIALIZED=3850171749
Unload module index
Unloaded link configuration context.
但是,在执行此操作后,python 仍然会向 USB 设备报告访问错误。
Traceback (most recent call last):
File "/home/user1/PycharmProjects/PyRow/statshow.py", line 22, in <module>
erg = pyrow.pyrow(ergs[0])
File "/home/user1/PycharmProjects/PyRow/pyrow.py", line 61, in __init__
usb.util.claim_interface(erg, INTERFACE)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/util.py", line 205, in claim_interface
device._ctx.managed_claim_interface(device, interface)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 159, in managed_claim_interface
self.managed_open()
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 120, in managed_open
self.handle = self.backend.open_device(self.dev)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/backend/libusb1.py", line 786, in open_device
return _DeviceHandle(dev)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/backend/libusb1.py", line 643, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/backend/libusb1.py", line 595, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
我不知道如何解决这个问题。有什么建议吗?
【问题讨论】:
【参考方案1】:使用this answer 我发现我可以使用plugdev 组更正权限问题。
所以在我使用的终端中
groups
groups [myuserid]
并且经过验证的 plugdev 在那里,并且该用户是该组的一部分。然后我把下面这行放在 /etc/udev/rules.d/10-local.rules
SUBSYSTEMS=="usb", ENVDEVTYPE=="usb_device", ATTRSidVendor=="17a4", ATTRSidProduct=="0001", GROUP="plugdev", MODE="0777"
我不确定 devtype 以及 0777 或 0666 是否正确,但这是有效的。
进行更改后,我还运行了以下命令来重置系统规则:
sudo udevadm control --reload
sudo udevadm trigger
【讨论】:
以上是关于为啥 Python pyusb usb.core 访问由于权限而被拒绝,为啥 rules.d 不能修复它?的主要内容,如果未能解决你的问题,请参考以下文章
Windows 10 上与 python PyUSB 的简单通信 USB