Skip to content

dmesg Linux Command Guide

The dmesg command in Linux is used to display kernel ring buffer messages. It can help diagnose hardware issues, view system boot messages, and troubleshoot system problems. By default, dmesg shows the most recent kernel messages. With various options, users can filter messages, display timestamps, and more.

dmesg Syntax:

Terminal window
dmesg [options]

Options:

OptionDescription
-cClear the ring buffer after printing.
-sSet the buffer size for the kernel log.
-lRestrict output to the given log level.
-nSet the level of messages shown.
-HHuman-readable output.
-TInclude timestamps in the output.
-DShow timestamps in readable format.
-xShow hexadecimal and ASCII representation of the message buffer.
-wWait for new messages to appear.
-dShow all kernel messages.
—colorColorize the output.

Parameters:

There are no specific parameters for the dmesg command.

dmesg Command Samples:

Display the most recent kernel messages

Terminal window
dmesg

This command displays the most recent kernel messages.

Display kernel messages with a timestamp

Terminal window
dmesg -T

Shows kernel messages with human-readable timestamps.

Terminal window
dmesg | grep memory

Filters the kernel messages to display only those related to memory.

Display messages of a specific log level

Terminal window
dmesg -l err

Shows kernel messages with a certain log level, in this case “error” messages.

Save kernel messages to a file

Terminal window
dmesg > kernel_messages.txt

Redirects the kernel messages output to a file named “kernel_messages.txt”.

Watch kernel messages in real-time

Terminal window
dmesg --follow

Allows watching kernel messages in real-time as they are generated.

Show the number of kernel messages

Terminal window
dmesg -c | wc -l

Counts and displays the number of kernel messages currently in the buffer.

dmesg FAQ:

How do I use dmesg in Linux?

To use the dmesg command in Linux, execute the following command:

Terminal window
dmesg

How can I display the last 20 lines of kernel messages using dmesg?

To display the last 20 lines of kernel messages in Linux with dmesg, use the following command:

Terminal window
dmesg | tail -n 20

How can I search for specific keywords in kernel messages using dmesg?

To search for specific keywords in kernel messages using dmesg in Linux, you can use the grep command along with dmesg. For example:

Terminal window
dmesg | grep "usb"

How can I clear the kernel ring buffer using dmesg?

To clear the kernel ring buffer in Linux using dmesg, use the following command:

Terminal window
sudo dmesg -c

How can I view kernel messages in real-time using dmesg?

To view kernel messages in real-time in Linux using dmesg, you can use the -w option. Execute the following command:

Terminal window
dmesg -w

How can I save kernel messages to a file with timestamps using dmesg?

To save kernel messages to a file with timestamps in Linux using dmesg, you can use the --ctime option along with redirection. Here’s an example:

Terminal window
dmesg --ctime > kernel_logs.txt

Applications of the dmesg command

  • Display kernel ring buffer messages
  • Troubleshoot hardware and driver issues
  • Check for system initialization errors
  • View system boot messages
  • Debug device detection and driver loading issues