The ‘dmesg‘ command displays the messages from the kernel ring buffer. A system passes multiple runlevel from where we can get lot of information like system architecture, cpu, attached device, RAM etc. When computer boots up, a kernel (core of an operating system) is loaded into memory. During that period number of messages are being displayed where we can see hardware devices detected by kernel.
The messages are very important in terms of diagnosing purpose in case of device failure. When we connect or disconnect hardware device 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 System.
Let’s throw some light on most famous tool called ‘dmesg’ command with their practical examples as discussed below. The exact syntax of dmesg as follows.
1 | # dmseg [options...] |
1. List all loaded Drivers in Kernel
We can use text-manipulation tools i.e. ‘more‘, ‘tail‘, ‘less‘ or ‘grep‘ with dmesg command. As output of dmesg log won’t fit on a single page, using dmesg with pipe more or less command will display logs in a single page.
1 | [root@tecmint.com ~]# dmesg | more |
Sample Output
1 | [ 0.000000] Initializing cgroup subsys cpuset |
Read Also: Manage Linux Files Effectively using commands head, tail and cat
2. List all Detected Devices
To discover which hard disks has been detected by kernel, you can search for the keyword “sda” along with “grep” like shown below.
1 | [root@tecmint.com ~]# dmesg | grep sda |
NOTE: The ‘sda’ first SATA hard drive, ‘sdb’ is the second SATA hard drive and so on. Search with ‘hda’ or ‘hdb’ in the case of IDE hard drive.
3. Print Only First 20 Lines of Output
The ‘head’ along with dmesg will show starting lines i.e. ‘dmesg | head -20’ will print only 20 lines from the starting point.
1 | [root@tecmint.com ~]# dmesg | head -20 |
4. Print Only Last 20 Lines of Output
The ‘tail’ along with dmesg command will print only 20 last lines, this is useful in case we insert removable device.
1 | [root@tecmint.com ~]# dmesg | tail -20 |
5. Search Detected Device or Particular String
It’s difficult to search particular string due to length of dmesg output. So, filter the lines with are having string like ‘usb‘ ‘dma‘ ‘tty‘ and ‘memory‘ etc. The ‘-i’ option instruct to grep command to ignore the case (upper or lower case letters).
1 | [root@tecmint.com log]# dmesg | grep -i usb |
Sample Output
1 | [ 0.000000] Scanning 1 areas for low memory corruption |
6. Clear dmesg Buffer Logs
Yes, we can clear dmesg logs if required with below command. It will clear dmesg ring buffer message logs till you executed the command below. Still you can view logs stored in ‘/var/log/dmesg‘ files. If you connect any device will generate dmesg output.
1 | [root@tecmint.com log]# dmesg -c |
7. Monitoring dmesg in Real Time
Some distro allows command ‘tail -f /var/log/dmesg’ as well for real time dmesg monitoring.
1 | [root@tecmint.com log]# watch "dmesg | tail -20" |
Conclusion: The dmesg command is useful as dmesg records all the system changes done or occur in real time. As always you can man dmesg to get more information.