Linux dmesg Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux dmesg command and how to use it to explore the kernel ring buffer. The dmesg command is a powerful tool for system monitoring and management, as it provides access to valuable kernel-level messages that can be used for troubleshooting and monitoring system health.

You will start by understanding the purpose and usage of the dmesg command, which allows you to view the contents of the kernel ring buffer. This circular buffer stores messages generated by the Linux kernel during system boot and runtime, including information about hardware initialization, driver loading, and system events. You will then explore various options to filter and analyze the dmesg output, enabling you to focus on specific types of messages or log levels.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/cat -.-> lab-422644{{"`Linux dmesg Command with Practical Examples`"}} linux/tail -.-> lab-422644{{"`Linux dmesg Command with Practical Examples`"}} linux/grep -.-> lab-422644{{"`Linux dmesg Command with Practical Examples`"}} linux/sudo -.-> lab-422644{{"`Linux dmesg Command with Practical Examples`"}} end

Understand the Purpose and Usage of the dmesg Command

In this step, you will learn about the purpose and usage of the dmesg command in Linux. The dmesg command is used to view the kernel ring buffer, which contains messages generated by the Linux kernel during system boot and runtime.

The kernel ring buffer is a circular buffer that stores kernel-level messages, including information about hardware initialization, driver loading, and system events. The dmesg command allows you to access and analyze these messages, which can be useful for troubleshooting system issues and monitoring system health.

To view the contents of the kernel ring buffer, run the following command:

sudo dmesg

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.15.0-58-generic (buildd@lgw01-amd64-054) (gcc-11) #64~20.04.1-Ubuntu SMP Thu Jan 5 12:11:15 UTC 2023 (Ubuntu 5.15.0-58.64~20.04.1-generic 5.15.52)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.15.0-58-generic root=UUID=0b1d7f41-a4a6-4c7e-9e2f-5d8d6d7d3b2e ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
[    0.000000]   Centaur CentaurHauls
[    0.000000]   zhaoxin   Shanghai

The output shows various kernel-level messages, including information about the kernel version, command-line parameters, and supported CPU architectures.

You can use various options with the dmesg command to filter and format the output, such as:

  • dmesg -T: Display timestamps in human-readable format.
  • dmesg -l <level>: Filter the output by log level (e.g., dmesg -l err to show only error messages).
  • dmesg -n <level>: Set the console log level, which determines the minimum level of messages that will be displayed on the console.
  • dmesg -w: Follow the kernel ring buffer in real-time, similar to tail -f.

Understanding the purpose and usage of the dmesg command is essential for system monitoring and troubleshooting in Linux.

Explore the Kernel Ring Buffer with dmesg

In this step, you will learn how to explore the kernel ring buffer using the dmesg command and its various options.

First, let's view the full contents of the kernel ring buffer:

sudo dmesg

This will display all the messages stored in the kernel ring buffer, including boot-time and runtime messages.

Next, let's filter the output to show only the most recent messages:

sudo dmesg -T | tail

The -T option displays the timestamps in a human-readable format, and tail shows the last 10 lines of the output.

You can also filter the output by log level. For example, to show only error messages:

sudo dmesg -l err

The -l option allows you to filter the output by log level, where err represents error messages.

To continuously monitor the kernel ring buffer in real-time, use the -w option:

sudo dmesg -w

This will keep the dmesg command running and display new messages as they are added to the kernel ring buffer.

Example output:

[  +0.000000] Linux version 5.15.0-58-generic (buildd@lgw01-amd64-054) (gcc-11) #64~20.04.1-Ubuntu SMP Thu Jan 5 12:11:15 UTC 2023 (Ubuntu 5.15.0-58.64~20.04.1-generic 5.15.52)
[  +0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.15.0-58-generic root=UUID=0b1d7f41-a4a6-4c7e-9e2f-5d8d6d7d3b2e ro quiet splash
[  +0.000000] KERNEL supported cpus:
[  +0.000000]   Intel GenuineIntel
[  +0.000000]   AMD AuthenticAMD
[  +0.000000]   Hygon HygonGenuine
[  +0.000000]   Centaur CentaurHauls
[  +0.000000]   zhaoxin   Shanghai

By exploring the different options of the dmesg command, you can effectively analyze the kernel ring buffer and gather valuable information for system monitoring and troubleshooting.

Filter and Analyze dmesg Output

In this final step, you will learn how to filter and analyze the output of the dmesg command to extract useful information for system monitoring and troubleshooting.

First, let's filter the dmesg output to show only the most recent messages:

sudo dmesg -T | tail

This will display the last 10 messages from the kernel ring buffer, with human-readable timestamps.

Next, let's search for specific keywords in the dmesg output. For example, to find any messages related to the network interface:

sudo dmesg | grep -i network

The -i option makes the search case-insensitive, so it will match both "network" and "Network".

You can also filter the output by log level and severity. For example, to show only warning and error messages:

sudo dmesg -l warn,err

This will display only the warning and error messages from the kernel ring buffer.

To get a summary of the kernel boot process, you can use the following command:

sudo dmesg | grep -E 'Linux version|Command line'

This will show the kernel version information and the command-line parameters used to boot the system.

Finally, let's save the dmesg output to a file for further analysis:

sudo dmesg > dmesg_output.txt

You can then review the contents of the dmesg_output.txt file using a text editor or other tools.

By mastering these filtering and analysis techniques, you can effectively use the dmesg command to monitor your Linux system and troubleshoot various issues.

Summary

In this lab, you learned about the purpose and usage of the dmesg command in Linux. The dmesg command is used to view the kernel ring buffer, which contains messages generated by the Linux kernel during system boot and runtime. You explored how to use the dmesg command to access and analyze these kernel-level messages, which can be useful for troubleshooting system issues and monitoring system health. You also learned about various options to filter and format the dmesg output, such as displaying timestamps, filtering by log level, and setting the console log level.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like