dmesg Command in Linux — How to Read Kernel Logs
dmesg reads messages from the kernel ring buffer — a log where the kernel records hardware detection, driver loading, and errors from the moment of boot. Unlike /var/log entries, these are low-level messages straight from the kernel, useful for diagnosing boot failures, missing hardware, disk errors, and network problems.
How to use dmesg? Basic commands
Simply running dmesg in the terminal will return hundreds, or even thousands, of lines. The art is in finding what you’re looking for.
# Display all messages
dmesg
The output is usually too large to analyze. So, let’s use other tools to filter it.
Paging through the output
To comfortably browse the logs, use less.
dmesg | less
You can now scroll up and down with the arrow keys and exit by pressing q.
Filtering for errors
Most often, we are looking for problems. Use grep to filter lines containing words like “error”, “fail”, or “warning”. grep is case-insensitive (-i), so we don’t have to worry about capitalization.
dmesg | grep -i "error"
Practical example: Diagnosing a USB problem
Imagine you plug in an external USB drive and nothing happens. How do you diagnose this?
- Open a terminal.
- Enter the command that will show new messages in real-time:
(or
dmesg -wsudo dmesg --followif-wdoesn’t work) - Now, plug in the USB device.
- Watch the terminal. You should see a series of new lines, informing you about the detection of a new device.
If you see something like this:
[ 1234.567890] usb 1-2: new high-speed USB device number 3 using xhci_hcd
[ 1234.567990] usb 1-2: New USB device found, idVendor=0bc2, idProduct=ab38
[ 1234.568090] scsi host4: uas
…it means the hardware was detected correctly.
However, if errors appear, e.g., device descriptor read/64, error -110, you now have a specific clue that you can search for on the internet to find a solution.
Coloring and readability
To make reading easier, you can add the --color=always option to grep or use the dmesg tool with the -H (human-readable) option, which formats the output nicely.
dmesg | grep -i --color=always "fail"
Other useful dmesg options
dmesg -T: Displays the date and time in a human-readable format (instead of seconds since system boot).dmesg -l err,warn: Shows only error and warning messages.dmesg -c: Displays the logs and then clears the buffer. Be careful with this option!
When something goes wrong at the hardware or driver level, dmesg is the first place to check.