Home / Blog / How to Use the dmesg Command on Linux

How to Use the dmesg Command on Linux

A stylized terminal prompt on a Linux laptop.
Fatmawati Achmad Zaenuri/Shutterstock

The dmesg command allows you to peer into the hidden world of the Linux startup processes. Evaluate and monitor {hardware} gadget and driver messages from the kernel’s personal ring buffer with “the fault finder’s good friend.”

How Linux’s Ring Buffer Works

In Linux and Unix-like computer systems, booting and startup are two distinct phases of the sequence of occasions that happen when the pc is powered on.

The boot processes (BIOS or UEFI, MBR, and GRUB) take the initialization of the system to the purpose the place the kernel is loaded into reminiscence and related to the preliminary ramdisk (initrd or initramfs), and systemd is began.

The startup processes then choose up the baton and full the initialization of the working system. Within the very early levels of initialization, logging daemons corresponding to syslogd or rsyslogd should not but up and operating. To keep away from shedding notable error messages and warnings from this part of initialization, the kernel incorporates a ring buffer that it makes use of as a message retailer.

A hoop buffer is a reminiscence house reserved for messages. It’s easy in design, and of a set measurement. When it’s full, newer messages overwrite the oldest messages. Conceptually it may be considered a “round buffer.”

The kernel ring buffer shops data such because the initialization messages of gadget drivers, messages from {hardware}, and messages from kernel modules. As a result of it incorporates these low-level startup messages, the ring buffer is an effective place to begin an investigation into {hardware} errors or different startup points.

However don’t go empty-handed. Take dmesg with you.

The dmesg Command

The dmesg command permits you to assessment the messages which might be saved within the ring buffer. By default, it’s essential use sudo to make use of dmesg.

sudo dmesg

sudo dmesg in a terminal window

All the messages within the ring buffer are displayed within the terminal window.

Output from sudo dmesg in a terminal window

That was a deluge. Clearly, what we have to do is pipe it by means of much less:

sudo dmesg | much less

sudo dmesg | less in a terminal window

Now we will scroll by means of the messages searching for objects of curiosity.

dmesg output in less in a terminal window

You need to use the search operate inside much less to find and spotlight objects and phrases you’re concerned with. Begin the search operate by urgent the ahead slash key “/” in much less.

RELATED: Tips on how to Use the much less Command on Linux

Eradicating the Want for sudo

If you wish to keep away from having to make use of sudo every time you utilize dmesg, you need to use this command. However, bear in mind: it lets anybody with a person account your laptop use dmesg with out having to make use of sudo.

sudo sysctl -w kernel.dmesg_restrict=0

sudo sysctl -w kernel.dmesg_restrict=0 in a terminal window

Forcing Colour Output

By default, dmesg will most likely be configured to provide coloured output. If it isn’t, you’ll be able to inform dmesg to colorize its output utilizing the -L (coloration)  choice.

sudo dmesg -L

sudo dmesg -L in a terminal window

To power dmesg to all the time default to a colorized show use this command:

sudo dmesg --color=all the time

sudo dmesg --color=always in a terminal window

Human Timestamps

By default, dmesg use a timestamp notation of seconds and nanoseconds because the kernel began. To have this rendered in a extra human-friendly format, use the -H (human) choice.

sudo dmesg -H

sudo dmesg -H in a terminal window

This causes two issues to occur.

output from sudo dmesg -H ina terminal window

  • The output is routinely displayed in much less.
  • The timestamps present a timestamp with the date and time, with a minute decision. The messages that occurred in every minute are labeled with the seconds and nanoseconds from the beginning of that minute.

Human Readable Timestamps

In case you don’t require nanosecond accuracy, however you do need timestamps which might be simpler to learn than the defaults, use the -T (human readable) choice. (It’s somewhat complicated. -H is the “human” choice, -T is the “human readable” choice.)

sudo dmesg -T

sudo dmesg -T in a terminal window

The timestamps are rendered as customary dates and instances, however the decision is lowered to a minute.

output from sudo dmesg -T in a terminal window

Every part that occurred inside a single minute has the identical timestamp. If all you’re bothered about is the sequence of occasions, that is adequate. Additionally, observe that you just’re dumped again on the command immediate. This selection doesn’t routinely invoke much less.

Watching Reside Occasions

To see messages as they arrive within the kernel ring buffer, use the --follow (look forward to messages) choice. That sentence may appear somewhat unusual. If the ring buffer is used to retailer messages from occasions that happen in the course of the startup sequence, how can dwell messages arrive within the ring buffer as soon as the pc is up and operating?

Something that causes a change within the {hardware} related to your laptop will trigger messages to be despatched to the kernel ring buffer. Replace or add a kernel module, and also you’ll see ring buffer messages about these modifications. In case you plug in a USB drive or join or disconnect a Bluetooth gadget, you’ll see messages within the dmesg output. Even digital {hardware} will trigger new messages to look within the ring buffer. Hearth up a digital machine, and also you’ll see new data arriving within the ring buffer.

sudo dmesg --follow

sudo dmesg --follow in a terminal window

Word that you’re not returned to the command immediate. When new messages seem they’re displayed by dmesg on the backside of the terminal window.

Output from sudo dmesg --follow n a terminal window

Even mounting a CD-ROM disk is seen as a change, since you’ve grafted the contents of the CD-ROM disk onto the listing tree.

dmesg ring buffer messages as a result of mounting a CD-ROM disk

To exit from the real-time feed, hit Ctrl+C.

Retrieve the Final Ten Messages

Use the tail command to retrieve the final ten kernel ring buffer messages. In fact, you’ll be able to retrieve any variety of messages. Ten is simply our instance.

sudo dmesg | final -10

sudo dmesg | last -10 in a terminal window

The final ten messages are retrieved and listed within the terminal window.

Output from sudo dmsesg | tail -10 in a terminal window

Looking out For Particular Phrases

Pipe the output from dmesg by means of grep to seek for explicit strings or patterns. Right here we’re utilizing the -i (ignore case) choice in order that the case of matching strings is disregarded. our outcomes will embrace “usb” and “USB” and another mixture of lowercase and uppercase.

sudo dmesg | grep -i usb

sudo dmesg | grep -i usb in a terminal window

The highlighted search outcomes are in uppercase and lowercase.

Search results showing uppercase and lowercase results in a terminal window

We are able to isolate the messages that comprise references to the primary SCSI exhausting disk on the system sda. (Really, sda can also be used these days for the primary SATA exhausting drive, and for USB drives.)

sudo dmesg | grep -i sda

sudo dmesg | grep -i sda in a terminal window

All the messages that point out sda are retrieved and listed within the terminal window.

output from sudo dmesg | grep -i sda in a terminal window

To make grep seek for a number of phrases directly, use the -E (lengthen common expression) choice. It’s essential to present the search phrases inside a quoted string with pipe “|” delimiters between the search phrases:

sudo dmesg | grep -E "reminiscence|tty|dma"

sudo dmesg | grep -E "memory|tty|dma" ina terminal window

Any message that mentions any of the search phrases is listed within the terminal window.

output from sudo dmesg | grep -E "memory|tty|dma" ina terminal window

Utilizing Log Ranges

Each message logged to the kernel ring buffer has a stage connected to it. The extent represents the significance of the data within the message. The degrees are:

  • emerg: System is unusable.
  • alert: Motion should be taken instantly.
  • crit: Crucial situations.
  • err: Error situations.
  • warn: Warning situations.
  • discover: Regular however vital situation.
  • data: Informational.
  • debug: Debug-level messages.

We are able to make dmesg extract messages that match a specific stage by utilizing the -l (stage) choice and passing the title of the extent as a command-line parameter. To see solely “informational” stage messages, use this command:

sudo dmesg -l data

sudo dmesg -l info in a terminal window

All the messages which might be listed are informational messages. They don’t comprise errors or warnings, simply helpful notifications.

Output from sudo dmesg -l info in a terminal window

Mix two or extra log ranges in a single command to retrieve messages of a number of log ranges:

sudo dmesg -l debug,discover

sudo dmesg -l debug,notice in a terminal window

The output from dmesg is a mix of messages of every log stage:

Output from sudo dmesg -l debug,notice in a terminal window

The Facility Classes

The dmesg messages are grouped into classes referred to as “amenities.” The checklist of amenities is:

  • kern: Kernel messages.
  • person: Person-level messages.
  • mail: Mail system.
  • daemon: System daemons.
  • auth: Safety/authorization messages.
  • syslog: Inner syslogd messages.
  • lpr: Line printer subsystem.
  • information: Community information subsystem.

We are able to ask dmesg to filter its output to solely present messages in a particular facility. To take action, we should use the -f (facility) choice:

sudo dmesg -f daemon

sudo dmesg -f daemon in a terminal window

dmesg lists all the messages referring to daemons within the terminal window.

output from sudo dmesg -f daemon in a terminal window

As we did with the degrees, we will ask dmesg to checklist messages from multiple facility directly:

sudo dmesg -f syslog, daemon

sudo dmesg -f syslog, daemon in a terminal window

The output is a mixture of syslog and daemon log messages.

output from sudo dmesg -f syslog, daemon in a terminal window

Combining Facility and Stage

The -x (decode) choice makes dmesg present the ability and stage as human-readable prefixes to every line.

sudo dmesg -x

sudo dmesg -x in a terminal window

The ability and stage might be seen at first of every line:

Output from sudo dmesg -x in a terminal window

The primary highlighted part is a message from the “kernel” facility with a stage of “discover.” The second highlighted part is a message from the “kernel” facility with a stage of “data.”

That’s Nice, However Why?

In a nutshell, fault discovering.

If you’re having points with a bit of {hardware} not being acknowledged or not behaving correctly, dmesg could throw some mild on the difficulty.

  • Use dmesg to assessment messages from the best stage down by means of every decrease stage, searching for any errors or warnings that point out the {hardware} merchandise, or could have a bearing on the difficulty.
  • Use dmesg to seek for any point out of the suitable facility to see whether or not they comprise any helpful data.
  • Pipe dmesg by means of grep and search for associated strings or identifiers corresponding to product producer or mannequin numbers.
  • Pipe dmesg by means of grep and search for generic phrases like “gpu” or “storage”, or phrases corresponding to “failure”, “failed” or “unable”.
  • Use the --follow choice and watch dmesg messages in real-time.

Glad looking.

About Dave McKay

Check Also

How to export your Apple Card monthly transactions as a CSV spreadsheet

One among Apple Card’s most important annoyances (at the least in comparison with many different …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.