1.dmesg介绍

dmesg 是一个在 Linux 和类 Unix 操作系统中用于显示内核环缓冲区(kernel ring buffer)内容的命令。内核环缓冲区存储了系统启动时和运行过程中产生的日志信息,主要用于诊断硬件问题、驱动问题和其他内核相关的事件。

主要功能:
查看系统启动日志:在系统启动时,内核和驱动程序会输出许多信息,如硬件检测、驱动加载、错误信息等。通过 dmesg 可以查看这些启动日志。
查看硬件信息:dmesg 命令能显示与硬件设备(如磁盘、网络接口、USB设备等)相关的信息。
诊断和排查问题:如果硬件出现故障或设备驱动有问题,dmesg 输出的日志通常能够提供有用的诊断信息。

2.dmesg用法

[root@patrolagent ~]# dmesg --help

Usage:
 dmesg [options]

Options:
 -C, --clear                 clear the kernel ring buffer
 -c, --read-clear            read and clear all messages
 -D, --console-off           disable printing messages to console
 -d, --show-delta            show time delta between printed messages
 -e, --reltime               show local time and time delta in readable format
 -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                 colorize messages
 -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
 -r, --raw                   print the raw message buffer
 -S, --syslog                force to use syslog(2) rather than /dev/kmsg
 -s, --buffer-size <size>    buffer size to query the kernel ring buffer
 -T, --ctime                 show human readable timestamp (could be 
                               inaccurate if you have used SUSPEND/RESUME)
 -t, --notime                don't print messages timestamp
 -u, --userspace             display userspace messages
 -w, --follow                wait for new messages
 -x, --decode                decode facility and level to readable string

 -h, --help     display this help and exit
 -V, --version  output version information and exit

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(q).
[root@patrolagent ~]#

3.实例

3.1.查看内核缓冲区日志

命令:

dmesg

该命令会显示当前内核缓冲区中的所有日志信息

[root@patrolagent ~]# dmesg | tail -n 10
[    3.559989] XFS (dm-3): Ending clean mount
[    3.561241] XFS (dm-2): Ending clean mount
[    3.565719] systemd-journald[565]: Received request to flush runtime journal from PID 1
[    3.583667] type=1305 audit(1731659062.040:4): audit_pid=748 old=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:auditd_t:s0 res=1
[    3.790385] NET: Registered protocol family 40
[    3.924605] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[    3.927665] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[    3.928119] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[    3.928180] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[    5.381025] floppy0: no floppy controllers found
[root@patrolagent ~]#

3.2.查看特定类型的信息

命令:

dmesg |grep -i eth0

[root@patrolagent ~]# dmesg |grep -i eth0
[    1.514492] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:7c:8d:7b
[    1.514505] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection
[root@patrolagent ~]#

3.3.查看日志并实时更新

命令:

dmesg -w |head -n 5

[root@patrolagent ~]# dmesg -w | head -n 5
[    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-693.el7.x86_64 (mockbuild@x86-038.build.eng.bos.redhat.com) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Thu Jul 6 19:56:57 EDT 2017
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-693.el7.x86_64 root=/dev/mapper/rhel_patrolagent-root ro rd.lvm.lv=rhel_patrolagent/root rd.lvm.lv=rhel_patrolagent/swap rhgb quiet LANG=en_US.UTF-8
[root@patrolagent ~]#

3.4.显示特定时间段的日志

命令:

dmesg -T

使用-T选项可以将时间戳转换为可读的日期和时间格式。

[root@patrolagent ~]# dmesg -T |tail -n 5
[Fri Nov 15 16:24:20 2024] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[Fri Nov 15 16:24:20 2024] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[Fri Nov 15 16:24:20 2024] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[Fri Nov 15 16:24:20 2024] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[Fri Nov 15 16:24:22 2024] floppy0: no floppy controllers found
[root@patrolagent ~]#

3.5.日志清除

命令:

 dmesg -C

备注:通常需要root权限

[root@patrolagent ~]# dmesg -C
You have new mail in /var/spool/mail/root
[root@patrolagent ~]# dmesg
[root@patrolagent ~]#