The Linux kernel, like other programs, can and does display various informational and error messages. All of them are output to the kernel message buffer, the so-called kernel ring buffer. The main reason for the existence of this buffer is to store messages that occur during system boot while the Syslog service is not yet running and cannot collect them.

To get messages from this buffer, you can simply read the file /var/log/dmesg. However, it is more convenient to do this with the dmesg command. In this article, we will look at how to use dmesg. Let’s deal with the options of the utility, as well as give examples of working with it.

Syntax and options for dmesg

The syntax for the dmesg command is very simple. You need to type the name of the command and, if necessary, the options:

$ dmesg options

Options allow you to control the output, add or hide additional information, and make viewing messages more convenient. Here they are:

  • -C, –clear – clear the kernel message buffer;
  • -c, –read-clear – display messages from the kernel buffer and then clear it;
  • -d, –show-delta – displays the elapsed time between two messages;
  • -f, –facility – display only messages from subsystems of a certain category;
  • -H, –human – enable human-friendly output;
  • -k, –kernel – display kernel messages only;
  • -L, –color – make the output color, automatically enabled when using the -H option ;
  • -l, –level – limit the output to the specified verbosity level;
  • -P, –nopager – display information in plain text, do not add pagination;
  • -r, –raw – print messages as is, without removing service prefixes;
  • -S, –syslog – use Syslog to read messages from the kernel, by default the /dev/kmsg file is used ;
  • -T, –ctime – display time in human-friendly format;
  • -t, –notime – do not display the time when the message arrived;
  • -u, –userspace – show only messages from programs from user space;
  • -w, –follow – after displaying all messages, do not terminate the program, but wait for new ones;
  • -x, –decode – print category and log level in human readable format.

These are not all options, but only the most interesting ones. If you want to see others, use the following command:

man dmesg

Supported logging categories:

  • kern – messages from the kernel;
  • user – messages from user-space programs;
  • mail – messages from mail services;
  • daemon – messages from system services;
  • auth – security messages and user authorization information;
  • syslog – messages sent by the Syslogd service;
  • lpr – messages from print services.

And here are the available logging levels:

  • emerg – an error caused the system to fail;
  • alert – user intervention required;
  • crit – critical error;
  • err – common error;
  • warn – warning;
  • notine – remark;
  • info – information;
  • debug – debug message.

Examples of using dmesg

As you already understood, the dmesg command shows not only messages from the kernel, but also other messages from system services, for example, from the systemd init system. To display all messages in general, run the command without options:

dmesg

How to use dmesg in Linux

By default, only the time offset from system loading is displayed before the message. If you want the full timestamp, use the -T option :

dmesg -T

How to use dmesg in Linux

At boot time, the kernel tests the hardware and prints detailed information about it to the kernel message buffer. Thus, using dmesg you can see information about the equipment, just filter the necessary data, for example, CPU:

dmesg | grep CPU

How to use dmesg in Linux

To display only messages from the kernel, and not everything in a row, use the -k option:

dmesg -k

Now there will be no messages from Systemd in the output of the utility. If you want to print all kernel messages in real time, use the -w option:

dmesg -w

How to use dmesg in Linux

To add color to the output and enable pagination instead of displaying text in one big chunk, use the -H option:

dmesg -H

How to use dmesg in Linux

To display the category of a message and its logging level, use the -x option:

dmesg -x

How to use dmesg in Linux

If you are only interested in errors, you can filter them out by logging level:

dmesg -l crit,err

Or just informational messages:

dmesg -l warn

How to use dmesg in Linux

You can only output messages that came here from user space, to do this, use the -u option :

dmesg -u

How to use dmesg in Linux

Alternatively, filter out the user and daemon categories with the -f option :

dmesg -f user,daemon

You can filter other categories in the same way. If you want to clear the kernel message buffer, use the -C option. The next time you run dmesg, it will be empty:

dmesg -C

How to use dmesg in Linux

Findings

In this short article, we have covered how to use dmesg. Now you know what this utility is responsible for, as well as what options you can use.

Source: https://losst.ru/. The article is distributed under the CC-BY-SA license