.rules文件创建

有时候有些设备(例如usb can 分析仪,usb 串口)等需要root权限才能读写。

下面介绍一种方法,可以不用root 权限就能读写。

首先在命令行下使用命令lsusb 查看usb 设备,比如下面的就是我在自己电脑上面使用这条命令的结果

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c077 Logitech, Inc. M105 Optical Mouse
Bus 001 Device 002: ID 413c:2107 Dell Computer Corp. 
Bus 001 Device 027: ID 0471:1200 Philips (or NXP) 
Bus 001 Device 028: ID 0781:5591 SanDisk Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

要知道哪一个对应着我们想要读写的usb口只需要把那个usb口拔掉再执行一次,看两次的结果差了哪个就知道了。

我的can设备是:Bus 001 Device 027: ID 0471:1200 Philips (or NXP)

注意ID 0471:1200这个字段,对我们很有用,可以把它理解为一个身份证号码,系统通过这个号码可以在众多usb中找到它。

接着进入到/etc/udev/rules.d目录下,创建一个新文件can_usb.rules,文件名可以自定义,扩展名使用.rules就好。

然后打开这个新创建的文件,在里面添加下面一行:

ACTION=="add",SUBSYSTEM=="usb", ATTRS{idVendor}=="0471", ATTRS{idProduct}=="1200", MODE:="0777",SYMLINK+="usb_can_II"

注意

ATTRS{idVendor}=="0471" 就是上面说的ID 0471:1200里面的0471, ATTRS{idProduct}=="1200"就是上面说的ID 0471:1200里面的1200,后面的SYMLINK+="usb_can_II"中的usb_can_II可以自己定义。写完保存,在该文件所在目录下(即/etc/udev/rules.d)运行命令行程序

sudo chmod 777 

然后重新插拔USB口就可以了。 

这个链接讲得很详细https://www.corvin.cn/474.html

 

First, run lsusb, then plug in your USB peripheral and run lsusb again. Compare the two listings: the second listing should have at least one extra line that describes your peripheral.

Once you've identified the correct line, make a note of the ID xxxx:yyyy numbers on that line. These are the vendor ID and product ID for the device.

But first, some principles about udev rule files:

You can add any udev rule files you want to /etc/udev/rules.d: the only name requirement is that the filename should have a .rules suffix.

The files in /lib/udev/rules.d are reserved for the pre-packaged rules of your Linux distribution: any changes you make to any existing files in there will be overwritten when a new patch affecting the udev rules is installed to the system.

If you need to modify the existing rules, you should instead copy the rule file you wish to modify from /lib/udev/rules.d to /etc/udev/rules.d, keeping the original name of the file. Any files in /etc/udev/rules.d will override files with identical names in /lib/udev/rules.d. Once you've made the copy, you can modify the copy in /etc/udev/rules.d as you wish, secure in the knowledge that your changes won't be overwritten at some future point when some security updates are installed.

The rule files in both directories are read (after taking into account the overrides) in a simple alphanumeric order, and if there are conflicting rules, then the last one wins. There is a convention that the rule file name should be something like NN-descriptive-name.rules, where NN identifies the place of this file in the overall ordering of the rules.

Then to the actual task of writing the rule.

If the ID for your device in the lsusb listing was xxxx:yyyy, then the part of the udev rule that specifies the device would be:

SUBSYSTEM=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy"
Note the doubled equals signs. The convention is similar to C, Java and certain other programming languages: a doubled equals sign means testing for equality, a single equals sign means setting something to a particular value.

In most modern versions of Linux, you can use TAG+="uaccess" at the end of your rule line to specify that a particular device should be accessible by whoever is currently logged in to the system locally.

If you need only some users to be able to access the device, create a group (sudo groupadd mydevice), add the users to the group (usermod -a -G mydevice username) and make the device accessible by that group only with GROUP="mydevice", MODE="0660" at the end of your rule line. Then people added to the group can e.g. use ssh to connect to the system remotely and still use the device; people that are not members of the group won't able to use the device at all.

Note: new group memberships will take effect at your next login, so if you add yourself to the new group, you'll need to logout and log back in before testing the device.

If you want to allow everyone on the system to access the device, you can just specify MODE="0666" at the end of your rule line. (You should think twice before doing this.)

Putting it all together

So, if you don't have a particular need to modify any existing rule file, you can just create your own, e.g. /etc/udev/rules.d/99-mydevice.rules.

sudo  /etc/udev/rules.d/99-mydevice.rules
If you want to just allow the locally logged-in user to use the device, the contents of the file should be like this:

SUBSYSTEM=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", TAG+="uaccess"
After you've saved the rule file, run this command to make your new rule take effect immediately:

udevadm control --reload-rules
If you instead used the group-based solution, logout and log back in at this point.

 

你可能感兴趣的:(linux)