为什么由于权限拒绝了python

为什么由于权限拒绝了python

本文介绍了为什么由于权限拒绝了python pyusb usb.core访问,为什么rules.d无法解决该问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个USB设备,正准备与在Linux上使用pyusb 1.0.2交谈(Linux tpad 4.15.0-38-generic#41〜16.04.1-Ubuntu SMP,星期三,10月10日20:16:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux).运行python 3.5时,我收到以下错误(在这篇文章的底部有完整的跟踪):

I have a USB device which I'm tring to talk to using pyusb 1.0.2 on linux (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). Running python 3.5 I get the following error (full trace at bottom of this post):

usb.core.USBError: [Errno 13] Access denied (insufficient permissions)

安装USB并使用lsusb然后对其进行检查:

Mounting the usb and using lsusb and then inspecting it:

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"
        ATTR{authorized}=="1"
        ATTR{avoid_reset_quirk}=="0"
        ATTR{bConfigurationValue}=="1"
        ATTR{bDeviceClass}=="00"
        ATTR{bDeviceProtocol}=="00"
        ATTR{bDeviceSubClass}=="00"
        ATTR{bMaxPacketSize0}=="8"
        ATTR{bMaxPower}=="98mA"
        ATTR{bNumConfigurations}=="1"
        ATTR{bNumInterfaces}==" 1"
        ATTR{bcdDevice}=="0111"
        ATTR{bmAttributes}=="80"
        ATTR{busnum}=="6"
        ATTR{configuration}==""
        ATTR{devnum}=="11"
        ATTR{devpath}=="1"
        ATTR{idProduct}=="0001"
        ATTR{idVendor}=="17a4"
        ATTR{ltm_capable}=="no"
        ATTR{manufacturer}=="Concept2"
        ATTR{maxchild}=="0"
        ATTR{product}=="Concept2 Performance Monitor 3 (PM3)"
        ATTR{quirks}=="0x0"
        ATTR{removable}=="unknown"
        ATTR{serial}=="300118412"
        ATTR{speed}=="12"
        ATTR{urbnum}=="12"
        ATTR{version}==" 1.10"

所以我在/etc/udev/rules.d/10-local.rules中这样写了一条规则(仅供参考-我也尝试过"user1",这是用户python显示它正在运行,而我ve都尝试了:="和"="):

So I wrote a rule in /etc/udev/rules.d/10-local.rules like this (FYI -- I've also tried "user1" which is the user python shows it is running under and I've tried both ":=" and "="):

    SUBSYSTEMS=="usb", ATTRS{idVendor}=="17a4", ATTRS{idProduct}=="0001", GROUP:="users", MODE="0777"

然后我运行了udevadm test/devices/pci0000:00/0000:00:10.0/usb6/6-1

Then I ran 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设备的访问错误.

However after doing this python still reports access errors to the USB device.

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)

关于如何解决此问题,我没有主意.有什么建议吗?

I'm out of ideas on how to fix this. Are there any suggestions?

推荐答案

使用我发现我可以使用plugdev组更正权限问题.

Using this answer I found I could correct the permissions problem using the plugdev group.

所以在我使用的终端机中

So in a terminal I used

groups
groups [myuserid]

和经过验证的plugdev在那儿,并且用户属于该组.然后,将以下行放在/etc/udev/rules.d/10-local.rules

and verified plugdev was there and the user was part of that group. Then I put the following line in /etc/udev/rules.d/10-local.rules

我不确定devtype以及0777或0666是否正确,但这是可行的.

I'm not sure if the devtype and if 0777 or 0666 is proper, but this is what worked.

进行更改后,我还运行了以下命令来重置系统规则:

After making the changes I also ran the following commands to reset the rules for the system:

sudo udevadm control --reload
sudo udevadm trigger

这篇关于为什么由于权限拒绝了python pyusb usb.core访问,为什么rules.d无法解决该问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:11