dmesg command is a Linux utility that displays kernel-related messages retrieved from the kernel ring buffer. The ring buffer stores information about hardware, device driver initialization, and messages from kernel modules that take place during system startup.
dmesg command is invaluable when troubleshooting hardware-related errors, warnings, and for diagnosing device failure.
In this tutorial, you will learn how to use the
dmesg command in Linux.
- A computer system running Linux.
- A user account with administrator privileges.
Syntax and Options
dmesg command syntax is:
The table below lists the most commonly used
|Clears the ring buffer.|
|Prints the ring buffer contents and then clears.|
|Restricts output to the specified comma-separated facility |
|Enables a human-readable output.|
|Adds color to the output. Omitting the |
|Restricts the output to the specified comma-separated level list.|
|Disables the feature of automatically escaping unprintable and potentially unsafe characters.|
|Uses the specified buffer size to query the kernel ring buffer. The default value is 16392.|
|Prints human-readable timestamps.|
|Prints timestamps using the specified |
|Decodes the facility and level numbers to human-readable prefixes.|
|Displays the help file with all the available options.|
Linux dmesg Command Examples
The examples are common
dmesg command use cases.
Display All Messages from Kernel Ring Buffer
dmesg without any options outputs the entire kernel buffer, without stops, and with no way to navigate the output.
The example above is a partial
dmesg command output. For easier navigation and better readability, pipe the
dmesg output into a terminal pager such as
more, or use grep.
sudo dmesg | less
less allows you to use the search function to locate and highlight items. Activate search by pressing /. Navigate to the next screen using the Space bar, or reverse using the B key. Exit the output by pressing Q.
Note: Read our article and learn more about the less command in Linux.
Display Colored Messages
dmesg produces a colored output. If the output isn't colored, use the
-L option to colorize it.
sudo dmesg -L
To turn off colored outputs, append the
--color=never option to
dmesg. Run the following command:
sudo dmesg --color=never
dmesg output is now uniform in color.
Display Messages as They Arrive
Monitor the kernel ring buffer in real-time using the
--follow option. The option instructs the command to wait for new messages related to hardware or kernel modules after system startup.
Run the following
dmesg command to enable real-time kernel ring buffer monitoring:
sudo dmesg --follow
The command displays any new messages at the bottom of the terminal window. Stop the process using Ctrl+C.
Note: Run any command repeatedly with the Linux watch command.
Search for a Specific Term
When searching for specific issues or hardware messages, pipe the
dmesg output into
grep to search for a particular string or pattern.
For example, if you are looking for messages about memory, run the following command:
dmesg | grep -i memory
The output shows all the lines from the buffer containing the
memory string. The
-i (ignore case) switch ignores care sensitivity.
Alternatively, if you are looking for buffer messages about USB, serial ports, network, or hard drives, run the following commands:
dmesg | grep -i usb
dmesg | grep -i tty
dmesg | grep -i eth
sudo dmesg | grep -i sda
Search for multiple terms at once by appending the
-E option to
grep and providing the search terms encased in quotations, separated by pipe delimiters. For example:
sudo dmesg | grep -E "memory|tty"
The output prints all the messages containing any of the search terms.
Read and Clear dmesg Logs
--read-clear) option lets you clear the
dmesg log after printing it. Clearing the buffer ensures you have only valid messages from the latest reboot.
Note: To save the entire log in a file before clearing it, redirect the output to a file with
sudo dmesg > log_file.
Run the following command:
sudo dmesg -c
dmesg has no output since the log has been cleared.
Enable Timestamps in dmesg Logs
Enable timestamps in
dmesg output by appending it with the
--human) option, which produces a human-readable output and automatically pipes the output into a pager (
Run the following command:
sudo dmesg -H
The command adds a timestamp with the date and time resolved in minutes. The events in the same minute are labeled with seconds and nanoseconds.
Quit the pager by pressing Q.
Enable Human-Readable Timestamps
Enable human-readable timestamps using the
--ctime) option. The option removes the nanosecond accuracy from the output, but the timestamps are easier to follow.
sudo dmesg -T
The timestamps in the output are standard dates and time, and the resolution is in minutes. The same timestamp is prepended to each action that occurred in the same minute.
Note: Unlike the
-H option, the
-T option doesn't automatically invoke
Choose Timestamp Format
--time-format [format] option to choose the timestamp format. The available formats are:
For example, to use the
iso format, run:
sudo dmesg --time-format=iso
The timestamp format is now
YYYY-MM-DD<T>HH:MM:SS,<microseconds>←+><timezone offset from UTC>.
ctime may show inaccurate time when the system is suspended and resumed.
Limit dmesg Output to a Specific Facility
dmesg output to a specific category using the
-f option. The system groups messages in the kernel ring buffer into the following facilities (categories):
kern. Kernel messages.
user. User-level messages.
daemon. Messages about system daemons.
auth. Authorization messages.
lpr. Line printer subsystem messages.
news. Network news subsystem messages.
For example, the following command limits the output to messages related to the
sudo dmesg -f syslog
To list messages from more than one facility, specify a comma-separated list of facilities. For example:
sudo dmesg -f syslog,daemon
Filter Log Levels
dmesg command associates each buffer message with a log level characterizing message importance. The available levels are:
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.
dmesg to print only the messages matching a certain level using the
-l option, followed by the level name. For example:
sudo dmesg -l info
The example above extracts only informational messages from the log.
Combine multiple levels in a comma-separated list to retrieve messages from those levels. For example:
sudo dmesg -l notice,warn
The output combines messages from the specified log levels.
Combining Facility and Level
Explicitly show each buffer message's facility and log level at the start of each line using the
-x (decode) option.
sudo dmesg -x
In the example above, each line is prepended with the appropriate facility and log level.
Read dmesg Log File
Each time the system boots up, the messages from the kernel ring buffer are stored in the /var/log/dmesg file. The
dmesg command shows the log file contents. If you have issues using the
dmesg command, open the log file in a text editor to view the contents.
Note: Read our tutorial for more information on Linux log files and how to read them.
In the example below, we use the cat command to view the log file and pipe it into
grep to search for a particular string occurring in the log:
cat dmesg | grep amd
The command outputs all
amd string instances in the log file.
Check for a CD Drive
Check if a remote machine is equipped with a CD drive by inspecting the buffer message log. For example, the following command shows all messages about CD devices initialized on startup:
sudo dmesg | grep -iE 'cdrom|dvd|cd/rw|cd-rom'
The results display information about the available CD-ROM drives, including the virtual CD-ROM drive on this machine.
Remove sudo Requirement
sudo sysctl -w kernel.dmesg_restrict=0
After setting the restrictions to
0, any user on the system can run
This guide explained how to use the Linux
dmesg command to view and control the kernel ring buffer. The utility is convenient when troubleshooting kernel or hardware issues.