您的位置:首页 > 运维架构 > Linux

【Linux】一步一步学Linux——dmesg命令(74)

2019-07-29 22:40 3083 查看
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

00. 目录

文章目录

01. 命令概述

dmesg命令被用于检查和控制内核的环形缓冲区。kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。开机信息保存在

/var/log/dmesg
文件里。

02. 命令格式

dmesg [选项]

03. 常用选项

选项:
-C, --clear                 清除内核环形缓冲区(ring butter)
-c, --read-clear            读取并清除所有消息
-D, --console-off           禁止向终端打印消息
-d, --show-delta            显示打印消息之间的时间差
-e, --reltime               以易读格式显示本地时间和时间差
-E, --console-on            启用向终端打印消息
-F, --file <文件>           用 文件 代替内核日志缓冲区
-f, --facility <列表>       将输出限制为定义的设施
-H, --human                 易读格式输出
-k, --kernel                显示内核消息
-L, --color                 显示彩色消息
-l, --level <列表>          限制输出级别
-n, --console-level <级别>  设置打印到终端的消息级别
-P, --nopager               不将输出通过管道传递给分页程序
-r, --raw                   打印原生消息缓冲区
-S, --syslog                强制使用 syslog(2) 而非 /dev/kmsg
-s, --buffer-size <大小>    查询内核环形缓冲区所用的缓冲区大小
-T, --ctime                 显示易读的时间戳(如果您使用了
SUSPEND/RESUME 则可能不准)
-t, --notime                不打印消息时间戳
-u, --userspace             显示用户空间消息
-w, --follow                等待新消息
-x, --decode                将设施和级别解码为可读的字符串

-h, --help     显示此帮助并退出
-V, --version  输出版本信息并退出

支持的日志设施:
kern - 内核消息
user - 随机的用户级消息
mail - 邮件系统
daemon - 系统守护进程
auth - 安全/认证消息
syslog - syslogd 内部生成的消息
lpr - 行打印机子系统
news - 网络新闻子系统

支持的日志级别(优先级):
emerg - 系统无法使用
alert - 操作必须立即执行
crit - 紧急条件
err - 错误条件
warn - 警告条件
notice - 正常但重要的条件
info - 信息
debug - 调试级别的消息

04. 参考示例

4.1 列出加载到内核中的所有驱动

dmesg
命令设备故障的诊断是非常重要的。在
dmesg
命令的帮助下进行硬件的连接或断开连接操作时,我们可以看到硬件的检测或者断开连接的信息。
dmesg
命令在多数基于Linux和Unix的操作系统中都可以使用。

[root@itcast ~]# dmesg

4.2 列出所有被检测到的硬件

要显示所有被内核检测到的硬盘设备,你可以使用grep命令搜索

sda
关键词

[root@itcast ~]# dmesg | grep sda
[    3.051454] sd 0:0:0:0: [sda] 209715200 512-byte logical blocks: (107 GB/100 GiB)
[    3.051559] sd 0:0:0:0: [sda] Write Protect is off
[    3.051562] sd 0:0:0:0: [sda] Mode Sense: 61 00 00 00
[    3.051700] sd 0:0:0:0: [sda] Cache data unavailable
[    3.051702] sd 0:0:0:0: [sda] Assuming drive cache: write through
[    3.053408]  sda: sda1 sda2 sda3
[    3.054299] sd 0:0:0:0: [sda] Attached SCSI disk
[    3.558354] XFS (sda3): Mounting V5 Filesystem
[    4.188185] XFS (sda3): Ending clean mount
[   12.636995] Adding 4194300k swap on /dev/sda2.  Priority:-1 extents:1 across:4194300k FS
[   12.922553] XFS (sda1): Mounting V5 Filesystem
[   14.708567] XFS (sda1): Ending clean mount
[root@itcast ~]#

4.3 只输出dmesg命令的前10行日志

[root@itcast ~]# dmesg | head
[    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-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-862.el7.x86_64 root=UUID=6fa6fd5d-0115-4ec3-b993-19322c836a32 ro rhgb quiet LANG=zh_CN.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
[root@itcast ~]#

4.4 只输出dmesg命令最后10行日志

[root@itcast ~]# dmesg | tail
[117804.072174] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[117804.078051] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[117804.079860] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[117804.079873] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[117831.955328] psmouse serio1: VMMouse at isa0060/serio1/input0 lost synchronization, throwing 2 bytes away.
[117833.738722] psmouse serio1: resync failed, issuing reconnect request
[124926.680233] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[124926.693764] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[124932.700536] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[124932.701799] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[root@itcast ~]#

4.5 搜索包含特定字符串的被检测到的硬件

由于‘dmesg’命令的输出实在太长了,在其中搜索某个特定的字符串是非常困难的。因此,有必要过滤出一些包含‘usb’ ‘dma’ ‘tty’ ‘memory’等字符串的日志行。grep 命令 的‘-i’选项表示忽略大小写。

[root@itcast ~]# dmesg | grep -i usb
[root@itcast ~]# dmesg | grep -i dma
[root@itcast ~]# dmesg | grep -i tty
[root@itcast ~]# dmesg | grep -i memory

4.6 清空dmesg缓冲区日志

[root@itcast ~]# dmesg -c

我们可以使用如下命令来清空dmesg的日志。该命令会清空dmesg环形缓冲区中的日志。但是你依然可以查看存储在‘/var/log/dmesg’文件中的日志。你连接任何的设备都会产生dmesg日志输出。

4.7 实时监控dmesg日志输出

[root@itcast ~]# tail -f /var/log/dmesg
[   14.708567] XFS (sda1): Ending clean mount
[   14.880408] floppy0: no floppy controllers found
[   14.880489] work still pending
[   14.988537] device-mapper: uevent: version 1.0.3
[   14.988785] device-mapper: ioctl: 4.37.0-ioctl (2017-09-20) initialised: dm-devel@redhat.com
[   15.604238] RPC: Registered named UNIX socket transport module.
[   15.604240] RPC: Registered udp transport module.
[   15.604241] RPC: Registered tcp transport module.
[   15.604242] RPC: Registered tcp NFSv4.1 backchannel transport module.
[   15.783627] type=1305 audit(1563324893.915:3): audit_pid=581 old=0 auid=4294967295 ses=4294967295 res=1

//或者
[root@itcast ~]# watch "dmesg |tail"

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

参考:https://www.geek-share.com/detail/2618751502.html

~]# watch “dmesg |tail”

## 05. 附录

参考:[【Linux】一步一步学Linux系列教程汇总](https://www.geek-share.com/detail/2773255823.html)

参考:https://www.geek-share.com/detail/2618751502.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: