Linux系统入门教程:在 Linux 中永久修改 USB 设备权限
提问:当我尝试在 Linux 中运行 USB GPS 接收器时我遇到了下面来自 gpsd 的错误。
<span class="pln">gpsd</span><span class="pun">[</span><span class="lit">377</span><span class="pun">]:</span><span class="pln"> gpsd</span><span class="pun">:</span><span class="pln">ERROR</span><span class="pun">:</span><span class="pln"> read</span><span class="pun">-</span><span class="pln">only device open failed</span><span class="pun">:</span><span class="typ">Permission</span><span class="pln"> denied</span>
<span class="pln">gpsd</span><span class="pun">[</span><span class="lit">377</span><span class="pun">]:</span><span class="pln"> gpsd</span><span class="pun">:</span><span class="pln">ERROR</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">ttyUSB0</span><span class="pun">:</span><span class="pln"> device activation failed</span><span class="pun">.</span>
<span class="pln">gpsd</span><span class="pun">[</span><span class="lit">377</span><span class="pun">]:</span><span class="pln"> gpsd</span><span class="pun">:</span><span class="pln">ERROR</span><span class="pun">:</span><span class="pln"> device open failed</span><span class="pun">:</span><span class="typ">Permission</span><span class="pln"> denied </span><span class="pun">-</span><span class="pln"> retrying read</span><span class="pun">-</span><span class="pln">only </span>
看上去 gpsd 没有权限访问 USB 设备(/dev/ttyUSB0)。我该如何永久修改它在Linux上的权限?
当你在运行一个会读取或者写入USB设备的进程时,进程的用户/组必须有权限这么做才行。当然你可以手动用chmod
命令改变 USB 设备的权限,但是手动的权限改变只是暂时的。USB 设备会在下次重启时恢复它的默认权限。
作为一个永久的方式,你可以创建一个基于 udev 的 USB 权限规则,它可以根据你的选择分配任何权限模式。下面是该如何做。
首先,你需要找出 USB 设备的 vendorID 和 productID。使用lsusb
命令。
<span class="pln">$ lsusb </span><span class="pun">-</span><span class="pln">vvv </span>
上面lsusb
的输出中,找出你的 USB 设备,并找出"idVendor"和"idProduct"字段。本例中,我们的结果是idVendor (0x067b)
和 idProduct (0x2303)
下面创建一个新的udev规则。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">udev</span><span class="pun">/</span><span class="pln">rules</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="lit">50</span><span class="pun">-</span><span class="pln">myusb</span><span class="pun">.</span><span class="pln">rules </span>
<span class="pln">SUBSYSTEMS</span><span class="pun">==</span><span class="str">"usb"</span><span class="pun">,</span><span class="pln"> ATTRS</span><span class="pun">{</span><span class="pln">idVendor</span><span class="pun">}==</span><span class="str">"067b"</span><span class="pun">,</span><span class="pln"> ATTRS</span><span class="pun">{</span><span class="pln">idProduct</span><span class="pun">}==</span><span class="str">"2303"</span><span class="pun">,</span><span class="pln"> GROUP</span><span class="pun">=</span><span class="str">"users"</span><span class="pun">,</span><span class="pln"> MODE</span><span class="pun">=</span><span class="str">"0666"</span>
用你自己的"idVendor"和"idProduct"来替换。MODE="0666"表示USB设备的权限。
现在重启电脑并重新加载 udev 规则:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> udevadm control </span><span class="pun">--</span><span class="pln">reload </span>
接着验证下 USB 设备的权限。
via: http://ask.xmodulo.com/change-usb-device-permission-linux.html