How to use the command "dmesg" (with examples)
- Osx
- December 25, 2023
The “dmesg” command is used to print and control the kernel ring buffer, which displays kernel messages. It enables users to view and analyze the log of important messages produced by the kernel during the system startup. The command can be used in a variety of scenarios to troubleshoot issues, perform debugging, and gather system information.
Use case 1: Show kernel messages
Code:
dmesg
Motivation: This use case is beneficial when you want to see the most recent kernel messages. It helps in determining any system-related events or errors that might have occurred during the system startup or while running processes.
Explanation:
dmesg
: Executes the command to display kernel messages on the standard output (stdout).
Example output:
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 5.4.0-91-generic (buildd@lgw01-amd64-069) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 (Ubuntu 5.4.0-91.102-generic 5.4.154)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-91-generic root=UUID=fd3f3d4c-09cc-40da-bf95-815f2953a7cf ro quiet splash vt.handoff=7
...
Use case 2: Show how much physical memory is available on this system
Code:
dmesg | grep -i memory
Motivation: When troubleshooting memory-related issues or simply gathering system information, it is essential to know the physical memory available on the system. This use case allows you to extract memory-related kernel messages from the log.
Explanation:
dmesg
: Executes the command to display kernel messages on the standard output (stdout).|
: Redirects the output of the previous command to the next command as input.grep -i memory
: Searches for lines containing the word “memory” in a case-insensitive manner.
Example output:
[ 0.000000] Memory: 8020296K/8291856K available (14346K kernel code, 2373K rwdata, 4112K rodata, 2688K init, 4840K bss, 271560K reserved, 0K cma-reserved)
[ 0.131739] Freeing SMP alternatives memory: 32K
[ 0.887510] initcall sonypi_init+0x0/0x1000 [sony_laptop] returned 0 after 0 usecs
...
Use case 3: Show kernel messages 1 page at a time
Code:
dmesg | less
Motivation: In situations where the kernel log is lengthy, it might be inconvenient to scroll through the entire log at once. By utilizing the “less” pager, you can read the kernel messages one page at a time, providing better readability and ease of navigation.
Explanation:
dmesg
: Executes the command to display kernel messages on the standard output (stdout).|
: Redirects the output of the previous command to the next command as input.less
: A pager that allows you to view text files in a paginated manner, where you can scroll forward and backward.
Example output:
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 5.4.0-91-generic (buildd@lgw01-amd64-069) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 (Ubuntu 5.4.0-91.102-generic 5.4.154)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-91-generic root=UUID=fd3f3d4c-09cc-40da-bf95-815f2953a7cf ro quiet splash vt.handoff=7
...
(END)
Conclusion:
The “dmesg” command is a powerful tool for inspecting kernel messages and gathering relevant system information. Whether you need to troubleshoot issues, analyze startup events, or investigate memory-related concerns, understanding the usage of “dmesg” is valuable. By following the examples provided, you can harness the command’s capabilities to enhance your system administration and troubleshooting skills.