.rules文件创建

2024-04-06 21:32
文章标签 rules 文件创建

本文主要是介绍.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口就可以了。 

这个链接讲得很详细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 togetherSo, 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.

 

这篇关于.rules文件创建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/880871

相关文章

Python业务规则引擎库之rules使用详解

概要 在软件开发中,业务规则引擎是一种重要的工具,可以帮助开发者将复杂的业务逻辑从代码中解耦出来,并以更直观的方式进行管理和维护。rules 是一个轻量级的 Python 库,专门用于定义和执行业务规则。它提供了一种简洁且强大的方式来管理应用程序中的规则逻辑,使代码更加简洁、可读和可维护。本文将详细介绍 rules 库,包括其安装方法、主要特性、基本和高级功能,以及实际应用场景,帮助

[HeadFirst] Static Memeber Final Rules

1. Static Member static kw标记出不需要类实例的方法。一个静态方法代表说“一种不依靠实例变量也就是不需要对象的行为”。 静态变量 - 静态方法 实例变量 - 实例方法(这里的实例指的是只能通过实例访问的非静态的变量和方法) 1.1 静态方法不能调用实例变量(非静态的变量) 静态的方法是不知道堆上有哪些实例的,因为静态方法没有维护一个对象的引用(即指向堆对象的

PostgreSQL的视图pg_rules

PostgreSQL的视图pg_rules pg_rules 是 PostgreSQL 中的一个系统视图,用于显示数据库中存在的规则(rules)的相关信息。规则是一种允许在表的查询、插入、更新或删除操作上定义自定义行为的机制。通过查询 pg_rules 视图,数据库管理员和开发人员可以查看当前数据库中定义的所有规则信息。 pg_rules 视图的主要列 列名类型描述schemanamena

【Bazel入门与精通】 rules之属性

https://bazel.build/extending/rules?hl=zh-cn#attributes Attributes An attribute is a rule argument. Attributes can provide specific values to a target’s implementation, or they can refer to other t

Apple开发者macOS描述文件创建

1.选择Profiles然后点击加号创建 2.选择类型为macOS App Development然后点击继续  3.选择描述类型与App ID 然后点击继续  4.选择证书然后点击继续  5.选择设备,然后点击继续 6.输入描述文件后,点击生成   生成成功,点击下载描述文件  下载完成会自动打开描述文件

Codeforces Round #289 (Div. 2, ACM ICPC Rules) (A, B, C, E)

A:水题,根据题目预处理一下输出即可 B:先把最大和最小找出来,可以让最小全是1,然后最大比最小多出的部分就放1,2,3,4,5...所以如果MAX - MIN > k就是NO,不然就根据这个构造出答案 C:贪心的策略,每次要让数字尽量小,那么就和上一个数字比较,如果需要的和比上一个小,就先找到一个新数字,使得和小于所需数字,并且该数字是大于上一个数字的最小值,找的方法就是从末尾不断放0进位。

Android混淆 proguard-rules.pro 常用配置模板

#下面代码中的路径配置,你要修改成与你相对应的路径#引入依赖包rt.jar(jdk路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)#-libraryjars 'C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar'#引入依赖包android.jar(android SDK路径)(注意:如在makeJar的时候提示指定了两次,可以

npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法

目录 一、问题描述二、问题原因三、解决方法 一、问题描述 npm build 报错: Syntax Error: Error: Cannot find module 'webpack/lib/rules/BasicEffectRulePlugin' 二、问题原因 各种操作把环境弄乱了。 三、解决方法 尝试了无数种方法,最后删除项目,重新下载源码,重新编译解决。

android studio 的aidl 文件创建步骤

1.创建好Book.java后,选中如图所示的aidl文件夹后点击右键,而不是‘选中项目文件夹然后右键’,原因是‘同时必须要指明包名,包名必须和java目录下的包名一致’。 2.因为新建AIDL文件时,如果把它命名为Book,就会提示interface name must be unique时,所以可以随意命名【这里我先命名为Booka.aidl】,新建完成后再重命名为Book.aidl。

ubuntu 为.sh文件创建桌面图标

1,在桌面创建 文件名.desktop文件。  2,文件内输入:  [Desktop Entry]  Name = Studio  Comment= android studio  Exec=/home/你的用户名/android-studio/bin/文件名.sh  Icon=/home/你的用户名/android-studio/bin/文件名.png  Terminal=false  Type