dmesg: An old, new, cool tool

A few months back I took the time to learn about top, and how to run it efficiently, and felt quite the wiser for it.

It’s time to investigate another core tool and learn to use it efficiently, and this time it’s dmesg. It’s probably safe to say that in most distros, dmesg is part of the util-linux suite. (Edit, 4 p.m.: This is from Arch Linux.)

But it’s also probably safe to say that dmesg is boring. Enter just dmesg at the terminal, and you get an immeasurable list of junk that either helps you solve your problem, or clutters up the landscape.

There are cleverer ways to use dmesg though, and not just by grepping it.

For example, you know I like color. :mrgreen: Try dmesg -L

2013-08-07-v5-122p-dmesg-01

Ah, now that is much easier to pick apart. It’s still a mangled mess of data, but it’s in color, at least. 😉

Another thing: It would be nice if the times it showed were calculated against startup, and not just total time running. I want to see differences in times, rather than just arbitrary start points.

Information like that would help me understand what’s taking a long time, what’s holding up the process, and so forth.

dmesg can do that. Here’s dmesg --reltime

2013-08-07-v5-122p-dmesg-02

Of course both that and the colorized output are nifty, but still just spatter information across the screen, willy-nilly.

We could pipe dmesg through a pager like most, but wait … here’s dmesg -H

2013-08-07-v5-122p-dmesg-03

Skim to the top of the output and check the bold green stamped time. Everything after that should appear as fractional times added to it.

Now you can pinpoint sluggards.

I most often use dmesg to find error messages; that’s what it’s really useful for. Of course, I could grep the output for keywords, like NET for network errors or VGA for graphics knots.

And of course now that I know about the -L flag, it would be easy to just look for red messages. Those are errors, of course.

What would be even more efficient would be if dmesg could censor itself of everything but error messages.

You know what? Yep, you guessed it. Here’s dmesg -H -l err

2013-08-07-v5-122p-dmesg-04

Hey, whaddayaknow. I have a couple of errors in my system setup. As luck would have it.

There are other “message levels;” try dmesg -H -l notice too. Good for additional warnings or errors.

“So K.Mandla,” you say. “Just give me something sparkly I can dump into my terminal.”

Okay, let’s combine a few other nifty flags, all at once. Here’s dmesg -deH, which will display a dual time format, colorize, output times in a more understandable fashion, and employ the pager if necessary.

2013-08-07-v5-122p-dmesg-05

Sparkly enough for you? 😉

We talked about levels; dmesg also filters by “facility.” Here’s dmesg -deH -f daemon

2013-08-07-v5-122p-dmesg-06

systemd, front and center. 🙂

Finally, two flags I’ll mention, but won’t show in use, are dmesg -c and dmesg -C .

They’re not terribly dangerous, but they will clear out all the information thus far collected in your kernel ring buffer, which means everything after that will be empty output — until the kernel stashes another message for your perusal.

I can see the function in that though, since error messages occuring after boot might be easier found without stringing through all the past information. dmesg output would be only the latest and greatest.

So there you have it. Use dmesg wisely and it will give you exactly what you want, in a format you can understand, in a split second.

Use it unwisely, and you’ll be wasting your precious time, straining through unwanted information. … 👿

Edit, 4 p.m.: Seems there are some inconsistencies between distros. I’ll double check what Ubuntu/Mint can or can’t do. Just for the record, this is with Arch Linux, util-linux 2.32.2 for x86_64. I am not sure why there should be such strong differences between this and some others. My apologies if the flags don’t work in your version. That rather defeats the purpose of this little essay, now doesn’t it … ? 😦

Edit, 4:15 p.m. It appears the Ubuntu versions are at 2.20.1 for Raring and Saucy. Debian Wheezy is at 2.20.1 as well. The reltime option appears in 2.22, and the color and “human-readable” flags are in 2.23. Again, sorry about the version discrepancies. Usually I work with old, abandoned software, so this really never comes into play. … 😳

Edit, 5:15 p.m.: I did a little checking beyond just release notes, and from a Linux Mint x86_64 live environment, I was able to use the -deH flags with a binary of dmesg extracted from the util-linux package downloaded from the Arch repositories. Barbaric and uncivilized, yes, but what that means is you’re likely to be able to use version 2.23 or later of dmesg with earlier versions of util-linux. Just rip out the binary and transplant it somewhere onto your system. The usual caveats apply, however: Don’t hold me responsible if your machine crashes, if dmesg freaks out and retreats into your bathroom to cry for three hours, or if hackers steal your credit card number. There’s only so much I can do, folks. …

12 thoughts on “dmesg: An old, new, cool tool

  1. Pingback: Useful dmesg commands | Useful snippets

  2. yanetut

    Mine doesn’t have -L, -H, -reltime, or -e
    From the current Ubuntu, 13.04, x86_64
    dmesg from util-linux 2.20.1

    1. K.Mandla Post author

      Yes, I’m trying to sift through some live versions of other distros, and see which ones carry the flags here. This is Arch, by the way, which is util-linux 2.23.2, for x86_64.

  3. Henk Slaaf

    On Debian wheezy, most commands do not work or have other switches:

    Options:
    -C, –clear clear the kernel ring buffer
    -c, –read-clear read and clear all messages
    -D, –console-off disable printing messages to console
    -d, –show-delta show time delta between printed messages
    -E, –console-on enable printing messages to console
    -f, –facility restrict output to defined facilities
    -h, –help display this help and exit
    -k, –kernel display kernel messages
    -l, –level restrict output to defined levels
    -n, –console-level set level of messages printed to console
    -r, –raw print the raw message buffer
    -s, –buffer-size buffer size to query the kernel ring buffer
    -T, –ctime show human readable timestamp (could be
    inaccurate if you have used SUSPEND/RESUME)
    -t, –notime don’t print messages timestamp
    -u, –userspace display userspace messages
    -V, –version output version information and exit
    -x, –decode decode facility and level to readable string

    1. K.Mandla Post author

      Are the man pages different then? I wonder if there are differences between Arch x86_64’s util-linux 2.23.2 and Wheezy’s version.

      Edit, 4:25 p.m.: I tracked the version differences through the release notes of each version. I have links at the end of the post. Sorry about the confusion.

  4. Col

    Which distro is this from? My dmesg (Debian testing, util-linux 2.20.1) doesn’t have -H, -L or –reltime (although it does have -d = show time delta between messages, which is nice).

    1. K.Mandla Post author

      Sorry about that. I should’ve mentioned Arch Linux higher up in the post. I’ve edited to be clearer. I also see that Debian Wheezy carries slightly different flags than what I have with util-linux 2.23.2 in Arch x86_64. My apologies. 😦

      1. Col

        Thanks for the response. No need to apologise! It’s just one of those things – no two linuxes are the same. My fault for settling on Debian.

  5. Pingback: dateutils: Making everything better | Inconsolation

  6. Pingback: episoder: Only if you’re serious about TV | Inconsolation

  7. Pingback: split: And the curse of the asinine defaults | Inconsolation

  8. Pingback: wget: And that which wget begot | Inconsolation

Comments are closed.