Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Use the dmesg Linux Command?
The dmesg command is a powerful tool in the Linux command-line arsenal. It stands for "diagnostic message" and is used to read and write data from/to the kernel ring buffer, a data structure that holds messages about the system's hardware, kernel, or driver messages. This article will guide you through the usage of the dmesg command, complete with examples and their outputs.
Basic Usage
The most basic usage of the dmesg command is to simply type dmesg into your terminal and hit enter. This will display all the kernel messages in your terminal.
$ dmesg
The output will be a long list of messages, which might be overwhelming. Each line in the output represents a single message from the kernel, and they are displayed in the order they were logged.
Filtering the Output
Given the volume of messages, it's often useful to filter the output. You can use the grep command in conjunction with dmesg to search for specific terms. For example, if you want to find messages related to USB devices, you can use:
$ dmesg | grep -i usb
This will display all lines containing the term "usb", regardless of case.
Displaying the Output with Timestamps
By default, dmesg does not display the timestamps of the messages. However, you can use the -T option to display human-readable timestamps.
$ dmesg -T
The output will now include the date and time of each message.
Displaying Kernel Messages of a Certain Level
Kernel messages are categorized into eight levels, from 0 (emergencies) to 7 (debug). You can use the -l option followed by the level to display messages of a certain level. For example, to display only emergency messages, you can use:
$ dmesg -l emerg
Clearing the dmesg logs
The dmesg logs can be cleared using the -c option. This can be useful if you want to clear out old messages before running a process that you expect to generate kernel messages.
$ sudo dmesg -c
Please note that this command requires root privileges.
Examples and Output
Let's dive deeper into the dmesg command with more examples and their corresponding outputs.
Example 1: Displaying Hardware Messages
If you want to display messages related to your hardware, you can use the dmesg command with the grep command. For example, to display messages related to your Ethernet adapter, you can use:
$ dmesg | grep -i eth
The output might look something like this:
[ 2.687402] r8169 0000:02:00.0 eth0: RTL8168h/8111h, 00:e0:4c:68:22:2a, XID 541, IRQ 47 [ 2.687405] r8169 0000:02:00.0 eth0: jumbo features [frames: 9200 bytes, tx checksumming: ko] [ 3.056728] r8169 0000:02:00.0 eth0: link down [ 3.056790] r8169 0000:02:00.0 eth0: link down [ 3.056891] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
Example 2: Displaying Boot Messages
To display messages related to the boot process, you can use:
$ dmesg | grep -i boot
The output might look something like this:
[ 0.000000] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-26-generic root=UUID=5a598e63-8b76-4619-9182-2b7e7b0f0b1c ro quiet splash vt.handoff=7 [ 0.232991] ACPI: Added _OSI(Linux-Dell-Video) [ 0.232991] ACPI: Added _OSI(Linux-Lenovo-NV-HDMI-Audio)
Example 3: Displaying Messages Related to a Specific Device
If you want to display messages related to a specific device, you can use the device's name with the grep command. For example, to display messages related to the sda device, you can use:
$ dmesg | grep -i sda
The output might look something like this:
[ 1.684723] sd 0:0:0:0: [sda] 500118192 512-byte logical blocks: (256 GB/238 GiB) [ 1.684727] sd 0:0:0:0: [sda] Write Protect is off [ 1.685918] sda: sda1 sda2 sda3 [ 1.686329] sd 0:0:0:0: [sda] Attached SCSI disk
Example 4: Displaying Messages Related to Memory
To display messages related to memory, you can use:
$ dmesg | grep -i memory
The output might look something like this:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000007b9fdfff] usable
Example 5: Displaying Messages with Specific Time Frame
If you want to display messages from a specific time frame, you can use the -T option with the grep command. For example, to display messages from May 30, you can use:
$ dmesg -T | grep 'May 30'
The output will include all messages from May 30.
Example 6: Displaying Error Messages
To display messages related to disk errors, you can use:
$ dmesg | grep -i error
The output might look something like this:
[ 2.687402] EXT4-fs (sda1): re-mounted. Opts: errors=remount-ro [ 3.056728] ACPI Error: [DSSP] Namespace lookup failure, AE_NOT_FOUND [ 3.056790] ACPI Error: Method parse/execution failed \_SB.PCI0.SAT0.SPT5._GTF
Common dmesg Options
| Option | Description | Example |
|---|---|---|
| -T | Show human-readable timestamps | dmesg -T |
| -c | Clear the kernel ring buffer | sudo dmesg -c |
| -l level | Show messages of specific level | dmesg -l err |
| -w | Follow new messages | dmesg -w |
| --follow | Wait for new messages | dmesg --follow |
Message Priority Levels
| Level | Name | Description |
|---|---|---|
| 0 | emerg | System is unusable |
| 1 | alert | Action must be taken immediately |
| 2 | crit | Critical conditions |
| 3 | err | Error conditions |
| 4 | warn | Warning conditions |
| 5 | notice | Normal but significant condition |
| 6 | info | Informational messages |
| 7 | debug | Debug-level messages |
Conclusion
The dmesg command is an essential tool for system administrators and users alike for diagnosing hardware issues, monitoring kernel messages, and troubleshooting boot problems. It provides valuable insights into system operation and hardware status. Remember that the actual output will vary depending on your system's configuration and current state. Use man dmesg to explore additional options and features.
