linux命令 dmesg
dmesg
is used to print and control kernel ring buffer. This explanation may be confusing for the most of the readers. Here simplified explanation. dmesg
provides logs for troubleshoot, diagnose or save which is created by Linux kernel. It provides low level messages which is not logged by other subsystems.
dmesg
用于打印和控制内核环形缓冲区。 这种解释可能会使大多数读者感到困惑。 这里简化说明。 dmesg
提供由Linux内核创建的用于故障排除,诊断或保存的日志。 它提供了其他子系统未记录的低级消息。
(Provided Information Type)
dmesg
command and buffer provides a lot of different type of messages and logs. Here the list of some of them.
dmesg
命令和缓冲区提供了许多不同类型的消息和日志。 这里列出其中一些。
- SCSSI
- USBUSB
- Controllers
- Device Mapper设备映射器
- CPU中央处理器
- Hard Drive硬盘
- ……
(List and Print dmesg)
We will start using dmesg
command without an option. This will print all message to the current working shell.
我们将开始使用不带选项的dmesg
命令。 这会将所有消息打印到当前工作的shell中。
$ dmesg
List and Print dmesg
列出并打印dmesg
(List and Troubleshoot Hard Drive Devices)
One of the most used situation for dmesg message is the hard drive low level events. We can see operating system level actions of specified hard disk drive. We will filter with grep
command. In this example we will filter device named sda
.
dmesg消息最常使用的情况之一是硬盘驱动器低级别事件。 我们可以看到指定硬盘驱动器的操作系统级别的操作。 我们将使用grep
命令进行过滤。 在此示例中,我们将过滤名为sda
设备。
$ dmesg | grep sda
List and Troubleshoot Hard Drive Devices
列出硬盘设备并对其进行故障排除
列出USB并对其进行故障排除(List and Troubleshoot USB)
In order to list and troubleshoot USB and related events in operating system we will use grep
too. This will list all log lines those have the term usb
为了列出操作系统中的USB和相关事件并对其进行故障排除,我们还将使用grep
。 这将列出所有带有术语usb
日志行
$ dmesg | grep sda
List and Troubleshoot USB
列出USB并对其进行故障排除
列出并解决内存或RAM消息(List and Troubleshoot Memory or RAM message)
We can list and print memory or ram related messages with memory
term.
我们可以列出并打印带有memory
项的内存或内存相关消息。
$ dmesg | grep -i memory
List and Troubleshoot Memory or RAM message
列出并解决内存或RAM消息
打印并列出最后20行(Print and List Last 20 Lines)
While examining the dmesg messages we need to look specific time messages. We can print the last 20 lines of message with the last
command like below.
在检查dmesg消息时,我们需要查看特定的时间消息。 我们可以使用最后last
命令打印最后20行消息,如下所示。
$ dmesg | tail -n 20
Print and List Last 20 Lines
打印并列出最后20行
打印并列出前20行(Print and List First 20 Lines)
We can also print the first 20 lines of the dmesg provided log with the head
command like below.
我们还可以使用head
命令打印dmesg提供的日志的前20行,如下所示。
$ dmesg | head -n 20
Print and List First 20 Lines
打印并列出前20行
LEARN MORE Understanding and Configuring Apache Access Log
了解更多信息了解和配置Apache访问日志
实时列出和打印dmesg消息(List and Print dmesg Messages in Real Time)
We may need to print the logs and messages in real time. There is two way to accomplish this. First if our Linux kernel is newer than 3.5 we can use -w
option like below.
我们可能需要实时打印日志和消息。 有两种方法可以完成此操作。 首先,如果我们Linux内核版本高于3.5,我们可以使用-w
选项,如下所示。
$ dmesg -w
Or if it is older than 3.5 we can use tail
command like below.
或者,如果它早于3.5,则可以使用如下所示的tail
命令。
$ watch dmesg | tail -f
(Clear dmesg Message Buffer)
If we need to start troubleshooting and storing logs into a clear kernel ring buffer we need to remove existing log. We can use -c
option in order to clear the log buffer.
如果我们需要开始故障排除并将日志存储到明确的内核环形缓冲区中,则需要删除现有日志。 我们可以使用-c
选项来清除日志缓冲区。
$ dmesg -c
翻译自: https://www.poftut.com/use-linux-dmesg-command-print-system-logs/
linux命令 dmesg