Oct 21, 2023 4 min read

Dmesg Command in Linux

Use dmesg command in Linux with our step-by-step tutorial. The dmesg command is used to display the kernel ring buffer messages.

Dmesg Command in Linux
Table of Contents

Introduction

Before we discuss dmesg command in Linux, let's briefly understand-What is dmesg Command ?

The dmesg command in Linux is used to display the kernel ring buffer messages. It shows the messages generated by the kernel during the boot process and while the system is running. These messages can include important information about hardware, software, drivers, and system events. dmesg is a useful tool for troubleshooting and diagnosing Linux system issues.

In, Linux and other Unix-like operating systems, the dmesg command-line utility is used to print and control the kernel ring buffer.

It's important for debugging hardware-related issues and reviewing kernel boot messages.

You'll go through the basics of the dmesg command in this tutorial. We will also address a few FAQs on dmesg command in Linux.

Using the dmesg Command

The dmesg command has the following syntax:

dmesg [OPTIONS]

When a command is executed without any parameters, all messages from the kernel ring buffer are written to standard output by dmesg:

dmesg

The dmesg command is available to all users by default. Non-root users may, however, be denied access to dmesg on some systems. When you run dmesg in this case, you'll get an error message like this:

Output

dmesg: read kernel buffer failed: Operation not permitted

kernel.dmesg_restrict is a kernel parameter that specifies if the unprivileged users can use dmesg to check messages from the kernel's log buffer. Set it to zero to eliminate the restrictions:

sudo sysctl -w kernel.dmesg_restrict=0

Because the output usually contains many lines of data, only the last half of the output is shown. Pipe the output to a pager utility like less or more to see one page at a time:

dmesg --color=always | less

The --color=always option is used to ensure that the colorful output is preserved.

grep can be used to filter the buffer messages. For example, if you just want to see USB-related messages, type:

dmesg | grep -i usb

The messages generated by the kernel are read from the /proc/kmsg virtual file by dmesg. Only one process can open this file, which offers an interface to the kernel ring buffer. If you try to read a file with cat or less while the syslog process is active on your system, the program will hang.

Because the syslog daemon logs kernel messages to /var/log/dmesg, you can utilize it as well:

cat /var/log/dmesg

Formatting dmesg Output

The dmesg command has a number of formatting and filtering options to assist you format and filter the output.

The -H (--human) option, which provides human-readable output, is one of the most often used dmesg options. This option sends the output of the command to a pager:

dmesg -H

Use the -T (--ctime) option to print human-readable timestamps:

dmesg -T
Output

[Wed Feb 03 14:38:04 2019] IPv6: ADDRCONF(NETDEV_CHANGE): wlp1s0: link becomes ready

The --time-format <format> option can also be used to specify the timestamps format, which can be ctime, reltime, delta, notime, or iso. To use the delta format, for instance, type:

dmesg --time-format=delta

Combining two or more options is also an option:

dmesg -H -T

Use the -w (--follow) option to see the output of the dmesg command in real time:

dmesg --follow

Filtering dmesg Output

You can limit the output of dmesg to specific facilities and levels.

The facility reflects the message-creation process. The following log facilities are supported by dmesg:

  • kern - kernel messages
  • user - messages at the user level
  • mail - mail system
  • daemon - system daemons
  • auth - security/authorization messages
  • lpr - line printer subsystem
  • news - network news subsystem

You can limit the output to specified facilities using the -f(--facility <list>) option. One or more comma-separated facilities can be used with this option.

To see only the kernel and system daemons messages, for example, type:

dmesg -f kern,daemon

Each log message has a log level that indicates how important the message is. The following log levels are supported by dmesg:

  • emerg - the system is unusable
  • alert - quick action is required
  • crit - critical conditions
  • notice - typical but notable situation
  • err - error conditions
  • warn - warning conditions
  • debug - debug-level messages
  • info - informational

The -l (--level <list>) option limits the output to a specific set of levels. One or more comma-separated levels can be used with this option.

Only the error and critical warnings are displayed with the following command:

dmesg -l err,crit

Clearing the Ring Buffer

You can clear the ring buffer with the -C (--clear) option:

sudo dmesg -C

The buffer can only be cleared by root or users with sudo rights.

Use the -c (--clear) option to print the buffer contents before clearing it:

sudo dmesg -c

Redirect the output to a file if you want to save the current dmesg logs before removing them:

dmesg > dmesg_messages

FAQs on Dmesg Command in Linux

How does the dmesg command work?

When the system boots or during runtime, the kernel generates messages that are stored in a circular buffer called the kernel ring buffer. The dmesg command retrieves and displays these messages.

How do I use the dmesg command to view kernel messages?

Simply execute the dmesg command without any arguments in a terminal. It will display the kernel messages starting from the most recent.

Can I filter dmesg output to display specific messages?

Yes, you can pipe the output of dmesg to other commands, like grep to filter for specific messages. For example, dmesg | grep "error" will display only the kernel messages that contain the word "error".

Can I view earlier kernel messages that are not visible in the current dmesg output?

Yes, you can use the -T or --ctime option with dmesg to display the timestamp of each message, allowing you to compare and find earlier kernel messages.

Can dmesg capture messages from previous system boots?

By default, dmesg shows the messages from the current system boot. However, if the system is configured accordingly, you can use the -r or --read-clear option to read messages from previous boots.

Can dmesg help with diagnosing hardware or driver-related issues?

Yes, dmesg can provide valuable information for troubleshooting hardware or driver-related problems. It can indicate errors, failures, or unexpected behavior related to hardware devices, driver loading, and interaction between the operating system and hardware components.

Can I save dmesg output to a file for future reference?

Yes, you can save the output of dmesg to a file by redirecting it. For example, dmesg > my_dmesg_output.txt will save the dmesg output into the file "my_dmesg_output.txt".

Conclusion

You can examine and control the kernel ring buffer with the dmesg command. When debugging kernel or hardware issues, it can be really valuable.

In your terminal, type man dmesg to see a list of all available dmesg options.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.