Home > Operation and Maintenance > Linux Operation and Maintenance > Do you really know how to debug Linux kernel failures? You will be enlightened after reading this article!

Do you really know how to debug Linux kernel failures? You will be enlightened after reading this article!

Release: 2023-08-03 16:50:34
forward
1098 people have browsed it

#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.

Use the dmesg command

The syntax of the dmesg command is as follows:

dmesg [OPTIONS]
在不带任何选项的情况下调用时,dmesg将所有消息从内核环形缓冲区写入标准输出:
Copy after login

$ 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
Copy after login

时间戳格式也可以使用--time-format选项设置,可以是ctime,reltime,delta,notime或iso。例如:要使用增量格式,你可以输入:

$ 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:

  • kern-kern message
  • user-user-level message
  • mail-mail system
  • daemon - system daemon
  • auth - security/authorization message
  • ##syslog - internal syslogd message
  • lpr-line printer subsystem
  • news-network news subsystem
  • -f( The --facility
    ) option allows you to limit the output to a specific device. The option accepts one or more comma-separated capabilities.
For example, to display only kernel and system daemon messages, you would use:

$ dmesg -f kern,daemon

Each log message is associated with a log level that shows the importance of the message. dmesg supports the following log levels:

  • emerg-The system is unavailable
  • alert-Action must be taken immediately
  • crit-Emergency
  • err-Error condition
  • warn-Warning condition
  • notice-Normal but important condition
  • info - Informational
  • debug - Debug level message

-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

Conclusion

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!

Related labels:
source:Linux中文社区
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template