.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 <filename>

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

这个链接讲得很详细使用udev规则创建USB设备挂载点映射 - ROS小课堂

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 <your-favorite-text-editor> /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.

.rule文件的详细说明文档见 man 7 udev

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值