Linux(Centos 7.6)命令详解:dmesg

1.命令作用

打印或控制内核环形缓冲区(print or control the kernel ring buffer);内核(kernel)会将开机信息存储在环形缓冲区(ring buffer)中,开机时一般来不及查看开机信息,就可以使用dmesg(display message)打印开机信息查看;开机信息也会保存在/var/log/dmesg中,也可以通过该文件查看

2.命令语法

Usage: dmesg [options]

3.参数详解

OPTION:

  • -C, --clear,清除内核环缓冲区
  • -c, --read-clear,阅读并清除所有消息
  • -D, --console-off,禁用向控制台打印消息,不输出任何内容
  • -d, --show-delta,显示打印消息之间的时间差
  • -e, --reltime,以易于理解的格式显示本地时间和时间差值
  • -E, --console-on,启用向控制台打印消息的功能
  • -F, --file <file>,使用该文件而非内核日志缓冲区
  • -f, --facility <list>,限制输出仅限于指定的设施;如kern、user、mail等,详见"日志设施"
  • -H, --human,人类可读的输出;添加-e -L的简化方式,另外可分页显示
  • -k, --kernel,显示内核消息,等同于-f kern
  • -L, --color,为消息着色
  • -l, --level <list>,将输出限制在规定的范围内;如info、debug、err等,详见"日志级别"
  • -n, --console-level <level>,设置打印到控制台的消息级别(无明显效果)
  • -P, --nopager,不要将输出内容直接发送到分页器中,与默认效果一致,与-H(分页)相反
  • -r, --raw,打印原始消息缓冲区(注:缓冲区与日志文件dmesg信息不完全一样)
  • -S, --syslog,强制使用syslog而非 /dev/kmsg
  • -s, --buffer-size <size>,用于查询内核环形缓冲区的缓冲区大小
  • -T, --ctime,显示人类可读的时间戳(如果使用了“暂停/恢复”功能,则可能不准确)
  • -t, --notime,不要打印消息的时间戳
  • -u, --userspace,显示用户空间消息
  • -w, --follow,等待新消息
  • -x, --decode,将编码设施和级别转换为可读字符串;即每行开头显示日志设施和日志级别
  • -h, --help,显示此帮助信息并退出
  • -V, --version,输出版本信息并退出

支持的日志设施(Supported log facilities):

  • kern,内核消息(kernel messages)
  • user,随机的用户级消息(random user-level messages)
  • mail,邮件系统(mail system)
  • daemon,系统守护进程(system daemons)
  • auth,安全/授权消息(security/authorization messages)
  • syslog,由syslogd自行生成的消息(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)

4.常用用例

4.1.查看Linux开机信息

4.1.1.内核环形缓冲区查看

[root@node2 ~]# dmesg
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-957.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Thu Nov 8 23:39:32 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=UUID=866b7b3c-2489-4bce-b78b-9d1c7950a7df ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
... ...
... ...
[    8.530572] Adding 2097148k swap on /dev/sda2.  Priority:-2 extents:1 across:2097148k FS
[    8.620050] XFS (sda1): Mounting V5 Filesystem
[    8.939129] input: PC Speaker as /devices/platform/pcspkr/input/input5
[    9.221967] XFS (sda1): Starting recovery (logdev: internal)
[    9.224524] XFS (sda1): Ending recovery (logdev: internal)
[    9.459693] cryptd: max_cpu_qlen set to 1000
[    9.534476] AVX2 version of gcm_enc/dec engaged.
[    9.534478] AES CTR mode by8 optimization enabled
[    9.623654] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    9.632463] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)
[    9.824208] ppdev: user-space parallel port driver
[   10.191485] floppy0: no floppy controllers found
[   10.191522] work still pending
[   10.560245] device-mapper: uevent: version 1.0.3
[   10.560765] device-mapper: ioctl: 4.37.1-ioctl (2018-04-03) initialised: dm-devel@redhat.com
[   10.654104] type=1305 audit(1752893866.948:3): audit_pid=6365 old=0 auid=4294967295 ses=4294967295 res=1
[   10.695169] RPC: Registered named UNIX socket transport module.
[   10.695171] RPC: Registered udp transport module.
[   10.695172] RPC: Registered tcp transport module.
[   10.695173] RPC: Registered tcp NFSv4.1 backchannel transport module.
[   11.872408] NET: Registered protocol family 40
[   12.211849] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[   12.221656] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   12.226404] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[   12.239755] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[   14.806693] ip6_tables: (C) 2000-2006 Netfilter Core Team
[   14.884133] Ebtables v2.0 registered
[   15.167746] bridge: filtering via arp/ip/ip6tables is no longer available by default. Update your scripts to load br_netfilter if you need this.
[   15.195845] tun: Universal TUN/TAP device driver, 1.6
[   15.195848] tun: (C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>
[   15.196603] virbr0: port 1(virbr0-nic) entered blocking state
[   15.196606] virbr0: port 1(virbr0-nic) entered disabled state
[   15.196798] device virbr0-nic entered promiscuous mode
[   15.409641] nf_conntrack version 0.5.0 (7780 buckets, 31120 max)
[   15.627795] virbr0: port 1(virbr0-nic) entered blocking state
[   15.627798] virbr0: port 1(virbr0-nic) entered listening state
[   15.627855] IPv6: ADDRCONF(NETDEV_UP): virbr0: link is not ready
[   15.774677] virbr0: port 1(virbr0-nic) entered disabled state
[   49.504059] fuse init (API version 7.22)
[   58.677880] TCP: lp registered
[ 4957.466602] e1000: ens33 NIC Link is Down
[ 4961.476272] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 7064.300080] e1000: ens33 NIC Link is Down
[ 7070.318608] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11725.890410] e1000: ens33 NIC Link is Down
[11731.904552] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11743.939929] e1000: ens33 NIC Link is Down
[11747.948416] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 

4.1.2.开机信息日志查看

[root@node2 ~]# cat /var/log/dmesg
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-957.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Thu Nov 8 23:39:32 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=UUID=866b7b3c-2489-4bce-b78b-9d1c7950a7df ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved
... ...
... ...
[    8.530572] Adding 2097148k swap on /dev/sda2.  Priority:-2 extents:1 across:2097148k FS
[    8.620050] XFS (sda1): Mounting V5 Filesystem
[    8.939129] input: PC Speaker as /devices/platform/pcspkr/input/input5
[    9.221967] XFS (sda1): Starting recovery (logdev: internal)
[    9.224524] XFS (sda1): Ending recovery (logdev: internal)
[    9.459693] cryptd: max_cpu_qlen set to 1000
[    9.534476] AVX2 version of gcm_enc/dec engaged.
[    9.534478] AES CTR mode by8 optimization enabled
[    9.623654] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    9.632463] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)
[    9.824208] ppdev: user-space parallel port driver
[   10.191485] floppy0: no floppy controllers found
[   10.191522] work still pending
[   10.560245] device-mapper: uevent: version 1.0.3
[   10.560765] device-mapper: ioctl: 4.37.1-ioctl (2018-04-03) initialised: dm-devel@redhat.com
[   10.654104] type=1305 audit(1752893866.948:3): audit_pid=6365 old=0 auid=4294967295 ses=4294967295 res=1
[   10.695169] RPC: Registered named UNIX socket transport module.
[   10.695171] RPC: Registered udp transport module.
[   10.695172] RPC: Registered tcp transport module.
[   10.695173] RPC: Registered tcp NFSv4.1 backchannel transport module.
[root@node2 ~]# 

4.1.3.以上两种方式的区别

  • dmesg命令查询的缓冲里面的信息包括了开机信息,也包括开机后的一些其他信息,比如网卡准备信息等;/var/log/dmesg日志信息仅包括开机信息

4.2.显示信息之间的时间差

4.2.1.添加时间差显示

# 每行在第一列时间后增加显示每行时间差的信息
# 可用于查看开机信息慢的位置
[root@node2 ~]# dmesg -d | tail
[   49.504059 <   33.729382>] fuse init (API version 7.22)
[   58.677880 <    9.173821>] TCP: lp registered
[ 4957.466602 < 4898.788722>] e1000: ens33 NIC Link is Down
[ 4961.476272 <    4.009670>] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 7064.300080 < 2102.823808>] e1000: ens33 NIC Link is Down
[ 7070.318608 <    6.018528>] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11725.890410 < 4655.571802>] e1000: ens33 NIC Link is Down
[11731.904552 <    6.014142>] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11743.939929 <   12.035377>] e1000: ens33 NIC Link is Down
[11747.948416 <    4.008487>] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 

4.2.1.替换为时间差显示

# 不显示时间,替换成每行的时间差显示
[root@node2 ~]# dmesg -e | tail
[ +33.729382] fuse init (API version 7.22)
[Jul21 10:20] TCP: lp registered
[Jul21 11:41] e1000: ens33 NIC Link is Down
[  +4.009670] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[Jul21 12:16] e1000: ens33 NIC Link is Down
[  +6.018528] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[Jul21 13:34] e1000: ens33 NIC Link is Down
[  +6.014142] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ +12.035377] e1000: ens33 NIC Link is Down
[  +4.008487] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 

4.3.显示可读的时间格式

[root@node2 ~]# dmesg -T | tail
[Mon Jul 21 10:19:55 2025] fuse init (API version 7.22)
[Mon Jul 21 10:20:04 2025] TCP: lp registered
[Mon Jul 21 11:41:43 2025] e1000: ens33 NIC Link is Down
[Mon Jul 21 11:41:47 2025] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[Mon Jul 21 12:16:50 2025] e1000: ens33 NIC Link is Down
[Mon Jul 21 12:16:56 2025] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[Mon Jul 21 13:34:31 2025] e1000: ens33 NIC Link is Down
[Mon Jul 21 13:34:37 2025] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[Mon Jul 21 13:34:49 2025] e1000: ens33 NIC Link is Down
[Mon Jul 21 13:34:53 2025] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 

4.4.不打印时间戳

[root@node2 ~]# dmesg -t | tail
fuse init (API version 7.22)
TCP: lp registered
e1000: ens33 NIC Link is Down
e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
e1000: ens33 NIC Link is Down
e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
e1000: ens33 NIC Link is Down
e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
e1000: ens33 NIC Link is Down
e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 

4.5.打印用户空间信息

[root@node2 ~]# dmesg -u | tail
[    1.236212] systemd[1]: Starting Create list of required static device nodes for the current kernel...
[    1.243273] systemd[1]: Started Create list of required static device nodes for the current kernel.
[    1.246898] systemd[1]: Started Apply Kernel Variables.
[    1.247276] systemd[1]: Starting Create Static Device Nodes in /dev...
[    1.252249] systemd[1]: Started Create Static Device Nodes in /dev.
[    1.324615] systemd[1]: Started Setup Virtual Console.
[    1.336502] systemd[1]: Started Journal Service.
[    5.197986] systemd-journald[88]: Received SIGTERM from PID 1 (systemd).
[    5.238596] systemd[1]: Inserted module 'ip_tables'
[    5.474499] systemd-journald[2977]: Received request to flush runtime journal from PID 1
[root@node2 ~]# 

4.6.打印日志设施和日志级别

[root@node2 ~]# dmesg -x | tail
kern  :info  : [   49.504059] fuse init (API version 7.22)
kern  :info  : [   58.677880] TCP: lp registered
kern  :info  : [ 4957.466602] e1000: ens33 NIC Link is Down
kern  :info  : [ 4961.476272] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
kern  :info  : [ 7064.300080] e1000: ens33 NIC Link is Down
kern  :info  : [ 7070.318608] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
kern  :info  : [11725.890410] e1000: ens33 NIC Link is Down
kern  :info  : [11731.904552] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
kern  :info  : [11743.939929] e1000: ens33 NIC Link is Down
kern  :info  : [11747.948416] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 

4.7.打印指定的日志设施

[root@node2 ~]# dmesg -f kern | tail             # 打印内核信息
[   49.504059] fuse init (API version 7.22)
[   58.677880] TCP: lp registered
[ 4957.466602] e1000: ens33 NIC Link is Down
[ 4961.476272] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 7064.300080] e1000: ens33 NIC Link is Down
[ 7070.318608] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11725.890410] e1000: ens33 NIC Link is Down
[11731.904552] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11743.939929] e1000: ens33 NIC Link is Down
[11747.948416] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 
[root@node2 ~]# dmesg -f daemon | tail           # 系统守护进程
[    1.234933] systemd[1]: Starting Apply Kernel Variables...
[    1.235548] systemd[1]: Starting Journal Service...
[    1.236212] systemd[1]: Starting Create list of required static device nodes for the current kernel...
[    1.243273] systemd[1]: Started Create list of required static device nodes for the current kernel.
[    1.246898] systemd[1]: Started Apply Kernel Variables.
[    1.247276] systemd[1]: Starting Create Static Device Nodes in /dev...
[    1.252249] systemd[1]: Started Create Static Device Nodes in /dev.
[    1.324615] systemd[1]: Started Setup Virtual Console.
[    1.336502] systemd[1]: Started Journal Service.
[    5.238596] systemd[1]: Inserted module 'ip_tables'
[root@node2 ~]# 
[root@node2 ~]# dmesg -f syslog | tail           # 打印syslog自动生成的信息
[    5.197986] systemd-journald[88]: Received SIGTERM from PID 1 (systemd).
[    5.474499] systemd-journald[2977]: Received request to flush runtime journal from PID 1
[root@node2 ~]# 

4.8.打印指定的日志级别

[root@node2 ~]# dmesg -l info | tail               # 打印info日志级别信息
[   49.504059] fuse init (API version 7.22)
[   58.677880] TCP: lp registered
[ 4957.466602] e1000: ens33 NIC Link is Down
[ 4961.476272] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 7064.300080] e1000: ens33 NIC Link is Down
[ 7070.318608] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11725.890410] e1000: ens33 NIC Link is Down
[11731.904552] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[11743.939929] e1000: ens33 NIC Link is Down
[11747.948416] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@node2 ~]# 
[root@node2 ~]# dmesg -l debug | tail              # 打印调试debug日志级别信息
[    1.183171] ehci-pci 0000:02:03.0: cache line size of 64 is not supported
[    1.195166] PM: Hibernation image not present or could not be loaded.
[    1.802416] libata version 3.00 loaded.
[    1.822264] ata_piix 0000:00:07.1: version 2.13
[    4.364353] sd 2:0:0:0: [sda] Mode Sense: 61 00 00 00
[    4.366410] sd 2:0:1:0: [sdb] Mode Sense: 61 00 00 00
[    4.388158] sr 1:0:0:0: Attached scsi CD-ROM sr0
[    5.228087] SELinux:  Unregistering netfilter hooks
[    6.050998] vmw_vmci 0000:00:07.7: irq 56 for MSI/MSI-X
[    6.051044] vmw_vmci 0000:00:07.7: irq 57 for MSI/MSI-X
[root@node2 ~]# 
[root@node2 ~]# dmesg -l err | tail                # 打印错误日志级别信息
[    4.364429] sd 2:0:0:0: [sda] Assuming drive cache: write through
[    4.366476] sd 2:0:1:0: [sdb] Assuming drive cache: write through
[    6.022994] piix4_smbus 0000:00:07.3: SMBus Host Controller not enabled!
[root@node2 ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆是浪个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值