Mastering the Linux dmesg Command: A Beginner’s Guide with 5 Practical Examples

The Linux kernel loads several device drivers when the system boots. Even after your system is up and running, connecting a hardware device will prompt the kernel to load the corresponding driver. Additionally, the kernel performs numerous crucial tasks. So, how do you gain insight into these kernel activities?

Enter the dmesg command—a handy utility for accessing kernel-generated messages. This guide will help you understand how the dmesg tool works through simple, illustrative examples.

Understanding the Linux dmesg Command

The dmesg command allows you to print or manage the kernel ring buffer. Its basic syntax is:

dmesg [options]

According to the tool’s manual page:

dmesg is used to examine or control the kernel ring buffer. The default action is to display all messages from the kernel ring buffer.

Below are some Q&A examples to clarify how the dmesg command functions.

Q1: How to Use the dmesg Command?

You can start using dmesg without any command-line options as shown:

dmesg

An example output from the command:

How to use dmesg command

Q2: How to Limit the Output to Errors and Warnings?

The output of dmesg can be extensive. Depending on your needs, filtering the output can be beneficial. This can be achieved using ‘levels,’ which are defined as:

    emerg - system is unusable
    alert - action must be taken immediately
    crit  - critical conditions
    err   - error conditions
    warn  - warning conditions
    notice - normal but significant condition
    info  - informational
    debug - debug-level messages

To filter the output to only errors and warnings, use:

dmesg --level=err,warn

Here’s a sample output from using the above command:

How to limit the output only to error and warnings

Q3: How to Add Timestamps to dmesg Output?

To associate timestamps with dmesg messages, utilize the -T option for human-readable timestamps:

dmesg -T

A sample output is as follows:

How to make dmesg produce timestamps in output

Note the prefixed timestamps with each message.

Q4: How to Display Device-Specific Information using dmesg?

To display information related specifically to the eth0 interface, use the following command:

dmesg | grep -i eth0

An example output is shown below:

How to make dmesg display info specific to a device

Q5: How to Display Only Userspace Messages?

If you wish to focus solely on userspace messages from dmesg, use the -u option:

dmesg -u

Below is a sample output:

How to make dmesg display only userspace messages

Conclusion

While not used daily, dmesg is invaluable when troubleshooting or when you’re asked to provide kernel messages. This often occurs in online forums, where experienced users might request this data for deeper insights.

This tutorial offers a beginner-friendly overview of dmesg. Once familiar, explore its detailed functionalities in the official man page.

Frequently Asked Questions

  • What does the dmesg command do?The dmesg command displays messages from the kernel ring buffer, useful for diagnosing system issues.
  • Can I filter output from dmesg?Yes, you can use the --level option to filter by message severity such as errors and warnings.
  • How do I include timestamps in dmesg output?Use the -T command line option to include human-readable timestamps in the output.
  • Is dmesg data persistent?No, the dmesg buffer is temporary and is cleared upon reboot.
  • Where can I find more information on using dmesg?Refer to the dmesg man page for more detailed information.