Site icon DesignLinux

Dmesg Command in Linux

Dmesg Command in Linux

The dmesg command-line utility is used to view 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. In this guide, we will show you the basics of the dmesg command.

In the Linux operating system the Linux kernel is the core that manages the system resources. 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.

Using the dmesg Command

The basic syntax for the dmesg command is as following:

dmesg [OPTIONS]

If you run the dmesg without any options it will writes all messages from the kernel ring buffer to the standard output:

dmesg

In some of systems the root level users only can run the dmesg command and in some of system it allows users to run dmesg command. If the user have not permission then following error message will be displayed:

dmesg: read kernel buffer failed: Operation not permitted

You also can manage the restriction that the non-root user can use the dmesg command or not by kernel parameter kernel.dmesg_restrict. You should set it zero to remove the restrictions:

sudo sysctl -w kernel.dmesg_restrict=0

There will many lines in output so the last few lines are viewable on terminal. You can pipe the output with less utility:

dmesg --color=always | less

The --color=always is used to preserve the colored output.

You also can use the grep command to filter the output. For example, to view only the USB related messages, you would type:

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. If syslog process is running on your system and you try to read the file with cat , or less, the command will hang.

The syslog daemon dumps kernel messages to /var/log/dmesg, so you can also use that log file:

cat /var/log/dmesg

Formating dmesg Output

As we seen previously the output of dmesg have the lots of lines. The dmesg command provides number of options that help to format and filter the output.

The -H (--human) is the commonly used option, which show the human readable output.

dmesg -H

To print human-readable timestamps use the -T (--ctime) option:

dmesg -T
[Fri Sep 4 10:20:04 2020] IPv6: ADDRCONF(NETDEV_CHANGE): wlp1s0: link becomes ready

The timestamps format can also be set using the –time-format option, which can be ctime, reltime, delta, notime, or iso. For example to use the delta format you would type:

You also can set the timestamps using the --time-format option. To use delta format, type:

dmesg --time-format=delta

You also can use the two options simultaneously:

dmesg -H -T

To watch the output of the dmesg command in real-time use the -w (--follow) option:

dmesg --follow

Filtering dmesg Output

The dmesg supports the following log facilities:

Using the -f (--facility ) option you can limit the output to specific facilities. The option accepts one or more comma-separated facilities.

For example, to display only the kernel and system daemons messages you would use:

dmesg -f kern,daemon

Each log message is associated with a log level that shows the importance of the message. dmesg supports the following log levels:

The -l (--level ) option restricts the output to defined levels. The option accepts one or more comma-separated levels.

The following command displays only the error and critical messages:

dmesg -l err,crit

Clearing the Ring Buffer

Option -C (--clear) allows you to clear the ring buffer:

sudo dmesg -C

Make sure to clear the ring buffer you should logged in as root or user with sudo privileges.

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

sudo dmesg -c

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

dmesg > dmesg_messages

Conclusion

The dmesg command allows you to view and control the kernel ring buffer. Type man dmesg in your terminal for information about all available dmesg options.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version