dmesg Command in Linux



The Linux dmesg command displays or controls the kernel ring buffer. The kernel ring buffer stores log messages generated by various kernel parts such as the device drivers.

The device drivers manage various hardware components such as graphics cards, storage devices, or network adapters. These log messages play a vital role in troubleshooting or debugging the system anomalies.

It is important to note that the ring buffer only has a fixed amount of memory. Once it is filled, it overwrites the oldest messages.

Table of Contents

Here is a comprehensive guide to the options available with the dmesg command −

Syntax of dmesg Command

The syntax for using the Linux dmesg command is as follows −

dmesg [options]

The [options] field is used to print log messages in a specific format.

dmesg Command Options

The commonly used options for the dmesg command are listed in the table below −

FlagsOptionsDescription
-C--clearIt clears the ring buffer
-c--read-clearIt displays the content of the buffer and then clears it
-D--console-offIt disables printing the messages to the console
-d--show-deltaIt shows the timestamp and delta time spent between messages
-E--console-onIt enables the printing the messages to the console
-e--reltimeIt displays the local time and delta in human-readable format (time conversion can be inaccurate)
-F file--file fileIt reads the system log messages from the specified file (it does not support kmsg format)
-f list--facility listTo list specific facilities from the output (e.g. facility=daemon)
-H--humanIt displays the output in human-readable format
-J--jsonIt displays the output in JSON format
-K file--kmsg-file fileIt reads the /dev/kmsg messages from a file
-L when--color=whenIt colorizes the output (auto, never, always)
-l list--level listTo list specific levels from the output e.g. facility=err, warn)
-n level--console-levelTo print messages of the specific level only
--noescapeTo disable escaping the unprintable or unsafe characters
-P--no-pagerIt disables the pagination of the output
-r--rawIt prints the raw output without stripping the log-level prefixes (unprintable data still escapes)
-S--syslogIt forces the command to read the syslog instead of /dev/kmsg
-T--ctimeIt prints the human-readable timestamps (which can be inaccurate)
--since timeIt displays the messages since the specified time
--until timeIt displays the messages till the specified time
-t--no-timeIt does not display the timestamp with kernel messages
--time-formatIt prints the timestamps according to the given format
-u--userspaceIt displays the user space messages
-w--followIt waits for the new messages (from kernel version 3.5.0 and above)
-W--follow-newIt waits and displays only new messages
-x--decodeIt decodes facility and level numbers to human-readable prefixes
--helpIt prints help about command
--versionIt prints the command version

Examples of dmesg Command in Linux

This section demonstrates the usage of the Linux dmesg command in Linux using examples −

  • Printing all the Kernel Messages
  • Printing the dmesg Command Output in Scrollable Format
  • Printing dmesg Command Output in Human Readable Format
  • Printing the dmesg Message Timestamps in Human Readable Format
  • Printing the dmesg Command Output in JSON Format
  • Printing the dmesg Command Output using Log Level
  • Printing dmesg Command Output using Facilities
  • Printing the dmesg Command Output with both Facility and Level
  • Extracting the Specific Information from the dmesg Command Output
  • Viewing the Live dmesg Command Output

Printing all the Kernel Messages

To print all the messages from the kernel ring buffer, execute the dmesg command with sudo privileges −

sudo dmesg
Printing All Kernel Messages dmesg

Printing the dmesg Command Output in Scrollable Format

By default, the dmesg command prints all the messages in one output. It sometimes becomes hard to read the messages. To make the output scrollable output can be piped to the less command.

sudo dmesg | less
dmesg Command Output in Scrollable Format

Printing dmesg Command Output in Human Readable Format

By default, the timestamps of the dmesg command are displayed in seconds or nanoseconds since the kernel started. To display them in human-readable format, use the -H flag or --human option.

sudo dmesg -H
dmesg Command Output in Human Readable Format 1

The output shows the timestamp in the human-readable format. Note that the output is also scrollable.

dmesg Command Output in Human Readable Format 2

Printing the dmesg Message Timestamps in Human Readable Format

To print the message timestamps in human-readable format use the -T or --ctime option −

sudo dmesg -T
dmesg Message Timestamps in Human Readable Format

Printing the dmesg Command Output in JSON Format

To display the output in JSON format -J or --json options are used. The JSON format is a key-value pair format. Each message is represented by 3 keys: pri, time, and msg, with their respective values.

sudo dmesg -J
dmesg Command Output in JSON Format

Printing the dmesg Command Output using Log Level

Each dmesg message is stored in the buffer with a specific log level. The log levels play a key role in troubleshooting especially finding the level of attention required to fix an issue. These levels are listed below −

LevelNumerical ValueDescription
emerg0It indicates the emergency state which means that the system is unstable (for example: hardware failure)
alert1It indicates the alerts, which means an immediate action is required (for example: data corruption)
crit2It indicates the critical state that can affect the system (for example: out of memory errors)
err3It indicates the error state that could disrupt normal operations (for example: connection failures)
warn4It indicates the potential issue but does not require immediate action (for example: low storage)
notice5It indicates normal operation but is significant enough to require attention (example: user logged in)
info6It shows the general information message about normal operations
debug7It provides the detailed information for debugging

The numerical values 0-7 are used in syslog or similar logging systems to manage the logs based on severity.

To print messages of a specified level, use the -l or --level option with the level name −

sudo dmesg -l warn

Or,

sudo dmesg --level=warn
dmesg Command Output using Log Level 1

To display messages of the different levels, use −

sudo dmesg -l warn,debug 
dmesg Command Output using Log Level 2

Multiple levels can be mentioned separating them by comma (,).

Printing dmesg Command Output using Facilities

The log messages are categorized into groups called facilities. The facilities are listed below −

FacilityDescription
kernIt shows the logs generated by the kernel
userIt shows the logs generated by the user
mailIt shows the logs generated by the mail system
daemonIt shows the logs generated by the system daemon
authIt shows security and authorization-related logs
syslogIt shows the logs generated by the internal syslog
lprIt shows line printer-related logs
newsIt shows the logs generated by the network news subsystem

To display the message of a facility, use the -f or --facility option −

sudo dmesg -f kern

Or,

sudo dmesg --facility=kern
dmesg Command Output using Facilities 1

To display messages from multiple facilities, use −

sudo dmesg -f kern,daemon
dmesg Command Output using Facilities 2

Printing the dmesg Command Output with both Facility and Level

To print the facility and level with each log message, use -x flag or --decode option −

sudo dmesg -x
dmesg Output with both Facility and Level

Extracting the Specific Information from the dmesg Command Output

It can be piped to the grep command to print specific information from the dmesg command output. For instance, to extract information about the specific device, mention the device name with the grep command −

sudo dmesg | grep -i vda
Extracting Specific Info from dmesg Command Output

The -i option is used to make the search case insensitive.

Viewing the Live dmesg Command Output

To print the live into the console, use -w or --follow option −

sudo dmesg -w

Or,

sudo dmesg --follow
Viewing Live dmesg Command Output

It prints the messages as they are logged. Note that it also prints the previous messages. To live-print the new messages only, use -W

sudo dmesg -W

To exit the wait, press Ctrl + C.

Conclusion

The dmesg command is a powerful tool to debug system-related issues. It prints the log messages stored in the kernel ring buffer. This tool also allows manipulation of the output to extract the required information only.

This tutorial covered the dmesg command, its syntax, and its usage in Linux through various examples.