.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

相关文章

【0324】Postgres内核 Shared Buffer Access Rules (共享缓冲区访问规则)说明

0. 章节内容 1. 共享磁盘缓冲区访问机制 (shared disk buffers) 共享磁盘缓冲区有两套独立的访问控制机制:引用计数(a/k/a pin 计数)和缓冲区内容锁。(实际上,还有第三级访问控制:在访问任何属于某个关系表的页面之前,必须持有该关系表的适当类型的锁。这里不讨论关系级锁。) Pins 在对缓冲区做任何操作之前,必须“对缓冲区pin”(即增加其引用计数, re

MISRA C2012学习笔记(8)-Rules 8.13

文章目录 8.13 副作用(Side effects)Rule 13.1 初始化程序列表不得包含持久性副作用Rule 13.2 在所有合法的评估命令下,表达式的值应与其持续的副作用相同Rule 13.3 包含自增(++)或自减(--)运算符的完整表达式,除由自增或自减运算符引起的副作用外,不应有其他潜在的副作用Rule 13.4 不得使用赋值运算符的结果Rule 13.5 逻辑与(&&)和逻

7 rules of Effortless English System Secret (Ⅲ)

7 rules of  Effortless English System Secret (Ⅲ)            视频链接(Get Video):点击打开链接       password:e961     文章中文版(Chinese paragraph):                                           http://b

50个BA分析工具第六个-Business Rules

知识卡片   工具名称: Business Rules(业务规则分析)   工具介绍:Business Rules,是用来识别和表达去校验修正一些组织的规则,这些规则会约束每天的业务行为,指导业务的决策。   解决问题: • 清晰地定义和管理业务规则,可以使得组织能够制定相关的细则,而不会影响到流程和系统   优点: • 业务政策和规则的指导,每天的这个业务运行都是在po

QT实例1--使用UI文件创建登陆窗口

基础信息 平台:window 10 QT版本 :5.14.2 时间:2024.08.25 本工程是QLayout专题,使用UI文件创建一个登陆窗体 本例程原始资料来源于B站,UP主“爱编程的大丙”的视频教程《QT开发编程-入门基础教程QT5–6.2窗口布局举例-制作一个登陆界面》 本笔记,用于实践上述例程,并进行笔记整理,提取最重要的干货信息。 创建工程 创建一个工程,工程名称

el-form表单在循环里如何写rules表单校验,解决办法

el-form表单在循环里如何写rules表单校验,解决办法 在日常开发中我们经常可以遇到 在form中 循环嵌套多个的情况 比如点击加减新增删除 并且还需要校验表单如何写呢? 实现办法 1.html (举例) contractFeesForm 为总数据源 <el-form :model="contractFeesForm" ref="contractFeesFormRef" :rules

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