
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Limiting the Number of Output Lines
If you want to limit the number of lines displayed, you can use the -n option followed by the number of lines you want to display. For example, to display the last 10 lines, you can use ?
$ dmesg -n 10
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.
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.000000] [Firmware Bug]: ACPI MWAIT C-state 0x0 not supported by HW (0x0) [ 0.000000] ACPI: \_PR_.CPU0: _OSC native thermal LVT Acked [ 0.232991] ACPI: Added _OSI(Linux-Dell-Video) [ 0.232991] ACPI: Added _OSI(Linux-Lenovo-NV-HDMI-Audio) [ 0.232991] ACPI: Added _OSI(Linux-HPI-Hybrid-Graphics)
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.684729] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00 [ 1.684743] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA [ 1.685918] sda: sda1 sda2 sda3 [ 1.686329] sd 0:0:0:0: [sda] Attached SCSI disk
Example 4: Displaying Messages from the Last Boot
To display messages from the last boot, you can use the -b option followed by 1 ?
$ dmesg -b 1
The output will be similar to the default dmesg output, but it will only include messages from the last boot.
Example 5: 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 0x00000000000e0000-0x00000000000fffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000007b9fdfff] usable [ 0.000000] BIOS-e820: [mem 0x000000007b9fe000-0x000000007ba53fff] ACPI NVS
Remember, the actual output of these commands will vary depending on your system's configuration and current state.
Example 6: Displaying Messages Related to CPU
To display messages related to the CPU, you can use ?
$ dmesg | grep -i cpu
The output might look something like this ?
[ 0.000000] smpboot: CPU0: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz (family: 0x6, model: 0x9e, stepping: 0x9) [ 0.000000] Performance Events: PEBS fmt3+, Skylake events, 32-deep LBR, full-width counters, Intel PMU driver. [ 0.000000] ... version: 4 [ 0.000000] ... bit width: 48 [ 0.000000] ... generic registers: 4 [ 0.000000] ... value mask: 0000ffffffffffff [ 0.000000] ... max period: 00007fffffffffff [ 0.000000] ... fixed-purpose events: 3 [ 0.000000] ... event mask: 000000070000000f
Example 7: Displaying Messages Related to a 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 8: Displaying Messages Related to Disk Errors
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 (20170831/psargs-364) [ 3.056790] ACPI Error: Method parse/execution failed \_SB.PCI0.SAT0.SPT5._GTF, AE_NOT_FOUND (20170831/psparse-550) [ 3.056891] ACPI Error: [DSSP] Namespace lookup failure, AE_NOT_FOUND (20170831/psargs-364) [ 3.056891] ACPI Error: Method parse/execution failed \_SB.PCI0.SAT0.SPT5._GTF, AE_NOT_FOUND (20170831/psparse-550)
Example 9: Displaying Messages Related to the Network
To display messages related to the network, you can use ?
$ dmesg | grep -i net
The output might look something like this ?
[ 0.000000] NET: Registered protocol family 16 [ 0.000000] audit: initializing netlink subsys (disabled) [ 0.000000] audit: type=2000 audit(1622470186.040:1): state=initialized audit_enabled=0 res=1 [ 0.000000] workingset: timestamp_bits=46 max_order=20 bucket_order=0 [ 0.000000] NFS: Registering theid_resolver key type [ 0.000000] Key type id_resolver registered [ 0.000000] Key type id_legacy registered [ 0.000000] NET: Registered protocol family 38 [ 0.000000] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 244) [ 0.000000] io scheduler mq-deadline registered [ 0.000000] io scheduler kyber registered
Example 10: Displaying Messages Related to the File System
To display messages related to the file system, you can use ?
$ dmesg | grep -i fs
The output might look something like this ?
[ 0.000000] Freeing unused kernel image memory: 2028K [ 0.000000] Write protecting the kernel read-only data: 20480k [ 0.000000] Freeing unused kernel image memory: 1984K [ 0.000000] Freeing unused kernel image memory: 1836K [ 0.000000] x86/mm: Checked W+X mappings: passed, no W+X pages found. [ 0.000000] rodata_test: all tests were successful [ 0.000000] x86/mm: Checking user space page tables [ 0.000000] x86/mm: Checked W+X mappings: passed, no W+X pages found. [ 0.000000] Run /init as init process [ 0.000000] with arguments: [ 0.000000] /init [ 0.000000] with environment: [ 0.000000] HOME=/ [ 0.000000] TERM=linux [ 0.000000] BOOT_IMAGE=/boot/vmlinuz-5.4.0-26-generic
Remember, the actual output of these commands will vary depending on your system's configuration and current state.
Conclusion
The dmesg command is a powerful tool for diagnosing and troubleshooting system issues. It provides a wealth of information about the system's operation, and with the right options, you can filter and format this information to suit your needs. As with any command, you can use man dmesg to display the manual and learn more about its usage.