What it is
dmesg displays the kernel ring buffer messages, useful for diagnosing hardware issues, driver problems, and system startup events.
Installation
dmesg is a standard utility and is available on virtually all Linux and macOS systems. It typically comes pre-installed with the kernel.
Commands / Usage
Viewing Messages
-
dmesgDisplay all kernel ring buffer messages. -
dmesg -HDisplay kernel ring buffer messages with a human-readable timestamp and colorized output. -
dmesg -TDisplay kernel ring buffer messages with human-readable timestamps. -
dmesg -kDisplay only kernel messages (default behavior). -
dmesg -l crit,err,warnDisplay kernel messages filtered by severity level (critical, error, warning). -
dmesg -f raw,userDisplay kernel messages filtered by facility (raw, user). -
dmesg -xDecode facility and level codes to human-readable strings.
Controlling Output
-
dmesg -n 8Set the console logging level to 8 (all messages). This affects future kernel messages, not those already in the buffer. -
dmesg -f 1Set the console logging facility to 1 (kernel).
Clearing the Buffer
sudo dmesg -cClear the kernel ring buffer after printing its contents. Use with caution, as this is a destructive operation. This is often used after a reboot to see only the messages from the current boot.
Following Messages
dmesg -wWatch for new kernel messages in real-time. Similar totail -f /var/log/kern.log.
Filtering and Searching
-
dmesg | grep -i "usb"Filterdmesgoutput to show lines containing "usb" (case-insensitive). -
dmesg | grep "error"Filterdmesgoutput to show lines containing "error". -
dmesg -l errDisplay only error messages. -
dmesg -eDisplay messages with extended information (e.g., CPU, process ID).
Timestamp Control
-
dmesg --time-format isoDisplay timestamps in ISO 8601 format. -
dmesg --time-format deltaDisplay timestamps as a delta from the earliest message.
Common Patterns
Checking for USB device connection issues:
dmesg | grep -i "usb"
This is a quick way to see if the kernel detected a USB device being plugged in and if there were any errors during its enumeration.
Investigating boot-time hardware problems:
sudo dmesg -c
After a reboot, run this command. It will print all messages from the current boot session and then clear the buffer. This helps isolate issues that occurred specifically during startup.
Real-time monitoring for kernel events:
dmesg -w
Leave this running in a terminal to see kernel messages as they happen. Useful for debugging intermittent issues or observing system behavior.
Finding specific hardware driver messages:
dmesg | grep -i "snd" # For sound card drivers
dmesg | grep -i "nvme" # For NVMe SSDs
dmesg | grep -i "eth" # For Ethernet network interfaces
Replace "snd", "nvme", or "eth" with the relevant driver or hardware identifier you suspect is causing problems.
Examining messages with human-readable timestamps:
dmesg -HT
This provides a more understandable view of when kernel events occurred, especially when correlating with other system logs.
Gotchas
- Permissions: On some systems, you might need
sudoto view certain kernel messages or to clear the buffer (sudo dmesg -c). - Clearing Buffer:
sudo dmesg -cdiscards the messages from the buffer. If you need to analyze them later, view them before clearing. - Buffer Size: The kernel ring buffer has a finite size. Older messages will be overwritten by newer ones. If you’re debugging a long-running issue, the relevant messages might have already been lost.
dmesg -wvs.tail -f /var/log/kern.log: Whiledmesg -wshows live kernel messages, traditional log files like/var/log/kern.log(or/var/log/messageson some systems) often contain more context, historical data, and messages from userspace daemons related to kernel events.dmesgis primarily for the kernel’s internal buffer.- Timestamp Interpretation: Timestamps can be relative to boot time or absolute human-readable times depending on the flags used (
-T,-H,--time-format). Be aware of which format you are seeing.