Understanding the Use of 'dmesg' Command (with examples)
- Linux
- December 17, 2024
The ‘dmesg’ command is a utility on Unix and Unix-like operating systems that retrieves and displays messages from the kernel ring buffer. These messages typically contain information relevant to hardware components, kernel activities, and system drivers. “dmesg” stands for “diagnose message” and it is indispensable for diagnosing hardware or kernel issues, monitoring system performance, or understanding the system state during bootup. Below are various use cases that illustrate different functionalities of the “dmesg” command.
Show Kernel Messages
Code:
sudo dmesg
Motivation:
The primary use of the dmesg
command is to display messages from the kernel ring buffer. Using sudo dmesg
, you can review these messages in their entirety, which is particularly useful for system administrators or developers who need to troubleshoot the operating system. The command reveals initialization messages, hardware configuration settings, and logs any driver or kernel issues, offering a window into the system’s state from boot time onward.
Explanation:
sudo
: Runningdmesg
with superuser privileges ensures access to all messages in the kernel buffer, as some messages might be restricted to root due to sensitive information they contain.dmesg
: This fetches and prints the messages from the kernel ring buffer to the standard output.
Example Output:
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
...
[ 12.345678] Bluetooth: Core ver 2.22
Show Kernel Error Messages
Code:
sudo dmesg --level err
Motivation:
Viewing only error messages can focus diagnostic efforts by filtering out irrelevant messages. This is efficient if you are only interested in identifying and resolving critical issues affecting system performance or stability. It is important for maintenance tasks and for preventing small issues from escalating into more severe problems.
Explanation:
--level err
: This flag filters and displays only the messages that have an “error” severity level, making it easier to spot and resolve errors amongst a sea of other kernel messages.
Example Output:
[ 15.123456] EXT4-fs (sda1): re-mounted. Opts: errors=remount-ro
Show Kernel Messages Continuously
Code:
sudo dmesg -w
Motivation:
Sometimes, continuous monitoring of kernel messages is necessary for real-time troubleshooting or diagnostics, such as when testing drivers or hardware. By using -w
, you can view messages as they come, without needing to rerun the command, similar to how you would use tail -f
for continuously monitoring a log file.
Explanation:
-w
: This option follows the messages in the kernel buffer, displaying new messages as they are appended, providing a continuously updated stream of information.
Example Output:
[ 1503.234567] usb 2-1: USB disconnect, device number 4
[ 1503.678910] usb 1-2: new high-speed USB device number 6 using xhci_hcd
Show Physical Memory Information
Code:
sudo dmesg | grep -i memory
Motivation:
Understanding how much physical memory is available or recognized by the system is essential for diagnosing hardware issues or optimizing system performance. By searching kernel messages for memory-related entries, one can acquire detailed insights into the memory configuration upon system startup.
Explanation:
|
: This pipes the output ofdmesg
to another command for further processing.grep -i memory
: This filters thedmesg
output to find lines containing the word “memory”, ignoring case differences due to-i
, providing a focused view into memory-specific messages.
Example Output:
[ 0.000000] Memory: 7923412K/8372224K available
Show Kernel Messages Page by Page
Code:
sudo dmesg | less
Motivation:
The less
command allows the user to scroll through terminal output one page at a time. When examining lengthy kernel logs, this approach is more manageable, facilitating easier navigation through the messages without overwhelming the terminal with continuous output.
Explanation:
|
: This symbol pipes the output ofdmesg
to another command.less
: A pager program invoked here to allow incremental reading of the log, providing an interface for scrolling back and forth through the output.
Example Output:
[ 0.000000] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-4.15.0-45-generic root=UUID=66c2c7dc-d969-4c8b-a4b5-d5c32da6e27c ro quiet splash
...
(END)
Show Kernel Messages with Timestamp
Code:
sudo dmesg -T
Motivation:
Kernel messages are often timestamped with the time since system boot, which can be cryptic. The -T
option translates these timestamps into human-readable date and time formats. This aids in correlating kernel events with real-world, chronological events.
Explanation:
-T
: This flag converts the relative timestamp into an absolute time, making it easier for users to understand when specific kernel events occurred.
Example Output:
[Mon Sep 20 15:42:43 2023] Initializing cgroup subsys cpuset
Show Human-readable Kernel Messages
Code:
sudo dmesg -H
Motivation:
Rendering kernel messages in a human-readable format improves clarity and understanding. The -H
option applies markup that highlights and colorizes kernel messages, simplifying the process of finding specific log entries, which is beneficial during troubleshooting.
Explanation:
-H
: This option enhances readability by colorizing and pager-marking the output. It makes it easier to locate and decipher information.
Example Output:
[Tue Sep 21 10:23:45 2023] xhci_hcd 0000:00:14.0: new USB bus registered, assigned bus number 1
Colorize Kernel Message Output
Code:
sudo dmesg -L
Motivation:
Colorized output can increase readability by helping users quickly identify different levels or types of messages, such as errors, warnings, or informational messages. This visual distinction is especially helpful when parsing through large volumes of logs for specific patterns.
Explanation:
-L
: This flag enables coloration of the output based on message type, improving the user’s ability to visually parse and interpret the log.
Example Output:
\033[31m[ 8.123456] Error: Device not found\033[0m
\033[32m[ 8.789012] System resource allocated\033[0m
Conclusion
The ‘dmesg’ command offers various functionalities that facilitate system diagnostics and monitoring by granting insight into kernel activities and system events. Through filtering, formatting, and continuous updating options, ‘dmesg’ supports both proactive and reactive system management efforts, proving to be a versatile tool for system administrators and developers alike.