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.
B. Syntax
The syntax for the at command in Linux simply contains the command itself and then desired options.
dmesg [OPTION]...
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.
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:
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.
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.
Similar Readings
- The “time” Command in Linux [4 Practical Examples]
- The “tty” Command in Linux [4 Practical Examples]
- The “uptime” Command in Linux [5 Practical Examples]
- The “vmstat” Command in Linux [6 Practical Examples]
- The “uname” Command in Linux [11 Practical Examples]
- The “apt-get” Command in Linux [10 Practical Examples]
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.
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.
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.
Similar Readings
- The “free” Command in Linux [8 Practical Examples]
- The “getent” Command in Linux [11 Practical Examples]
- The “groupadd” Command in Linux [7 Practical Examples]
- The “addgroup” Command in Linux [7 Practical Examples]
- The “groups” Command in Linux [6 Practical Examples]
- The “firewall-cmd” Command in Linux [7 Practical Examples]
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.
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 messages
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.
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
- The “groupmod” Command in Linux [5+ Practical Examples]
- The “id” Command in Linux [7+ Practical Examples]
- The “service” Command in Linux [6 Practical Examples]
- The “sestatus” Command in Linux [4 Practical Examples]
- The “shutdown” Command in Linux [7 Practical Examples]
- The “finger” Command in Linux [6 Practical Examples]
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.In this 2nd image, you can see that the process is still running and waiting for new messages.
Similar Readings
- The “passwd” Command in Linux [7 Practical Examples]
- The “install” Command in Linux [6+ Practical Examples]
- The “reboot” Command in Linux [3 Practical Examples]
- The “sync” Command in Linux [8 Practical Examples]
- The “chage” Command in Linux [7 Practical Examples]
- The “env ” Command in Linux [9 Practical Examples]
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.
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
- The “sudo” Command in Linux [8 Practical Examples]
- The “df” Command in Linux [11 Practical Examples]
- The “apt” Command in Linux [13+ Practical Examples]
- The “top” Command in Linux [8 Practical Examples]
- The “htop” Command in Linux [7 Practical Examples]
- The “enable” Command in Linux [6 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners