#The Linux kernel is the core of the operating system, which controls access to system resources (such as CPU, I/O devices, physical memory, and file systems). During the boot process and while the system is running, the kernel writes various messages to the kernel ring buffer. These messages include a variety of information about system operations.
The kernel ring buffer is a part of physical memory used to save the kernel's log messages. It has a fixed size, which means that once the buffer is full, older log records will be overwritten.
The dmesg command line utility is used to print and control the kernel ring buffer in Linux and other Unix-like operating systems. Useful for inspecting kernel boot messages and debugging hardware-related issues.
In this tutorial, we will cover the basics of the dmesg command.
The syntax of the dmesg command is as follows:
dmesg [OPTIONS] 在不带任何选项的情况下调用时,dmesg将所有消息从内核环形缓冲区写入标准输出:
$ dmesg
Default , all users can run the dmesg command. However, on some systems, access to dmesg may be restricted to non-root users. In this case, you will receive the following error message when calling dmesg:
dmesg: readkernel buffer failed: Operation not permitted
Kernel parameterskernel.dmesg_restrictSpecifies whether non-privileged users can use dmesg to view messages from the kernel log buffer information. To remove the restriction, set it to zero:
$ sudo sysctl -w kernel.dmesg_restrict=0
Typically the output contains many lines of information, so only See the last part of the output. To view one page at a time, pipe the output to a paging utility such as less or more:
$ dmesg --color=always | less
where The --color=always parameter is used to preserve colored output.
If you want to filter buffer messages, you may use grep. For example, to view only USB-related messages, type:
$ dmesg | grep -i usb
dmesg 从/proc/kmsg虚拟文件中读取内核生成的消息。该文件提供了到内核环形缓冲区的接口,并且只能由一个进程打开。如果系统上正在运行syslog进程,并且你尝试使用cat或less命令读取文件,则命令将挂起。
syslog守护程序将内核消息转储到/var/log/dmesg,因此你也可以使用该日志文件:
$ cat /var/log/dmesg
格式化 dmesg 输出。
dmesg命令提供了许多选项,可帮助你格式化和过滤输出。
dmesg中最常用的选项之一是-H(--human),它将输出更容易读的结果。
$ dmesg -H
要打印人类可读的时间戳,请使用-T(--ctime选项):
$ dmesg -T [Mon Oct 14 14:38:04 2019] IPv6: ADDRCONF(NETDEV_CHANGE): wlp1s0: link becomes ready
时间戳格式也可以使用--time-format
$ dmesg --time-format=delta
You can also combine two or more options:
$ dmesg -H -T
To watch the output of the dmesg command in real time, use the -w (--follow) option:
$ dmesg --follow
Filter dmesg output.
You can limit dmesg output to a given facility and level. dmesg supports the following types:
$ dmesg -f kern,daemon
-l( --level <list>)
option allows you to limit the output to a defined level, this option accepts one or more comma separated levels. The following command displays only error and critical messages:
$ dmesg -l err,crit
clear ring buffer
-C (--clear ) option allows you to clear the ring buffer:
$ sudo dmesg -C
Only root or a user with sudo privileges can clear the buffer.
To print the buffer contents before clearing, use the -c (--read-clear) option:
$ sudo dmesg -c
If you want to Save the current dmesg log to a file before clearing it, you can redirect the output to a file:
$ dmesg > dmesg_messages
The dmesg command allows you to view and control the kernel ring buffer. It is useful when troubleshooting kernel or hardware issues.
Enter man dmesg in the terminal and you can get information about all available dmesg options.
The above is the detailed content of Do you really know how to debug Linux kernel failures? You will be enlightened after reading this article!. For more information, please follow other related articles on the PHP Chinese website!