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 −

Flags Options Description
-C --clear It clears the ring buffer
-c --read-clear It displays the content of the buffer and then clears it
-D --console-off It disables printing the messages to the console
-d --show-delta It shows the timestamp and delta time spent between messages
-E --console-on It enables the printing the messages to the console
-e --reltime It displays the local time and delta in human-readable format (time conversion can be inaccurate)
-F file --file file It reads the system log messages from the specified file (it does not support kmsg format)
-f list --facility list To list specific facilities from the output (e.g. –facility=daemon)
-H --human It displays the output in human-readable format
-J --json It displays the output in JSON format
-K file --kmsg-file file It reads the /dev/kmsg messages from a file
-L when --color=when It colorizes the output (auto, never, always)
-l list --level list To list specific levels from the output e.g. –facility=err, warn)
-n level --console-level To print messages of the specific level only
--noescape To disable escaping the unprintable or unsafe characters
-P --no-pager It disables the pagination of the output
-r --raw It prints the raw output without stripping the log-level prefixes (unprintable data still escapes)
-S --syslog It forces the command to read the syslog instead of /dev/kmsg
-T --ctime It prints the human-readable timestamps (which can be inaccurate)
--since time It displays the messages since the specified time
--until time It displays the messages till the specified time
-t --no-time It does not display the timestamp with kernel messages
--time-format It prints the timestamps according to the given format
-u --userspace It displays the user space messages
-w --follow It waits for the new messages (from kernel version 3.5.0 and above)
-W --follow-new It waits and displays only new messages
-x --decode It decodes facility and level numbers to human-readable prefixes
--help It prints help about command
--version It 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 −

Level Numerical Value Description
emerg 0 It indicates the emergency state which means that the system is unstable (for example: hardware failure)
alert 1 It indicates the alerts, which means an immediate action is required (for example: data corruption)
crit 2 It indicates the critical state that can affect the system (for example: out of memory errors)
err 3 It indicates the error state that could disrupt normal operations (for example: connection failures)
warn 4 It indicates the potential issue but does not require immediate action (for example: low storage)
notice 5 It indicates normal operation but is significant enough to require attention (example: user logged in)
info 6 It shows the general information message about normal operations
debug 7 It 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 −

Facility Description
kern It shows the logs generated by the kernel
user It shows the logs generated by the user
mail It shows the logs generated by the mail system
daemon It shows the logs generated by the system daemon
auth It shows security and authorization-related logs
syslog It shows the logs generated by the internal syslog
lpr It shows line printer-related logs
news It 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.

Advertisements