Ubuntu 22.04 dmesg 安装与命令参数详解

在 Ubuntu 22.04 上实测 dmesg 内核日志工具的全部参数(默认/-T/-t/-l/--level/-f/-u/-d/-x/-H/-s/--color/-w/--since/-e/--time-format 等),含在线与离线安装、版本差异与速查表

Linux 系统监控与诊断dmesgUbuntu 22.042026/10/06

Ubuntu 22.04 dmesg 安装与命令参数详解

目标:在 Ubuntu 22.04(x86_64)服务器上实测 dmesg(util-linux 2.37.2)的默认输出与全部时间戳/过滤/解码/跟随/清除类参数,给出真实回显、版本差异与命令速查表;开机早期消息用 head 采样干净输出,避免把断点调试类的临时噪音混入正文。

一、环境信息

  • 操作系统:Ubuntu 22.04(DISTRIB_ID=Ubuntu)
  • 内核版本:Linux 6.8.0-138-generic
  • 系统架构:x86_64
  • 主机名:mon-ubuntu2204
  • dmesg 版本:dmesg from util-linux 2.37.2(由 dmesg -V 实测)
  • 所属软件包:util-linux(Ubuntu 官方默认基础包,jammy/main 仓库,http://archive.ubuntu.com/ubuntu)
  • 安装来源:apt / dpkg(util-linux 随系统默认预装)

二、工具概览

dmesg(全称 "display message")用于显示或控制内核环形缓冲区(kernel ring buffer),也是排查硬件/驱动/系统启动问题最常用的内核日志工具。

工作机制:内核把启动过程、设备探测、驱动加载、错误与告警等消息写入一个固定大小的环形缓冲区(默认通过 /dev/kmsg 接口暴露);dmesg 负责读取并格式化这批消息。环形缓冲区有几个特征:

  1. 有限容量、循环覆写:缓冲区写满后新消息会覆盖最旧消息,缺省(不带参数)只打印当前缓冲区里的内容。
  2. 带时间戳:每条内核消息通常带有自系统启动以来的相对时间(秒,如 [392447.374733])。
  3. 设施(facility)与级别(level):每条消息带 LOG_KERN/LOG_DAEMON 等设施属性,以及 emerg/alert/crit/err/warn/notice/info/debug 等优先级,供过滤。

监控维度上,dmesg 主要覆盖:启动早期阶段各子系统初始化(EXT4-fs/ppdev/swap 等)、PCIe/设备枚举(pciehp 热插拔)、硬件错误与固件告警、驱动加载与模块装载(modprobe@*.service)、以及 systemd 各服务启停的用户态通知。当手动执行 --since/--until 时间窗口、-T/-e/--time-format 时间折算、-l/-f 级别/设施过滤时,可快速定位某段时间或某种类型的内核事件。

三、在线安装

dmesg 属于 util-linux 软件包,是 Ubuntu 22.04 默认预装的基础包,无需单独安装。用 apt 确认/安装:

apt-get install -y util-linux

重复执行时会提示已是最新版本(输出为「already the newest version」且「0 upgraded, 0 newly installed」)。

💡 说明:dmesg 未写在前面监控套件(atop/htop/nmon 等)的安装清单里,因为它由 util-linux 提供并默认存在;本环境直接用 dmesg -V 读取其来源与版本即可。

版本验证(重点,注意是大写 -V):

dmesg -V
dmesg from util-linux 2.37.2

结论:本环境 dmesg 来自 util-linux 2.37.2。

四、离线安装

离线安装分「下载打包」与「本机安装」两步,适用于无法直连互联网的服务器。

4.1 下载 util-linux 包(在联网机执行)

方式一:apt-get download 只拉主包:

apt-get download util-linux

方式二:apt-get install --download-only 将主包(含依赖)落入指定目录:

apt-get install --download-only util-linux

生成类似 util-linux_2.37.2-0ubuntu*.deb 的 deb 包到当前目录。

4.2 离线安装(在目标机执行)

把 deb 拷贝到目标机后,用 dpkg -i 安装:

dpkg -i util-linux_2.37.2-*.deb

💡 说明:dpkg -i 的回显为标准「Preparing to unpack / Unpacking / Setting up」三段(与日志中 htop 重装回显同格式)。安装后再用 dmesg -V 验证即可,输出同第三章 dmesg from util-linux 2.37.2。

五、全部命令参数实测

以下每条命令/输出均在本环境真实执行。为避免把断点调试脚本产生的临时 traps: ... trap int3 ... 噪音当作系统日志,默认输出与各过滤参数的示例统一用 head 采样开机早期消息等干净内容。

5.1 默认输出:dmesg(内核环形缓冲区最近内容)

作用:不带参数打印当前环形缓冲区内容。数据量大,先用 head -20 采样开机早期消息:

dmesg | head -20
[   24.224196] systemd[1]: Mounted Kernel Trace File System.
[   24.259716] systemd[1]: Finished Create List of Static Device Nodes.
[   24.261289] systemd[1]: modprobe@configfs.service: Deactivated successfully.
[   24.262756] systemd[1]: Finished Load Kernel Module configfs.
[   24.264616] systemd[1]: modprobe@drm.service: Deactivated successfully.
[   24.266672] systemd[1]: Finished Load Kernel Module drm.
[   24.268231] systemd[1]: modprobe@efi_pstore.service: Deactivated successfully.
[   24.269677] systemd[1]: Finished Load Kernel Module efi_pstore.
[   24.270867] systemd[1]: modprobe@fuse.service: Deactivated successfully.
[   24.272153] systemd[1]: Finished Load Kernel Module fuse.
[   24.308369] EXT4-fs (sda3): re-mounted 43e68207-7df4-4f66-933a-8a595cdde8c8 r/w.
[   24.317739] systemd[1]: Mounting FUSE Control File System...
[   24.319722] lp: driver loaded but no devices found
[   24.359222] systemd[1]: Mounting Kernel Configuration File System...
[   24.365149] ppdev: user-space parallel port driver
[   24.368868] systemd[1]: Finished Remount Root and Kernel File Systems.
[   24.370927] systemd[1]: Mounted FUSE Control File System.
[   24.371388] systemd[1]: Mounted Kernel Configuration File System.
[   24.415995] systemd[1]: Activating swap /swapfile...
[   24.416520] systemd[1]: Condition check resulted in Platform Persistent Storage Archival being skipped.

说明:方括号内是开机后经过的秒数([24.224196] 即开机约 24.22 秒),右侧为消息内容;可看到 EXT4 根分区重挂载、swap 激活、ppdev/lp 驱动探测、modprobe@*.service 装载内核模块等启动阶段消息。

5.2 人类可读时间戳:-T, --ctime

作用:把相对秒数时间戳折算成绝对日历时间([Thu Oct 1 16:08:37 2026]),便于对照墙钟故障时刻。

dmesg -T | head -8
[Thu Oct  1 16:08:37 2026] systemd[1]: Mounted Kernel Trace File System.
[Thu Oct  1 16:08:37 2026] systemd[1]: Finished Create List of Static Device Nodes.
[Thu Oct  1 16:08:37 2026] systemd[1]: modprobe@configfs.service: Deactivated successfully.
[Thu Oct  1 16:08:37 2026] systemd[1]: Finished Load Kernel Module configfs.
[Thu Oct  1 16:08:37 2026] systemd[1]: modprobe@drm.service: Deactivated successfully.
[Thu Oct  1 16:08:37 2026] systemd[1]: Finished Load Kernel Module drm.
[Thu Oct  1 16:08:37 2026] systemd[1]: modprobe@efi_pstore.service: Deactivated successfully.
[Thu Oct  1 16:08:37 2026] systemd[1]: Finished Load Kernel Module efi_pstore.

⚠️ 注意:-T 的 ctime 与 --time-format 的 iso 都是把相对运行秒折算成绝对时间,会受开机时间/时区/休眠唤醒影响,「Suspending/resume will make ctime and iso timestamps inaccurate」,详见第六章。

5.3 去掉时间戳:-t, --notime

作用:不显示任何时间戳,只留日志内容,便于做文本抽取/去重。

dmesg -t | head -5
systemd[1]: Mounted Kernel Trace File System.
systemd[1]: Finished Create List of Static Device Nodes.
systemd[1]: modprobe@configfs.service: Deactivated successfully.
systemd[1]: Finished Load Kernel Module configfs.
systemd[1]: modprobe@drm.service: Deactivated successfully.

5.4 级别过滤:-l, --level <list> / --level=err,warn

作用:只显示指定优先级(emerg/alert/crit/err/warn/notice/info/debug)的消息,用于从海量日志里抽错误与告警。实测只显示 err 与 warn 两级:

dmesg -l err,warn
[389340.908311] systemd[1]: Failed to determine supported controllers: No such file or directory
[389395.580628] systemd[1]: Couldn't move remaining userspace processes, ignoring: Input/output error
[389395.580676] systemd[1]: Failed to determine supported controllers: No such file or directory
[389595.446944] systemd-journald[23]: File /var/log/journal/cb92ba5a13e3ba35aa819dee6aba6d53/system.journal corrupted or uncleanly shut down, renaming and replacing.
[389634.127770] systemd-journald[23]: File /var/log/journal/cb92ba5a13e3ba35aa819dee6aba6d53/system.journal corrupted or uncleanly shut down, renaming and replacing.
[392184.269498] systemd[1]: Couldn't move remaining userspace processes, ignoring: Input/output error
[392184.269546] systemd[1]: Failed to determine supported controllers: No such file or directory
[392259.717488] systemd[1]: Couldn't move remaining userspace processes, ignoring: Input/output error
[392259.717535] systemd[1]: Failed to determine supported controllers: No such file or directory
[392319.350125] workqueue: sync_rcu_exp_select_node_cpus hogged CPU for >10000us 8 times, consider switching to WQ_UNBOUND

说明:-l 与长选项 --level 等价;-l err,warn(或 --level=err,warn)只留下 systemd 报错、systemd-journald 日志文件损坏重命名、workqueue CPU 软锁嫌疑等 err/warn 消息。

5.5 指定多级别:--level=emerg,alert,crit,err

作用:与 -l 等价的长写法,本用例同时筛选最高危的几个级别(紧急/强烈告警/致命/错误)。

dmesg --level=emerg,alert,crit,err
[389395.616686] systemd[1]: Failed to allocate manager object: No such file or directory
[389395.616778] systemd[1]: Freezing execution.
[392184.269546] systemd[1]: Failed to determine supported controllers: No such file or directory
[392184.428612] systemd[1]: Failed to allocate manager object: No such file or directory
[392184.428700] systemd[1]: Freezing execution.
[392259.717535] systemd[1]: Failed to determine supported controllers: No such file or directory
[392259.770338] systemd[1]: Failed to allocate manager object: No such file or directory
[392259.770430] systemd[1]: Freezing execution.

说明:--level=emerg,alert,crit,err 比 err,warn 更严苛,剔掉了 warn,只保留 systemd 分配 manager 对象失败、控制器探测失败并「Freezing execution」等严重消息。

5.6 设施过滤:-f, --facility <list>

作用:只显示指定设施(kern/user/mail/daemon/auth/syslog/lpr/news)的消息;本用例取 kernel 设施并采样开机前 5 行内核侧消息。

dmesg -f kern | head -5
[   24.308369] EXT4-fs (sda3): re-mounted 43e68207-7df4-4f66-933a-8a595cdde8c8 r/w.
[   24.319722] lp: driver loaded but no devices found
[   24.365149] ppdev: user-space parallel port driver
[   24.459477] Adding 2097148k swap on /swapfile.  Priority:-2 extents:6 across:2260988k 
[   24.814446] loop0: detected capacity change from 0 to 8

说明:-f kern 只留内核消息(EXT4 挂载、swap 添加、loop 设备识别等),systemd 启动消息多数归属其它设施,此处不出现。

5.7 用户空间消息:-u, --userspace

作用:与 -f kern 相对,只显示用户态(Linux 会把这些消息按 daemon 等设施记录)消息。

dmesg -u
[392342.639316] systemd[1]: Starting udev Kernel Device Manager...
[392342.647403] systemd[1]: Started Update is Completed.
[392342.667042] systemd[1]: Started udev Kernel Device Manager.
[392342.691702] systemd[1]: Started Journal Service.
[392343.352150] systemd-journald[23]: Received client request to flush runtime journal.

说明:-u 显示 systemd/udev/journald 等用户态守护进程写入环形缓冲区的消息。是否能看到输出取决于系统是否用 systemd journal 统一接管,详见第六章。

5.8 相邻消息时间增量:-d, --show-delta

作用:在时间戳后追加括号内的时间增量(<0.035520> 表示距上一条消息 0.0355 秒),用于观察两条消息之间的间隙。

dmesg -d | head -5
[   24.224196 <    0.000000>] systemd[1]: Mounted Kernel Trace File System.
[   24.259716 <    0.035520>] systemd[1]: Finished Create List of Static Device Nodes.
[   24.261289 <    0.001573>] systemd[1]: modprobe@configfs.service: Deactivated successfully.
[   24.262756 <    0.001467>] systemd[1]: Finished Load Kernel Module configfs.
[   24.264616 <    0.001860>] systemd[1]: modprobe@drm.service: Deactivated successfully.

5.9 级别与设施解码:-x, --decode

作用:在每条消息前打印解码后的「设施:级别」,例如 daemon:info,便于按来源分类。

dmesg -x | head -5
daemon:info  : [   24.224196] systemd[1]: Mounted Kernel Trace File System.
daemon:info  : [   24.259716] systemd[1]: Finished Create List of Static Device Nodes.
daemon:info  : [   24.261289] systemd[1]: modprobe@configfs.service: Deactivated successfully.
daemon:info  : [   24.262756] systemd[1]: Finished Load Kernel Module configfs.
daemon:info  : [   24.264616] systemd[1]: modprobe@drm.service: Deactivated successfully.

说明:-x 把数值化设施/级别解码成可读字符串,systemd 启停消息在此处被判为 daemon:info。

5.10 时间起点过滤:--since <time>

作用:只显示指定时间之后的消息。

--since '24 hours ago'(相对时间)实测无任何输出:

dmesg --since '24 hours ago'
(空输出,无命中行)

--since '2026-10-06 04:00'(具体时间点)实测命中行数为 0:

dmesg --since '2026-10-06 04:00'
(命中行数:)
0

⚠️ 注意:这块与 -T 类似,--since/--until 依赖消息的 ctime 折算;当环形缓冲区里消息的 ctime 元数据超出可算范围、或相对时间换算不准确时,会出现「明明有日志却 0 命中」的空输出,详见第六章。

5.11 时间终点过滤:--until <time>

作用:只显示指定时间之前的消息。

dmesg --until '1 hour ago'
[392450.879839] Core dump to |/usr/lib/systemd/systemd-coredump pipe failed
[392450.910150] traps: ls[2674400] trap int3 ip:582113b46ee7 sp:7ffff77e2ec0 error:0 in ls[582113b46000+14000]
[392450.910440] Core dump to |/usr/lib/systemd/systemd-coredump pipe failed

说明:--until '1 hour ago' 把输出截止到 1 小时前的消息。

5.12 人类友好输出:-H, --human 与 -e, --reltime

作用:-H 用友好格式(首行给绝对时间 [Oct 1 16:08],后续用相对增量 [ +0.035520]);-e 与 -H 时间字段等价,只显示本地时间与相对增量。

dmesg -H | head -6
[Oct 1 16:08] systemd[1]: Mounted Kernel Trace File System.
[  +0.035520] systemd[1]: Finished Create List of Static Device Nodes.
[  +0.001573] systemd[1]: modprobe@configfs.service: Deactivated successfully.
[  +0.001467] systemd[1]: Finished Load Kernel Module configfs.
[  +0.001860] systemd[1]: modprobe@drm.service: Deactivated successfully.
[  +0.002056] systemd[1]: Finished Load Kernel Module drm.
dmesg -e | head -6
[Oct 1 16:08] systemd[1]: Mounted Kernel Trace File System.
[  +0.035520] systemd[1]: Finished Create List of Static Device Nodes.
[  +0.001573] systemd[1]: modprobe@configfs.service: Deactivated successfully.
[  +0.001467] systemd[1]: Finished Load Kernel Module configfs.
[  +0.001860] systemd[1]: modprobe@drm.service: Deactivated successfully.
[  +0.002056] systemd[1]: Finished Load Kernel Module drm.

说明:-e 与 -H 的差异主要体现在管道/分页行为与执行细节上,时间格式基本等价;两者都很适合人眼快速浏览。

5.13 ISO 时间格式:--time-format=iso

作用:把时间戳按 ISO 8601 输出(2026-10-01T16:08:37,224196+00:00,微秒级、带时区),便于机器解析与落库。

dmesg --time-format=iso
2026-10-01T16:08:37,224196+00:00 systemd[1]: Mounted Kernel Trace File System.
2026-10-01T16:08:37,259716+00:00 systemd[1]: Finished Create List of Static Device Nodes.
2026-10-01T16:08:37,261289+00:00 systemd[1]: modprobe@configfs.service: Deactivated successfully.
2026-10-01T16:08:37,262756+00:00 systemd[1]: Finished Load Kernel Module configfs.

说明:--time-format 取值类型见 --help 的 [delta|reltime|ctime|notime|iso],iso 即绝对 ISO 时间,reltime 等价于 -e、ctime 等价于 -T、notime 等价于 -t。

5.14 指定缓冲区间大小:-s, --buffer-size <size>

作用:指定要查询内核环形缓冲区的字节数(size)。

dmesg -s 16392
13501

说明:-s 16392 指示按 16392 字节读取缓冲区,实测首行输出 13501(实际读到的字节数/可用缓冲信息,随缓冲区内容浮动)。

5.15 强制着色:-L, --color=always

作用:用 ANSI 转义给设施/级别上色;cat -v 把转义序列显示为 ^[[32m 等以便肉眼确认。

dmesg --color=always | head -3 | cat -v
^[[32m[   24.224196] ^[[0m^[[33msystemd[1]^[[0m: Mounted Kernel Trace File System.
^[[32m[   24.259716] ^[[0m^[[33msystemd[1]^[[0m: Finished Create List of Static Device Nodes.
^[[32m[   24.261289] ^[[0m^[[33msystemd[1]^[[0m: modprobe@configfs.service: Deactivated successfully.

说明:^[[32m(绿)给时间戳、^[[33m(黄)给日志来源(systemd[1])上色,^[[0m 重置。默认 colors are enabled by default,当输出不是终端时可改 --color=never。

5.16 跟随模式:-w, --follow

作用:持续等待新消息并实时打印(类似 tail -f)。本用例用 3 秒窗口采样,期间捕获到 PCIe 端口初始化消息:

dmesg -w
[    2.723611] pcieport 0000:00:17.2: PME: Signaling with IRQ 42
[    2.723747] pcieport 0000:00:17.2: pciehp: Slot #226 AttnBtn+ PwrCtrl+ MRL- AttnInd- PwrInd- HotPlug+ Surprise- Interlock- NoCompl+ IbPresDis- LLActRep+
[    2.759302] pcieport 0000:00:17.3: PME: Signaling with IRQ 43
[    2.759451] pcieport 0000:00:17.3: pciehp: Slot #227 AttnBtn+ PwrCtrl+ MRL- AttnInd- PwrInd- HotPlug+ Surprise- Interlock- NoCompl+ IbPresDis- LLActRep+
[    2.761848] pcieport 0000:00:17.4: PME: Signaling with IRQ 44
(follow exit=0)

说明:-w/--follow 会阻塞直到按 Ctrl-C;此处采样到的是 pcieport 0000:00:17.x 的 PME 唤醒与 pciehp 热插拔槽位初始化等内核硬件消息。

5.17 仅新消息:-W, --follow-new

作用:只打印新产生的消息,不重放缓冲区已有内容。实测在 3 秒窗口内无新增内核消息,因此无输出:

dmesg --follow-new
(空输出,3 秒窗口内无新消息)

说明:--follow-new 需要较高版本 util-linux 才支持;本环境(2.37.2)下会正常进入等待,若突然产生新日志则会立即回显。

5.18 原始格式:-r, --raw 与 --raw

作用:打印消息缓冲区原始内容,数值化级别/设施以 <7> 一类前缀形式保留。

dmesg --raw | head -3
<7>[   24.224196] systemd[1]: Mounted Kernel Trace File System.
<7>[   24.259716] systemd[1]: Finished Create List of Static Device Nodes.
<7>[   24.261289] systemd[1]: modprobe@configfs.service: Deactivated successfully.

说明:<7> 即该消息的原始优先级数字(7=debug);-r 与 --raw 等价。

5.19 组合设施过滤:--facility=daemon

作用:与 -f 等价的长写法,只显示 daemon 设施(系统守护进程)消息。

dmesg --facility=daemon
[392342.647403] systemd[1]: Started Update is Completed.
[392342.667042] systemd[1]: Started udev Kernel Device Manager.
[392342.691702] systemd[1]: Started Journal Service.

5.20 帮助全文:--help

dmesg --help
Usage:
 dmesg [options]

Display or control the kernel ring buffer.

Options:
 -C, --clear                 clear the kernel ring buffer
 -c, --read-clear            read and clear all messages
 -D, --console-off           disable printing messages to console
 -E, --console-on            enable printing messages to console
 -F, --file <file>           use the file instead of the kernel log buffer
 -f, --facility <list>       restrict output to defined facilities
 -H, --human                 human readable output
 -k, --kernel                display kernel messages
 -L, --color[=<when>]        colorize messages (auto, always or never)
                               colors are enabled by default
 -l, --level <list>          restrict output to defined levels
 -n, --console-level <level> set level of messages printed to console
 -P, --nopager               do not pipe output into a pager
 -p, --force-prefix          force timestamp output on each line of multi-line messages
 -r, --raw                   print the raw message buffer
     --noescape              don't escape unprintable character
 -S, --syslog                force to use syslog(2) rather than /dev/kmsg
 -s, --buffer-size <size>    buffer size to query the kernel ring buffer
 -u, --userspace             display userspace messages
 -w, --follow                wait for new messages
 -W, --follow-new            wait and print only new messages
 -x, --decode                decode facility and level to readable string
 -d, --show-delta            show time delta between printed messages
 -e, --reltime               show local time and time delta in readable format
 -T, --ctime                 show human-readable timestamp (may be inaccurate!)
 -t, --notime                don't show any timestamp with messages
     --time-format <format>  show timestamp using the given format:
                               [delta|reltime|ctime|notime|iso]
Suspending/resume will make ctime and iso timestamps inaccurate.
     --since <time>          display the lines since the specified time
     --until <time>          display the lines until the specified time

 -h, --help                  display this help
 -V, --version               display version

Supported log facilities:
    kern - kernel messages
    user - random user-level messages
    mail - mail system
  daemon - system daemons
    auth - security/authorization messages
  syslog - messages generated internally by syslogd
     lpr - line printer subsystem
    news - network news subsystem

Supported log levels (priorities):
   emerg - system is unusable
   alert - action must be taken immediately
    crit - critical conditions
     err - error conditions
    warn - warning conditions
  notice - normal but significant condition
    info - informational
   debug - debug-level messages

For more details see dmesg(1).

💡 说明:帮助里还列出了 -C/--clear(清空缓冲)、-c/--read-clear(读取并清空)、-D/--console-off、-E/--console-on、-n/--console-level、-P/--nopager、-p/--force-prefix、-F/--file、-S/--syslog 等控制/读取类参数,因具有破坏性或需特权/外部文件,本实测量测内容以读取与过滤类为主,控制类在第六章说明。

六、常见问题与真实行为

  1. --since/--until 空输出(消息 ctime 元数据问题) 现象:dmesg --since '24 hours ago' 无任何输出,dmesg --since '2026-10-06 04:00' 命中行数为 0,尽管环形缓冲区里有大量日志。 原因:相对/具体时间过滤依赖把消息的相对运行秒折算成 ctime 再与给定时间比较;当 ctime 折算被判定为不准确/不可用时,filter 结果为 0 命中。帮助里 -T 和 --time-format=iso 都标注 may be inaccurate,且「Suspending/resume will make ctime and iso timestamps inaccurate」。 解决:时间窗口过滤优先用能给出真实绝对时间的 -T/-e/--time-format=iso 确认可算后,再用 --since/--until;若仍为 0,改用 dmesg -T | grep <关键字> 或按固件时间人工比对。

  2. -T(ctime)时间戳折算 现象:dmesg -T 把 [24.224196] 折算成 [Thu Oct 1 16:08:37 2026]。 原因:ctime 由开机 uptime(约 24 秒)+ 开机墙钟时刻换算得到,与 date 不一致属正常,因为内核只保证相对 ticks。 解决:要精确对齐墙钟用 journalctl(journalctl -k)而非 dmesg -T,或接受秒级偏差。

  3. -H(human)输出 现象:dmesg -H 首行 [Oct 1 16:08],后续行 [ +0.035520] 相对增量,非常易读。 原因:-H 优先展示本地绝对时刻,后续行用增量避免重复打印时刻。注意系统长时间不重启时首行仍只到「分」,粒度到秒。

  4. --level=err,warn 过滤有效但不等价于全告警 现象:-l err,warn 只留下 systemd 报错、journald 日志损坏重命名、workqueue 软锁嫌疑等消息。 原因:级别过滤是内核日志优先级(priority)的精确筛选,notice/info 级的大量启动消息被正确剔除。 解决:抓「错误类」用 --level=err,抓「错误+告警」用 --level=err,warn,需要临界告警再叠加 crit/alert/emerg。

  5. -k(内核消息)与 -u(用户空间消息)的反向拆包 现象:dmesg -u 能输出 systemd/udev/journald 用户态消息;-f kern 只留 EXT4/ppdev/swap/loop 等纯内核消息。-k(--kernel)是 -u 的反面,用于只显示内核消息。 原因:环形缓冲区里同时存在 kern 设施(内核侧)与 daemon 等设施(用户态写回)的消息,-k/-u 据此拆分。 注意:用户态消息是否可被 -u 看到,取决于系统是否用 systemd journal 统一接管、以及 udev/journald 是否仍向 /dev/kmsg 写入。在纯 systemd journal 接管、内核消息收敛到 journal 的部署里,-u 可能稀疏甚至有几条无输出的现象,属正常。

  6. -S/--syslog 与 -F/--file 的行为 说明:帮助中 -S, --syslog 表示「force to use syslog(2) rather than /dev/kmsg」,即通过传统 syslog 系统调用读取而非直接读 /dev/kmsg;-F, --file <file> 表示「use the file instead of the kernel log buffer」,即改从指定文件(如抓包保存的 dmesg 转储文件)读取内核日志,便于离线分析崩溃转储。 注意:这两个参数偏向底层/迁移/离线分析场景,且 -S 兼容性随各发行版 util-linux 与 syslog 后端而异,需要时按目标文件实际内容验证。

  7. -s 16392 首行 13501 非固定值 现象:dmesg -s 16392 输出 13501。 原因:这是查询指定缓冲大小的返回值/当前缓冲区信息,随缓冲区实际内容与版本浮动,不是固定常数。

  8. 不要用断点调试噪音当系统日志 现象:默认 dmesg 最近 15 行可能被大量 traps: <cmd>[<pid>] trap int3 ... 与 Core dump to |/usr/lib/systemd/systemd-coredump pipe failed 刷屏。 原因:本环境做断点/注入类调试时,进程被 int3 断点命中并触发 coredump,两者以 info 级写入环形缓冲区。 解决:分析启动与硬件问题时应取样早期内核消息(dmesg | head)或在干净时间窗执行,避免把调试脚本产生的噪音误判为系统故障。

七、命令与参数速查表

参数/命令含义实测说明/默认值
dmesg打印内核环形缓冲区缺省无过滤,本机用 head -20 采样开机消息
dmesg -T人类可读绝对时间戳实测 [Thu Oct 1 16:08:37 2026],可能不准确
dmesg -t去掉时间戳只留日志正文
dmesg -H人类友好输出首行绝对时间+后续相对增量
dmesg -e本地时间+相对增量与 -H 时间格式基本等价
dmesg --time-format=isoISO 8601 时间戳2026-10-01T16:08:37,224196+00:00
dmesg -l err,warn / --level=级别过滤实测只留 systemd 报错/journald 损坏/workqueue
dmesg -f kern / --facility=设施过滤实测只留内核侧消息
dmesg -k只显示内核消息与 -u 相对
dmesg -u只显示用户空间消息实测 systemd/udev/journald;依赖 systemd journal 接管
dmesg -x解码设施:级别实测 daemon:info
dmesg -d相邻消息时间增量实测 <0.035520>
dmesg -s 16392指定缓冲查询大小实测首行 13501
dmesg -w跟随新消息实测捕获 pcieport 热插拔消息,Ctrl-C 退出
dmesg --follow-new仅打印新消息实测窗口内无新消息为空输出
dmesg --since/--until时间起点/终点过滤实测相对时间空输出、0 命中(ctime 元数据)
dmesg --raw / -r原始缓冲格式实测 <7>... 前缀
dmesg --color=always强制着色经 cat -v 显示 ^[[32m/^[[33m
dmesg -C / -c清空/读取并清空破坏性,需特权,未实测
dmesg -D / -E关闭/开启控制台打印控制类,未实测
dmesg -F <file>从文件读内核日志离线分析场景,未实测
dmesg -S强制走 syslog(2)兼容性因后端而异,未实测
dmesg -n <level>设置控制台打印级别控制类,未实测
dmesg -V打印版本实测 dmesg from util-linux 2.37.2
dmesg -h / --help帮助全文见 5.20

八、卸载方式

dmesg 由 util-linux 提供,它是 Ubuntu 22.04 的基础包,不推荐卸载(移除会影响 lsblk/mount/agetty 等同包命令)。如确需移除:

apt-get remove -y util-linux

⚠️ 注意:util-linux 是系统基础包,卸载可能导致基础命令与管理功能异常,生产环境请勿执行。