How To Use Linux dmesg Command To Print System Logs?
dmesg is used to print and control kernel ring buffer. This explanation may be confusing for the most of the readers. Here simplified explanation.
dmesg provides logs for troubleshoot, diagnose or save which is created by Linux kernel. It provides low level messages which is not logged by other subsystems.
Provided Information Type
dmesg command and buffer provides a lot of different type of messages and logs. Here the list of some of them.
- Device Mapper
- Hard Drive
List and Print dmesg
We will start using
dmesg command without an option. This will print all message to the current working shell.
List and Troubleshoot Hard Drive Devices
One of the most used situation for dmesg message is the hard drive low level events. We can see operating system level actions of specified hard disk drive. We will filter with
grep command. In this example we will filter device named
$ dmesg | grep sda
List and Troubleshoot USB
In order to list and troubleshoot USB and related events in operating system we will use
grep too. This will list all log lines those have the term
$ dmesg | grep sda
List and Troubleshoot Memory or RAM message
We can list and print memory or ram related messages with
$ dmesg | grep -i memory
Print and List Last 20 Lines
While examining the dmesg messages we need to look specific time messages. We can print the last 20 lines of message with the
last command like below.
$ dmesg | tail -n 20
Print and List First 20 Lines
We can also print the first 20 lines of the dmesg provided log with the
head command like below.
$ dmesg | head -n 20
List and Print dmesg Messages in Real Time
We may need to print the logs and messages in real time. There is two way to accomplish this. First if our Linux kernel is newer than 3.5 we can use
-w option like below.
$ dmesg -w
Or if it is older than 3.5 we can use
tail command like below.
$ watch dmesg | tail -f
Clear dmesg Message Buffer
If we need to start troubleshooting and storing logs into a clear kernel ring buffer we need to remove existing log. We can use
-c option in order to clear the log buffer.
$ dmesg -c