The ‘dmesg‘ command displays the messages from the kernel ring buffer. A system passes multiple run levels from where we can get lot of information like system architecture, CPU, attached device, RAM etc. When a computer boots up, a kernel (core of an operating system) is loaded into memory. During that period a number of messages are being displayed where we can see hardware devices detected by the kernel.
The messages are very important in terms of diagnosing problems in case of a device failure. When we connect or disconnect hardware devices on the system, with the help of dmesg command we come to know detected or disconnected information on the fly. The dmesg command is available on most Linux and Unix based Operating Systems.
The exact syntax of dmesg as follows.
~# dmseg [options…]
We can use text-manipulation tools like ‘more‘, ‘tail‘, ‘less‘ or ‘grep‘ with the dmesg command. As the output of the dmesg log won’t fit on a single page, using dmesg with pipe more or less command will display logs in a single page.
~# dmesg | more
~# dmesg | less
To discover which hard disks have been detected by the kernel, you can search for the keyword “sda” along with “grep” like shown below.
~# dmesg | grep sda
The ‘head’ along with dmesg will show the starting lines i.e. ‘dmesg | head -20′ will print only 20 lines from the starting point.
~# dmesg | head -20
The ‘tail’ along with the dmesg command will print only 20 last lines, this is useful in case we insert a removable device.
~# dmesg | tail -20
It’s difficult to search particular strings due to length of the dmesg output. So, filter the lines with having a string like ‘usb‘ ‘dma‘ ‘tty‘ or ‘memory‘ etc. The ‘-i’ option instruct to grep the command to ignore the case (upper or lower case letters).
~# dmesg | grep -i usb
~# dmesg | grep -i dma
~# dmesg | grep -i tty
~# dmesg | grep -i memory
We can clear dmesg logs if required with the below command. It will clear the dmesg ring buffer message logs till you have executed the command below. Still you can view logs stored in ‘/var/log/dmesg‘ files. If you connect any device this will generate a dmesg output.
~# dmesg -c
Some distro’s allows the command ‘tail -f /var/log/dmesg’ as well for real time dmesg monitoring.
# watch “dmesg | tail -20”