A tutorial on dmesg command in Linux

Dmesg Command Featured Image

dmesg command, shorthand for “diagnostic message”, is a Linux utility for displaying the messages that flow within the kernel ring buffer. A kernel ring buffer is a logging mechanism from the system startup which comes into play for diagnostics of a failed device.

The diagnostic messages sent by the devices may differ in their formats depending on the type of device. The traditional format of the message contains the type of the device followed by a colon. Then follows the detailed message sent by the device. Each line is timestamped by the number of seconds since boot.

Let us take a look at the basic output of the dmesg command.

Basic Output of dmesg command

In order to display the complete unfiltered output on the terminal, we simply enter dmesg. It is advised to pipe the output to the less command since the output may contain hundreds of lines of messages from the time of start-up.

dmesg | less
Dmesg Command Basic Output
Basic Output of ‘dmesg’ command

This kernel-related jargon is useful to programmers who are looking for specific information. For instance, to fetch the Linux kernel version, we run:

dmesg | grep "Linux version"
Dmesg Command Get Version
Linux Kernel version using ‘dmesg’ command

The extracted part in the above figure is also present in the basic output of the dmesg command, but along with loads of other information, making it unnoticeable.

The grep command is a searching tool in Linux. We have a complete article dedicated to the grep command.


Extract messages for a specific device

Using the combination of dmesg and grep command, we can easily extract the information for a specific device from the seemingly never-ending output. For instance, to get a glimpse of messages related to the keyboard device, we run:

dmesg | grep -i "Keyboard"
Dmesg Command Keyboard
Keyboard-specific ‘dmesg’ output

In the above snippet of the terminal, we are looking at the messages exchanged between kernel and keyboard drivers.


Display messages along with type of facilities and levels

The messages displayed can be grouped into two classes:

  • Facility: The facility responsible for the message, like “kern” for kernel messages.
  • Level: The level of importance of the message, like “warn” for warnings.

With the help of '-x' option, we can alter the output format so that the facilities and levels are visible.

dmesg -x | less
Dmesg Command Facilities Levels
Facilities and Levels in ‘dmesg’ output

The name of the facility is followed by the type of the level. These messages can be filtered out on the basis of their types.

Filter messages on the basis of facilities

The '-f' or the '--facility' can be used to filter the messages for specified facilities.

dmesg -x -f <FACILITY>
Dmesg Command Facility
Filter facility-type

As shown in the figure, for filtering multiple facility types, their keywords are appended with a comma in between. We can get to know about the type of facilities by running dmesg -h.


Filter messages on the basis of levels

Similar to the previous method, the '-l' or the '--level' option is used specify the type of levels to display.

dmesg -x -l <LEVEL>
Dmesg Command Level
Filter level-type

We can filter multiple types of levels using this method. The list of levels can be accessed through the terminal by running dmesg -h.


Print messages with Human-readable timestamps

The numeric timestamps can not be interpreted easily, therefore dmesg command provides an option to switch the numeric timestamps to human-readable timestamps. This can be achieved by '-T' flag.

dmesg -T
Dmesg Command Timestamp
Human-readable timestamps

The new timestamp include date and time of the moment the message was exchanged. The timestamps can be inaccurate as the time-record is not updated after system is paused or suspended.


Clearing the dmesg logs

The dmesg command provides the ability to clear the logs stored in the ring buffer. The '-c' option is used for this purpose.

dmesg -C
Dmesg Command Clear
Clear ‘dmesg’ logs

Since the data stored in the ring buffer is nothing but log information, deleting it would not affect any functioning of the devices. This only affects the ability to perform a diagnosis on any device that is not working properly.


Conclusion

There is not much into this Linux diagnostic tool. Curious readers can access the manual pages through the terminal by running man dmesg.

We hope this article was easy to follow by the reader. Feel free to comment below for any queries or suggestions.