The “dmesg” Command in Linux [7+ Practical Examples]

The dmesg command in Linux  is used to display and control the Kernel Ring Buffer messages. You can use this command to monitor the device driver and hardware messages to find faults in your system. In this article, you will find the most useful applications of the dmesg command with practical examples.

The Kernel Ring Buffer in Linux

A Ring Buffer is a fixed-sized space on the system reserved for storing messages. It is called a Ring Buffer because it overwrites old messages with new ones when the space is full. Similarly, the Kernel Ring Buffer is a kind of Ring Buffer that stores the startup messages of device drivers, hardwares and Kernel modules. As it reserves the startup messages, the Kernel Ring Buffer is utilized to investigate system issues.

A. Description

The dmesg command in Linux enables users to examine and control the Kernel Ring Buffer.

Note: In some machines, non-root users may not have permission to use this command. In that case, you can remove this non-root user restriction. You will find the process of enabling the dmesg permissions in the next section.

B. Syntax

The syntax for the at command in Linux simply contains the command itself and then desired options.

dmesg [OPTION]...
Note: In the above syntax OPTION enclosed by square brackets and followed by 3 dots represents that multiple options can be utilized simultaneously.

C. Options

The dmesg command in Linux offers several options to modify your experience. However, you will find the most useful one’s here for your convenience. For any further features, you can always look at the man page.

man dmesg

Useful Options

  • -c: Clears the Ring Buffer.
  • -C: Clears the Ring Buffer after printing its contents on the terminal.
  • -D: Disables showing messages in the terminal.
  • -E: Enables showing messages in the terminal.
  • -f: Restricts the output to specified facilities (i.e. kern, daemon etc).
  • -H: Displays messages in a human-readable format.
  • -k: Displays kernel messages.
  • -l: Restricts the output to specified log levels (i.e. err, info etc).
  • -L/–color: Colorizes output.
  • -T: Displays messages in human-readable Timestamps.
  • -w/–follow: Waits for new messages.
NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Removing non-root User Restriction of “dmesg” Command

In most of the systems, all users have permission to run the dmesg command. However, in some machines, the command may be restricted to non-root users. You can check whether you are allowed to use the command as a non-root user by simply typing the command.

dmesg

You will get the following message on your terminal if it is restricted:Restriction message of non-root user for using dmesg command.

In this case, don’t be overwhelmed. You can easily remove the restriction within a minute by following the instructions below.

Steps to Follow >

➊  At first open the Ubuntu Terminal

➋   Type the following in your command prompt:

sudo sysctl -w kernel.dmesg_restrict=0

➌ Type your password

➍ Now, press ENTER

Output >

Upon completion of these four steps, you will be able to run the dmesg command on your machine without root permission.Enabling non-root user to use dmesg command in linux.

Practical Examples of the “dmesg” Command in Linux

The diff command in Linux is a convenient command to monitor Kernel messages and find issues. Some of the most helpful applications of the dmesg command have been illustrated below.

Example 1: Displaying All the Messages From the Kernel Ring Buffer Using the “dmesg” Command in Linux

You can view all the Kernel Ring Buffer messages using the dmesg  command in Linux. In this example, I will show you all the Ring Buffer messages on my machine. You can do the same by following the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

dmesg

➌ Press the ENTER button.

Output >

In the image below, you can see that I have shown all the Kernel Ring Buffer messages of my system.Displaying the Kernel Ring Buffer  messages of system using dmesg command in linux.


Similar Readings


Example 2: Enabling/Disabling Coloured Output Using the “dmesg” Command in Linux

You can remove colors from your output or colorize the messages using the dmesg command in Linux. The option –color is used to colorize or remove the colors whereas the option -L  will only generate colored output. You will find the uses of these options below.

Case A: Disabling Colored Output Using the “dmesg” Command in Linux

To remove colors from messages follow the given steps.

Steps to Follow >

➊ At first, launch the Ubuntu Terminal.

➋ Write the following command in the command line:

dmesg --color=never

➌ Now, hit the ENTER button.

Output >

In the given image, you can see that I have removed colors from the out.Removing color of the Kernel Ring Buffer  messages.

Case B: Enabling Colored Output Using the “dmesg” Command in Linux

To generate colored messages follow the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

dmesg -L

➌ Press the ENTER button.

Output >

In the image below, you can see that I have generated colored messages.Colorizing the Kernel Ring Buffer  messages.

Example 3: Displaying the Kernel Ring Buffer Messages in Human-Readable Format

You can display the Kernel Ring Buffer messages in the human-readable format using the dmesg command in Linux. To do so you will need to use option -H. The option will print the output in a less pager with all the advanced features. In this example, I will display the messages in a human-readable format. To achieve the same results you can follow the steps below.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command line:

dmesg -H

➌ Hit ENTER .

Output >

In the following image, you can see that I have displayed the messages in a human-readable format.Displaying the Kernel Ring Buffer Messages in Human-Readable Format.


Similar Readings


Example 4: Enabling Human-Readable Timestamps Using the “dmesg” Command in Linux

You can enable human-readable timestamps using the -T option with the dmesg command in Linux. In this example, I will demonstrate activating the human-readable timestamps while displaying the Kernel Ring Buffer messages. To do the same, follow the given steps.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

dmesg -T

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have enabled the human-readable timestamps of the Kernel Ring Buffer messages.Enabling Human-Readable Timestamps Using the dmesg Command in Linux

Example 5: Filtering Ring Buffer Messages Using the “dmesg” Command in Linux

You can filter Ring Buffer messages using the dmesg command in Linux. Messages can be filtered by specific facilities or log levels using the -f and -l options respectively. Follow the examples explained below to learn more about the applications of the options.

Case A: Filter Output by Specified Facilities Using the “dmesg” Command in Linux

To filter the Kernel Ring Buffer messages by specified facility follow the given steps.

Steps to Follow >

➊ At first launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

dmesg -f user,daemon

➌ Now, hit the ENTER button.

Output >

In the given Image, you can see that I have displayed only the user and daemon related Ring Buffer messagesFiltering Output by Specified Facilities.

Note: The system divides Ring Buffer messages into multiple facilities. A list of these facilities is given below.

  • kern: Kernel messages.
  • user: User-level messages.
  • mail: Mail system messages.
  • daemon: Messages about system daemons.
  • auth: Authorization messages.
  • syslog: Internal syslogd messages.
  • lpr: Line printer subsystem messages.
  • news: Network news subsystem messages.

Case B: Filter Output by Specified Log Levels Using the “dmesg” Command in Linux

To filter the Kernel Ring Buffer messages by specified log levels follow the given steps.

Steps to Follow >

➊ Go to your Ubuntu Terminal.

➋ Type the following command in the command line:

dmesg -l err,notice

➌ Strike ENTER.

Output >

In the given Image, you can see that I have displayed only the err and notice related Ring Buffer messages.Filtering Output by Log levels.

Note: The system indicates each Ring Buffer message with a certain log level. A list of these log levels is given below.

  • emerg: Emergency messages.
  • alert: Alerts requiring immediate action.
  • crit: Critical conditions.
  • err: Error messages.
  • warn: Warning messages.
  • notice: Normal but significant conditions.
  • info: Informational messages.
  • debug: Debugging-level messages.

Similar Readings


Example 6: Monitoring New Messages Using the “dmesg” Command in Linux

You can monitor live Kernel Ring Buffer messages using the -w/–follow options with the dmesg command in Linux. In this example, I will show you the application of these options. You can follow the steps below to get the same output.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

dmesg -w

OR,

dmesg --follow

➌ Now, press the ENTER button.

Output >

In the 1st image, you can see that I have displayed the  Kernel Ring Buffer messages.Waiting for new Kernel Buffer Ring messagesIn this 2nd image, you can see that the process is still running and waiting for new messages.Monitoring live Kernel ring messages using dmesg command in linux.


Similar Readings


Example 7: Clearing Message Logs Using the “dmesg” Command in Linux

You can clear the Kernel Ring Buffer messages using the -c/-C options with the dmesg command in Linux. You will need to act as a super user to clear the messages and use the command sudo. In this example, I will clear the Ring Buffer on my machine. You can do the same by following the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type any of the following commands in the command prompt:

dmesg -c

OR,

dmesg -C

➌ Press the ENTER button.

Output >

In the image below, you can see that I have cleared the Kernel Ring Buffer.Clearing Message Logs Using the dmesg Command in Linux

Conclusion

In this example, I have presented some of the most useful examples of the dmesg command in Linux. From now you will be able to examine and monitor the Kernel Ring Buffer messages from your terminal. I hope the knowledge from this article will aid your experience in Linux and help you become a power user.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment