Exploring Dmesg command in Linux

Anirudh Swarnakar
4 min readOct 30, 2019

--

From the last six months, I have been trying to get a hold on Linux internals, it’s structure, working, the flow of execution of commands etc. With over 13+ million lines of code, the Linux kernel is one of the largest open-source projects in the world and believe me it’s daunting if you emphasize on 13 million figure. But exploring and learning it part by part making modules of it virtually makes it a bit easier to get idea of what is happening under the hood. I am so far amazed to see how much thought process and engineering time went down making it what it is now and capable of doing. Obviously the evolution came with its iterations. I am working on Orange Pi( for now)and Computer Vision and it occurred to me that “Can I make it’s OS/Kernel more efficient for chunking my video files and process it and utilize its SIMD ( single instruction multiple data ) architecture for my purpose?” that’s where my motivation came about kernel.

The Linux kernel is the core of the operating system that controls access to the system resources, such as CPU, I/O devices, physical memory, and file systems. The kernel writes various messages to the kernel ring buffer during the boot process, and when the system is running. These messages include various information about the operation of the system, In embedded devices even about the Voltages. My Raspberry Pi used to throw under voltage detected kernel message whenever I power it from a cheap power adapter. The kernel ring buffer is a portion of the physical memory that holds the kernel’s log messages. It has a fixed size, which means once the buffer is full, the older logs records are overwritten. The dmesg command-line utility is used to print and control the kernel ring buffer in Linux and other Unix-like operating systems. It is useful for examining kernel boot messages and debugging hardware related issues.

Syntax of the dmesg command is

dmesg [OPTIONS]

When invoked without any options dmesg writes all messages from the kernel ring buffer to the standard output. Usually, the output contains a lot of lines of information, so only the last part of the output is viewable. If you are searching for kernel messages about a specific device or topic use GREP. For example to view messages about USB then

dmesg | grep -i usb

dmesg reads the messages generated by the kernel from the /proc/kmsg virtual file. This file provides an interface to the kernel ring buffer and can be opened only by one process.

Formatting options of dmesg command:-

dmesg provides with numerous options to format the output. Formatting the output makes it more readable as well as presentable in case you want to present it.
-H (--human) enables the human-readable output. It pipes the output to a pager.

dmesg -H

-T (--ctime) prints human-readable timestamps.
The timestamps format can also be set using the --time-format <format> option, which can be ctime, reltime, delta, notime, or iso. If timestamps are meaningless to you then use -t ( --notime) , then no timestamps will be printed.

dmesg -T
dmesg -t

-w (--follow) updates the output in realtime. Waits for new messages. This is only supported only on systems with a readable /dev/kmsg ( since kernel 3.5.0)

dmesg -w

-r (--raw) print the raw message buffer i.e. do not strip the log-level prefixes.
-P (--nopager) do not pipe output into a pager. A pager is enabled by default for --human format.
-L ( --color[=when]) Colorize the output. The optional argument when can be auto, never or always. If the when argument is omitted, it defaults to auto.

Dmesg also support multiple arguments or formats such as

 dmesg -H -T -w

Filtering dmesg output
dmesg contains messages about everything, getting specific information from that can be overwhelming. Here are some options to filter dmesg output.
Messages can be filtered based on facility or level. The -f ( --facility <list>) option allows you to limit the output to specific facilities. The option accepts one or more comma-separated facilities.
a. kern -kernel messages
b. user -user-level messages
c. mail -mail system
b. daemon -system daemons
e. auth -authorization/security messages
f. syslog -internal syslogd messages
g. lpr -line printer subsystem
h. news -network news subsystem

dmesg -f kern,user

this will only print kernel and user-level messages.
Filtering based on importance level, the -l (--level <list>) option restricts the output to defined levels. The option accepts one or more comma-separated levels. Supported levels are
a. crit -critical conditions
b. emerg -system is unusable
c. alert -action must be taken immediately
d. err -error conditions
e. warn -warning conditions
f. notice -normal but signinficant conditions
g. info -informational
h. debug -debug-level messages

dmesg -l crit,emerg

The above command displays only the emergency and critical messages.

Ring Buffer.

The -C ( --clear) option allows you to clear the ring buffer. Ring buffer can only be cleared by root :

sudo dmesg -C

To print the buffer contents before clearing use the -c (--read-clear) option:

sudo dmesh -c

If you want to save the current dmesg logs in a file before clearing it, redirect the output to a file:

dmesg > msg_op

Conclusion
The dmesg command allows you to view and control the kernel ring buffer. It can be very useful when troubleshooting kernel or hardware issues or developing device drivers.
Type man dmesg in your terminal for information about all available dmesg options.

--

--

Anirudh Swarnakar
Anirudh Swarnakar

Written by Anirudh Swarnakar

I am a Technophile with leisure pursuit in Robotics, Embedded Linux, IoT. Here on medium, you will find me sharing some of my experiences & acquired knowledge.