dmesg(display message)
命令主要用来查看Linux内核信息(打印或控制内核环缓冲区),常用来排查系统故障,如硬件问题等
介绍
$ dmesg -h
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
-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
-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.
-h, --help display this help
-V, --version display version
...
常用命令
# 输入信息
dmesg
# 优化格式
dmesg -H
# 清除内核环形缓冲区
dmesg -c
# 显示时间
dmesg -e/-T
# 实时监控
dmesg --follow
# 消息级别
# emerg 系统无法使用
# alert 必须立即采取行动
# crit 临界条件
# err 错误条件
# warn 警告条件
# notice 正常但重要的情况
# info 信息
# debug 调试级消息
dmesg -l warn
dmesg -l warn,err
扩展
/dev/kmsg
有 dmesg 信息
- 权限问题
sudo sysctl kernel.dmesg_restrict=0
或添加如下文件并重启
$ cat /etc/sysctl.d/10-local.conf
kernel.dmesg_restrict = 0
- 缓冲区的大小由 CONFIG_LOG_BUF_SHIFT 控制,修改需要重新编译内核(参考)
size = 2**CONFIG_LOG_BUF_SHIFT
,16 => 64 KB
- 如果 dmesg 的消息被覆盖,可以在
/var/log/syslog
或 /var/log/messages
中查找历史信息