Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Packages and Services

Linux dmesg command

Reference for the Linux dmesg command: read kernel messages from the ring buffer, filter by level, show readable timestamps, follow new output and fix permission errors.

dmesg prints the kernel's ring buffer: a fixed-size memory area where the kernel stores its own log messages. It records what happens at boot (hardware detection, drivers, filesystems mounted) and later events such as USB devices plugged in, disk errors, network link changes, and the out-of-memory killer ending a process.

When something hardware- or driver-related goes wrong, dmesg is one of the first places to look. The buffer has a limited size, so the oldest lines are overwritten on a busy system; for permanent history use journalctl -k.

Terminal
dmesg | tail -20
sudo dmesg --level=err,warn
dmesg -T | grep -i usb

Try it

Reading the kernel buffer is often restricted in containers and sandboxes, so the first example reports what happens here. The second example filters sample kernel lines with the same patterns you would use on a real system.

Bash Try reading the buffer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Filtering kernel messages (sample data)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
dmesg [OPTION]...
Option Description
-T, --ctime Human-readable timestamps (approximate: based on boot time).
-H, --human Readable output with colors, relative times and a pager.
-l LIST, --level=LIST Only these levels: emerg, alert, crit, err, warn, notice, info, debug.
-f LIST, --facility=LIST Only these facilities (kern, user, daemon, ...).
-w, --follow Keep running and print new messages as they appear.
-W, --follow-new Like -w but skip existing lines.
-c, --read-clear Print, then clear the buffer (root).
-C, --clear Clear without printing (root).
-n LEVEL Set the console log level (root).
-x, --decode Show facility and level as text.
-k Kernel messages only.

Returns: 0 on success; non-zero if the buffer cannot be read, for example "Operation not permitted".

Usage notes

  • Permission denied: many distributions set kernel.dmesg_restrict=1, so ordinary users need sudo or membership of a log group. Check with sysctl kernel.dmesg_restrict.
  • Timestamps are seconds since boot. -T converts them to wall-clock time but can be wrong after suspend or long uptime, because the clock the kernel counts with pauses during sleep.
  • The buffer is finite (set at boot, often 256 KiB or more). After a crash or reboot it is gone; journalctl -k -b -1 shows the previous boot if the journal is persistent.
  • Typical searches: dmesg | grep -i -E 'oom|killed process' for memory kills; grep -i 'I/O error' for failing disks; grep -i firmware for missing driver files.
  • Following events: run dmesg -w in one terminal, then plug in a device in another; the new lines tell you which device name it received.
  • Clearing the buffer hides evidence. Avoid -c on a machine you are troubleshooting.
  • Containers usually share the host's kernel, but container runtimes often block dmesg for safety.
  • Levels are numbers 0 (emerg) to 7 (debug); only messages at or above the console level are printed to the screen, but all of them are stored in the buffer.