Understanding the `dmesg` Command (with examples)
- Sunos
- December 17, 2024
The dmesg
command is a powerful tool for system administrators and developers alike. It stands for “diagnostic message” and is used to examine or control the kernel ring buffer. Essentially, dmesg
fetches and displays messages generated by the kernel, which are essential for diagnosing system behavior, detecting hardware issues, and ensuring overall system health.
Use case 1: Show kernel messages
Code:
dmesg
Motivation:
The primary motivation for using this command is to obtain a comprehensive view of the kernel messages in their entirety. These messages, often pertaining to hardware activities, drivers, and system boot sequences, can be critical for system diagnostics and troubleshooting. When something goes wrong or when investigating how the system operates on a deeper level, having access to the raw kernel messages can provide invaluable insights.
Explanation:
- The
dmesg
command by itself, without any arguments, outputs all the messages in the kernel ring buffer directly to the terminal. This includes information collected from the system start-up to the current session. It’s a straightforward way to get a complete dump of kernel activities.
Example output:
[ 0.000000] Linux version 5.11.0-27-generic (buildd@lgw01-amd64-019)...
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.11.0-27-generic ...
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
...
Use case 2: Show how much physical memory is available on this system
Code:
dmesg | grep -i memory
Motivation:
Understanding how much physical memory is available on a system is essential for system optimization and resource allocation. This command can be particularly useful when configuring or optimizing applications that are memory-intensive or when troubleshooting performance issues. By filtering the kernel messages, you can directly pinpoint memory-related information without needing to sift through unrelated entries.
Explanation:
dmesg
: Outputs all kernel messages tostdout
.|
: The pipe operator is used to redirect the output ofdmesg
to the next command.grep
: A command-line utility used to search plain-text data sets for lines matching a regular expression.-i
: This argument tellsgrep
to ignore case sensitivity, meaning it will match “memory”, “Memory”, “MEMORY”, etc.memory
: This is the search patterngrep
uses to filter messages containing the word “memory”.
Example output:
[ 0.000000] Memory: 16318792K total, 15873164K used, 445628K free...
[ 0.000000] Freeing initrd memory: 20064K
[ 0.000000] NR_IRQS:4352 nr_irqs:256 0
Use case 3: Show kernel messages 1 page at a time
Code:
dmesg | less
Motivation:
Sometimes kernel messages may be too voluminous to absorb all at once, especially when troubleshooting a long-running system or diagnosing complex issues. Viewing the messages one page at a time allows for more detailed scrutiny and analysis. This method avoids screen overflow, making it easier to search through or read logs at your pace.
Explanation:
dmesg
: Outputs the full set of kernel messages.|
: Directs this output to the next command.less
: A terminal pager program that allows users to view the content of a file one screenful at a time. Unlikemore
,less
allows backward navigation in the file as well as forward movement.
Example output:
[ 0.110663] ACPI: Added _OSI(Linux-Manufacturer)
[ 0.115120] ACPI: [Firmware Bug]: BIOS _OSI(Linux) query ignored
...
--More--
Here, you would navigate through the output using arrow keys
, and exit with the q
key.
Conclusion:
The dmesg
command, as demonstrated in the use cases above, is an invaluable utility for uncovering the inner workings of a Linux-based system. From analyzing real-time kernel messages to filtering specific information like memory availability, and even controlling how one interacts with the data, dmesg
serves as an essential toolset for developers and system administrators dedicated to maintaining optimal system performance and stability.