linux udev

article/2025/9/23 16:38:35
  • 了解udev (Linux dynamic device management)

1.概述

  Udev is the device manager for the Linux kernel. Udev dynamically creates or removes device node files at boot time in the /dev directory for all types of devices.

  udev is a replacement for the Device File System (DevFS) starting with the Linux 2.6 kernel series. It allows you to identify devices based on their properties, like vendor ID and device ID, dynamically. udev runs in userspace (as opposed to devfs which was executed in kernel space).

  Udev is the Linux subsystem that supplies your computer with device events. In plain English, that means it’s the code that detects when you have things plugged into your computer, like a network card, external hard drives (including USB thumb drives), mouses, keyboards, joysticks and gamepads, DVD-ROM drives, and so on. That makes it a potentially useful utility, and it’s well-enough exposed that a standard user can manually script it to do things like performing certain tasks when a certain hard drive is plugged in.

  udev 是 Linux2.6 内核里的一个功能,它替代了原来的 devfs,成为当前 Linux 默认的设备管理工具。udev 以守护进程的形式运行,通过侦听内核发出来的 uevent 来管理 /dev目录下的设备文件。不像之前的设备管理工具,udev 在用户空间 (user space) 运行,而不在内核空间 (kernel space) 运行。
  udev负责在用户空间,根据用户设置的规则,在监测到设备被插入后,在/dev/下自动创建并命名设备文件节点(实际上只能做一个已存在节点的链接文件),也可以自动设置设备属性。

1.1.Overview

  The udev system is composed of some kernel services and the udevd daemon. The kernel informs the udevd daemon when certain events happen. The udevd daemon is configured to respond to events with corresponding actions. The event information comes from the kernel - the actions happen in userspace. The responses to the events are configurable in “rules”.

  The userspace udev functionality is implemented by the systemd-udevd.service Its config file is in /etc/udev/udev.conf(Configuration for device event managing daemon). The rules files (which amount to more configuration for udevd) are taken from /run/udev/rules.d, /etc/udev/rules.d or /lib/udev/rules.d. Packages install rules in /lib/udev/rules.d), while the /etc and /run locations provide a facility for the administrator to override the behavior of a package-provided rule. If a file with the same name is present in more than one of these directories then the latter(s) file will be ignored. Files in there are parsed in alpha order, as long as the name ends with “.rules”. When the config file or rules files are changed, the udevadm program should be used to instruct systemd-udevd to reload the rules (see below).

  udev was created to respond to hotplug type of events. Much documentation refers to creating devices in response to new devices that have appeared. But, udev is more general; it can run arbitrary userspace commands in response to a new device appearing - or to whatever events it receives from the kernel.

  The times when udevd is active are:

  • at startup, it parses all the config files and rule files and builds a rules database in memory.
  • When an event happens, it checks its rule database and performs the appropriate actions.

1.2.systemd-udevd.service

  The udev daemon, systemd-udevd.service, receives device uevents directly from the kernel whenever a device is added or removed from the system, or it changes its state. When udev receives a device event, it matches its configured set of rules against various device attributes to identify the device. Rules that match may provide additional device information to be stored in the udev database or to be used to create meaningful symlink names.

  All device information udev processes is stored in the udev database and sent out to possible event subscribers. Access to all stored data and the event sources is provided by the library libudev.

  Synopsis:

  • systemd-udevd.service

  • systemd-udevd-control.socket

  • systemd-udevd-kernel.socket

  • /usr/lib/systemd/systemd-udevd ( udev 的守护进程)

    systemd-udevd listens to kernel uevents. For every event, systemd-udevd executes matching instructions specified in udev rules.

Debug:
  In Grub menu, press E for edit. Remove quiet splash to get boot in text mode. Add udev.log-priority=debug. Then Ctrl+X to boot. You can see the udev messages while booting or after using: journalctl -u systemd-udevd.service -f
  For permanent settings, you may set udev.log-priority=debug in GRUB_CMDLINE_LINUX

/usr/share/initramfs-tools/scripts/init-top/udev

1.3.udev在linux的那个位置

  udev的守护进程在linux的位置在systemd中的位置如下所示,举个例子:如果向pc中插入一个usb设备,kernel在总线上发现这个设备,使用dirver初始化,在sysfs创建device目录等操作之后,将通知用户空间的udev,然后上层的显示层才能看到这个usb设备,并最终将它显示在desktop上:
在这里插入图片描述
2.工作原理

  udev是一个通用的内核设备管理器。它以守护进程的方式运行于Linux系统,并监听在新设备初始化或设备从系统中移除时,内核(通过netlink socket)所发出的uevent。

  系统提供了一套规则用于匹配可发现的设备事件和属性的导出值。匹配规则可能命名并创建设备节点,并运行配置程序来对设备进行设置。udev规则可以匹配像内核子系统、内核设备名称、设备的物理等属性,或设备序列号的属性。规则也可以请求外部程序提供信息来命名设备,或指定一个永远一样的自定义名称来命名设备,而不管设备什么时候被系统发现.

在这里插入图片描述

2.1.工作方式

  当设备连接或者移除的时候,内核会发出热拔插事件(hotplug event),而热拔插的时候,设备的详细的信息会由内核通过 netlink 套接字发送出来,发送出来的事情,我们称之为 uevent。udev 基址,就根据这些内核发出来的信息,去创建设备文件节点工作,而设备的命名策略、权限控制、事件处理都是由 udev 规则文件去执行。

  • (1)内核检测设备的连接状况,通过 netlink 发出 uevent

  • (2) udev 获取内核发出的信息,进行规则的匹配工作。

  udev 工作流程图:
在这里插入图片描述
2.2.udev系统架构:

udev系统可以分为三个部分:

  • libudev函数库,可以用来获取设备的信息。
  • udevd守护进程,处于用户空间,用于管理虚拟/dev
  • 管理命令udevadm,用来诊断出错情况。

2.2.1.udevadm (udev management tool)

  udevadm可以用来监视和控制udev运行时的行为,请求内核事件,管理事件队列,以及提供简单的调试机制。

选项:

  • -d, --debug
    Print debug messages to standard error. This option is implied in udevadm test and udevadm test-builtin commands.

  • info options:查询udev数据库中的设备信息。也可以从sysfs文件系统中查询到设备的属性以辅助创建udev规则。

    • –query=type     从数据库中查询指定类型的设备。需要–path和–name来指定设备。合法的查询文件是:设备名,链接,路径,属性
    • –path=devpath    设备的路径
    • –name=file      设备节点或者链接
    • –attribute-walk    打印指定设备的所有sysfs记录的属性,以用来udev规则匹配特殊的设备。该选项打印链上的所有设备信息,最大可能到sys目录。
    • –device-id-of-file=file 打印主/从设备号
    • –export-db      输出udev数据库中的内容
  • trigger [options]:接收内核发送来的设备事件。主要用于重放coldplug事件信息。
    内核在启动时已经检测到了系统的硬件设备,并把硬件设备信息通过sysfs内核虚拟文件系统导出。udev扫描sysfs文件系统,根据硬件设备信息生成热插拔(hotplug)事件,udev再读取这些事件,生成对应的硬件设备文件。由于没有实际的硬件插拔动作,所以这一过程被称为coldplug。

  • test [options] devpath : 模拟一个udev事件,打印出debug信息

  • control : 修改运行的udev守护进程的内部状态。

    • udevadm control --log-priority=debug
    • udevadm control reload-rules && udevadm trigger
  • monitor [options]:监听内核事件和udev发送的events事件。打印事件发出的设备。可以通过比较内核或者udev事件的时间戳来分析事件时序。

    • –kernel  输出内核事件
    • –udev  输出udev规则执行时的udev事件
    • –property  输出事件的属性
    • –subsystem-match=string  通过子系统或者设备类型过滤事件。只有匹配了子系统值的udev设备事件通过。
    • –tag-match=string  通过属性过滤事件,只有匹配了标签的udev事件通过。

Note: 一般systemd-udev-trigger.service中应用udevadm trigger回放coldplug:
ExecStart=/bin/udevadm trigger --type=subsystems --action=add ; /bin/udevadm trigger --type=devices --action=add

Example:

//當前系統識別了多少個磁盤
root@uos-PC:/home/uos# fdisk -l  | grep -o '^Disk /dev/[sh]d[a-z]\>'
Disk /dev/sda//顯示/dev/sda磁盤路徑硬件點
root@uos-PC:/home/uos# udevadm info -q path -n /dev/sda
/devices/pci0000:00/0000:00:08.0/ata1/host0/target0:0:0/0:0:0:0/block/sda//列出設備的具體屬性udevadm info -a -p  [device name]    #這裏的device name表示目前識別的規則NAME

3.udev rules

  udev rules written by the administrator go in /etc/udev/rules.d/, their file name has to end with .rules. The udev rules shipped with various packages are found in /usr/lib/udev/rules.d/. If there are two files by the same name under /usr/lib and /etc, the ones in /etc take precedence.

  udev 规则是定义在一个以 .rules 为扩展名的文件中。那些文件主要放在两个位置:

  • /usr/lib/udev/rules.d,这个目录用于存放系统安装的规则;

  • /etc/udev/rules.d/ 这个目录是保留给自定义规则的。

  • /etc/udev/udev.conf

uos@uos-PC:/usr/lib/udev$ cat /etc/udev/udev.conf
# see udev.conf(5) for details
#
# udevd is also started in the initrd.  When this file is modified you might
# also want to rebuild the initrd, so that it will include the modified configuration.#udev_log=info
#children_max=
#exec_delay=
#event_timeout=180
#resolve_names=early

Note: udev_log:syslog记录日志的级别,默认值是 err。如果改为 info 或者 debug 的话,会有冗长的 udev 日志被记录下来。
refer to https://man7.org/linux/man-pages/man5/udev.conf.5.html

  • /usr/lib/udev/rules.d
    这些文件都是一些 udev 的规则文件,当一个设备连接的时候,就根据这些文件的规则,进行设备节点的创建。
uos@uos-PC:/usr/lib/udev/rules.d/$ ls
39-usbmuxd.rules         60-libgphoto2-6.rules             65-libwacom.rules           75-net-description.rules             77-mm-simtech-port-types.rules            80-mm-candidate.rules           90-pulseaudio.rules
40-usb_modeswitch.rules  60-libsane.rules                  69-libmtp.rules             75-probe_mtd.rules                   77-mm-telit-port-types.rules              80-net-setup-link.rules         92-libccid.rules
50-firmware.rules        60-libtss2-udev.rules             69-lvm-metad.rules          77-mm-cinterion-port-types.rules     77-mm-ublox-port-types.rules   

The difference between the two folders is that:

  • /usr/lib/udev/rules.d
    Contains default rules files. Do not edit these files.

  • /etc/udev/rules.d/*.rules
    Contains customized rules files. You can modify these files or create them.

  However, files with identical file names replace each other. Files in /etc have the highest priority and take precedence over files with the same name in /lib. This can be used to override the default rules.

  Another thing, to disable the the rules file entirely, you can create a symlink in /etc with the same name as a rules file in /lib, pointing to /dev/null.

refer to

  • https://wiki.debian.org/udev
  • https://wiki.archlinux.org/index.php/Udev
  • https://wiki.debian.org/DeviceManagement
  • https://man7.org/linux/man-pages/man7/udev.7.html
  • http://www.reactivated.net/writing_udev_rules.html
  • https://www.freedesktop.org/software/systemd/man/udev.conf.html#
  • https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/
  • https://blog.csdn.net/li_wen01/article/details/89435306
  • https://www.cnblogs.com/carriezhangyan/p/9564900.html

http://chatgpt.dhexx.cn/article/QTcHrMfQ.shtml

相关文章

udev(六):udev

原创文章&#xff0c;转载请注明出处&#xff0c;谢谢&#xff01; 作者&#xff1a;清林&#xff0c;博客名&#xff1a; 飞 空静渡 <!-- page { margin: 2cm } P { margin-bottom: 0.21cm } --> 基于上面提到的问题&#xff0c;便开始了 udev 的项目。 …

udev(八):实战:使用udevadm修改usb优盘在/dev下的名字

原创文章&#xff0c;转载请注明出处&#xff0c;谢谢&#xff01; 作者&#xff1a;清林&#xff0c;博客名&#xff1a; 飞 空静渡 udev的规则的编写&#xff0c;我这里就不说了&#xff0c;大家可以在udev的文档中可以了解&#xff0c;有时间的话我在安排一…

Linux下udev规则详解

如何运用udev&#xff1f;编写udev规则&#xff1f;通过udev更好的管理Linux设备? udev是什么&#xff1f; udev 是 Linux2.6 内核里的一个功能&#xff0c;它替代了原来的 devfs&#xff0c;成为当前 Linux 默认的设备管理工具。udev 以守护进程的形式运行&#xff0c;通过侦…

Udev设备管理器

udev 引入 在2.4内核中 引入了 devfs&#xff0c;他支持设备节点的动态创建。devfs提供了在内存中的文件系统中创建设备节点的能力。设备驱动程序能够自主的管理自己的设备文件。而命名节点的任务还是落到了设备驱动程序头上。但是&#xff0c;设备命名策略是可管理的&#xf…

linux下udev详解

一.udev简介 udev是一个设备管理工具&#xff0c;udev以守护进程的形式运行&#xff0c;通过侦听内核发出来的uevent来管理/dev目录下的设备文件。udev在用户空间运行&#xff0c;而不在内核空间 运行。它能够根据系统中的硬件设备的状态动态更新设备文件&#xff0c;包括设备…

[2020.1.14][udev] udev介绍

1 什么是udev udev是一种工具&#xff0c;它能够根据系统中的硬件设备的状态动态更新设备文件&#xff0c;包括设备文件的创建&#xff0c;删除等。 设备文件通常放在/dev目录下。使用udev后&#xff0c;在/dev目录下就只包含系统中真正存在的设备。 设备访问控制udev:user …

LINUX下 Udev详解

如果你使用Linux比较长时间了&#xff0c;那你就知道&#xff0c;在对待设备文件这块&#xff0c;Linux改变了几次策略。在Linux早期&#xff0c;设备文件仅仅是是一些带有适当的属性集的普通文件&#xff0c;它由mknod命令创建&#xff0c;文件存放在/dev目录下。后来&#xf…

wmic bug

今天尝试用wmic进行横向移动&#xff0c;老是出现 , - Alias not found&#xff0c;再三确认不是命令的问题&#xff0c;尝试修改域控用户的密码后成功解决&#xff0c;难道这时wmic的bug&#xff1f;&#xff1f;&#xff1f; 猜测是因为密码种含有 , . 这样的符号导致命令产生…

wmic命令学习

我目前知道wmic可以查询进程&#xff0c;还可以查询服务。查询进程使用wmic process,如果想知道进程的名字、进程号、执行文件路径可以通过get来获取&#xff0c;还可以根据where筛选进程进行查询。 wmic process get name, processid,executablepath 查询主机进程信息&#xf…

Windows 命令 WMIC 使用详解

WMIC扩展WMI&#xff08;Windows Management Instrumentation&#xff0c;Windows管理工具&#xff09; &#xff0c;提供了从命令行接口和批命令脚本执行系统管理的支持。在WMIC出现之前&#xff0c;如果要管理WMI系统&#xff0c;必须使用一些专门的WMI应用&#xff0c;例如S…

wmic java_Java执行wmic命令获取系统环境变量

1.首先编写文件setenv.bat设置系统环境变量&#xff1a; echo %cd% set framework_home%cd% echo %framework_home% wmic ENVIRONMENT create name"framework_home",username"",VariableValue"%framework_home%" Note: 此处假设framework_home为…

wmic命令

友情链接&#xff1a;wmic命令 目录 一、如何获取帮助文档 &#xff08;1&#xff09;wmic /? &#xff08;2&#xff09;wmic nic /? &#xff08;3&#xff09;wmic nic get ##获取网卡所有信息&#xff0c;如下只列出部分列 &#xff08;4&#xff09;信息筛…

内网渗透 | powershellwmic详解

文章目录 引子PowerShell0x001基本概念0x002命令格式0x003基本使用0x004常用命令0x005powershell脚本0x006参考文章 WMIC0x001基本概念0x002命令格式0x003常用命令1、目标系统相关2、用户管理 0x004参考文章 引子 之前在内网渗透&#xff0c;免杀中到处能看到powershell和wmic的…

wmic横向

WMI&#xff0c;是Windows 2K/XP管理系统的核心&#xff1b;对于其他的Win32操作系统&#xff0c;WMI是一个有用的插件。WMI以CIMOM为基础&#xff0c;CIMOM即公共信息模型对象管理器&#xff08;Common Information Model Object Manager&#xff09;&#xff0c;是一个描述操…

【转】wmic命令

友情链接&#xff1a;wmic命令 目录 一、如何获取帮助文档 &#xff08;1&#xff09;wmic /? &#xff08;2&#xff09;wmic nic /? &#xff08;3&#xff09;wmic nic get ##获取网卡所有信息&#xff0c;如下只列出部分列 &#xff08;4&#xff09;信息筛选 二、…

WMIC学习笔记

wmic 是什么 wmic 是计算机管理工具&#xff0c;提供给管理员以命令行的方式管理计算机。在 win10 已经弃用&#xff0c;不过还能用。 执行模式 WMIC 有两种执行模式&#xff1a;交互模式(Interactive mode)和非交互模式(Non-Interactive mode)。 在命令提示符或“运行”窗口…

信息收集--wmic命令

后渗透信息收集之wmic命令的一些使用方法 前言一、关于wmic二、wmic的简单使用三、以进程为例展现wmic的使用四、关于powershell的Get-Wmi 对象 前言 wmic和cmd一样在所有的windows版本中都存在&#xff0c;同时wmic有很多cmd下不方便使用的部分 一、关于wmic WMI命令行(WMIC)实…

Wmic

WMIC&#xff08;Windows Management InstrumentationCommand Line&#xff09; windows除了cmd &#xff0c;powershell以外另一个更为强大的命令执行shell: windows最令网管诟病的地方就是命令行没有unix和linux强大。但这种情况正在不断改观&#xff0c;windows命令行也越来…

比CMD更强大的命令行:WMIC后渗透利用(系统命令)

写在前面的话 在这篇文章中&#xff0c;我们将讨论如何在攻击的后渗透利用阶段使用WMIC&#xff08;Windows Management InstrumentationCommand Line&#xff09;。当攻击者在远程PC上拿到meterpreter会话之后&#xff0c;他们就可以枚举大量的系统信息&#xff0c;并利用WMI…

“教务管理系统”ER模型

将ER模型转换成逻辑数据模型 ⑴学生表&#xff08;学号&#xff0c;姓名&#xff0c;性别&#xff0c;出生日期&#xff0c;专业&#xff0c;籍贯&#xff0c;联系电话&#xff0c;班级编号&#xff09;&#xff1b; ⑵课程表&#xff08;课程编号&#xff0c;课程名称&#…