How to Use the dmesg Linux Command

The dmesg command is a useful tool in Linux that lets you check what’s happening with your system’s kernel. It helps you see important messages from the kernel, which can be helpful for fixing problems and improving your system’s performance. These kernel messages come from the kernel modules that are loaded when your system starts up.

In this tutorial, we will demonstrate what the dmesg command is and practical examples of using this command in Linux.

What is dmesg in Linux?

The dmesg command displays kernel messages on Unix-like systems, which provide information stored in a special memory area known as the ring buffer. This ring buffer captures important events related to hardware, device driver initialization, and messages from kernel modules during system startup and operation. The basic syntax to use this command is shown below:

dmesg [options]

The common options for dmesg command are listed below, along with their functionalities:

  • -C: It clears the ring buffer.
  • -c: Print and clear the ring buffer.
  • -H: It provides the human-readable output, making timestamps and information more understandable
  • -T: Displays timestamps in a human-readable format for each message. 
  • -l: Filters messages by their log level, such as errors, warnings, or info.
  • -k: Displays only kernel-related messages, hiding user-space messages.
  • -s: Specifies how much buffer to read for messages, such as 1024 bytes.
  • -f: Filters messages by their facility, like kernel or user messages.
  • -r: Shows the raw output without converting priority info into readable text.
  • -x: Converts the facility and log level into readable text, like “kernel.info”.

You can learn more about the options available for the Linux dmesg command by typing man dmesg in the terminal. This command opens the manual page for dmesg, where you can see detailed information about its usage, options, and examples:

man dmesg
dmesg man page

Practical Examples of Using dmesg Command in Linux

Here are a few examples of the dmesg command that demonstrate its basic usage in Linux:

Example 1: Displaying Kernel Messages Using dmesg

Let’s run the dmesg command with any optional flag to see its basic functionality in Linux:

sudo dmesg
show kernel messages via dmesg

The output shows that the dmesg command retrieves the system messages related to the Linux kernel.

Example 2: Navigating Through Logs Page by Page

The dmesg command displays a lot of information, which can be hard to read all at once. To make it easier, you can use commands like tail, head, or less with dmesg to view the logs one page at a time. For example, the below command uses the dmesg with less command to show the system messages page by page:

sudo dmesg | less
dmesg with less

You can use the Down Arrow or Enter to scroll line by line in the less viewer, and press q to quit and return to the terminal.

Example 3: Adding Color to dmesg Output

If you want to add colors to make the messages easier to understand, you can colorize them with the -L option:

sudo dmesg -L
dmesg with l option

To disable the colored output, you can use the “–color=never” option, as follows:

sudo dmesg --color=never
disable color

Example 4: Displaying Kernal Messages in Human-Friendly Timestamps 

dmesg shows timestamps in seconds and nanoseconds. We can use the -H option with the dmesg command to see the timestamps in a human-friendly format with the date and time: 

sudo dmesg -H
dmesg with h option

Example 5: Viewing Kernel Messages in Standard Date and Time Format

You can use the -T (or –ctime) option with the dmesg command to enable human-readable timestamps. This option removes nanosecond details, making the timestamps simpler to read:

sudo dmesg -T

The output below shows that dmesg returns the kernel messages in standard date and time format, which is easily understandable:

dmesg with t option

Example 6: Displaying a Specific Number of Kernel Messages

We can use the dmesg command with the head command to show a limited/specific number of kernel messages from the top:

sudo dmesg | head -15

This command shows the first 15 kernel messages:

dmesg with head

Similarly, we can use the tail command with dmesg to show the last N kernel messages:

sudo dmesg | tail -15

This command shows the last 15 kernel messages:

dmesg with tail

Example 7: Filtering Kernel Messages by Specific Log Level

Each message logged to the kernel ring buffer has a “level” that shows how important the message is. Here are the commonly used levels:

  • emerg: System is unusable.
  • alert: Immediate action needed.
  • crit: Critical conditions.
  • err: Errors.
  • warn: Warnings.
  • notice: Normal but important.
  • info: General information.
  • debug: Debugging details.

To filter messages by level, we can use the -l (level) option with the dmesg command, followed by the level name. For instance, to list all “warn” level messages, we can use the “dmesg -l warn” command to see the warning notifications:

sudo dmesg -l warn
filter kernel messages

To find the disk-related messages such as memory, RAM, hard disk, or USB drives, we can use the grep command with dmesg, as follows:

sudo dmesg | grep -i sda

This command uses grep with “sda” to check which hard disks the kernel has detected. The “sda” part refers to hard disk drives; this command will show any messages where “sda” appears:

dmesg with grep

Example 9: Displaying Facility and Level

The -x (decode) option adds the facility and level as prefixes to each line, making them easier to read. For instance, we use the -x option with the dmesg command to display each message’s facility and log level at the beginning of each line:

sudo dmesg -x
dmesg with x option

Example 10: Reading dmesg Log File

Each time our system boots, it saves the kernel ring buffer messages in the /var/log/dmesg file. We can use the dmesg command to view these messages. If we encounter issues with dmesg, we can open the /var/log/dmesg file in a text editor to view the messages directly. For this, first, access the /var/log directory using the Linux cd command:

cd /var/log

Now, use the Linux cat command to view the log file and then pipe it into grep to find a specific word or phrase in the log:

cat dmesg | grep amd
reading dmesg log file

Conclusion

The dmesg command is a useful command line utility for checking kernel messages in Linux. It provides useful information about our system’s hardware and drivers, which helps us fix problems and improve performance. In this tutorial, we discussed several use cases of the dmesg command, such as filtering messages, showing timestamps, viewing logs in an easy-to-read way, etc. 

The dmesg command lets you view kernel messages and system events, making it essential for troubleshooting in Linux. For a smoother experience, consider Ultahost’s fast VPS hosting. With various affordable plans, Ultahost ensures reliable uptime, giving you the ideal environment to practice dmesg and other Linux commands effortlessly.

FAQ

What does the dmesg command do?
Why is dmesg important for Linux users?
How do I run the dmesg command?
What are some common options for the dmesg command?
How can I view the output of dmesg one page at a time?
Can I filter dmesg messages by log level?
Where can I find the saved dmesg logs?

Related Post

How to Use the wc Command in Linux

wc is a command-line utility in Linux that stands for w...

How to configure PHP parameters on Linux Serv

PHP a widely used server-side scripting language plays ...

Unlocking the power of Linux Pipe Command

The pipe command, denoted by the bar "|", is a powerful...

Guide to Installing Commands on CentOS

When a Windows user switches to Linux the first thing i...

How to Forward Ports With Iptables in Linux

Port forwarding is a crucial technique for network admi...

How to Use the which Command in Linux

The which command in Linux is a simple yet powerful uti...

Leave a Comment