How to use the command 'dmesg' (with examples)

How to use the command 'dmesg' (with examples)

The ‘dmesg’ command in Linux is used to display kernel messages. These messages provide information about the system, including hardware detection, device drivers, and any errors or warnings that may have occurred. It is a useful tool for troubleshooting issues and monitoring system events.

Use case 1: Show kernel messages

Code:

dmesg

Motivation: This use case allows you to see all the kernel messages generated since the system was booted. It provides an overview of system events and can be helpful in diagnosing issues or analyzing the system’s behavior.

Explanation: The ‘dmesg’ command, when used without any arguments, displays all the kernel messages stored in the kernel ring buffer. These messages include information about various system events, such as device initialization, module loading, and system errors.

Example output:

[    0.000000] Linux version 5.4.0-54-generic (buildd@lgw01-amd64-056) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04))
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-54-generic root=UUID=d82e8e92-26bf-4591-8984-1639921318da ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
[    0.000000]   Centaur CentaurHauls
[    0.000000]   zhaoxin   Shanghai  
...

Use case 2: Show kernel error messages

Code:

dmesg --level err

Motivation: When troubleshooting system issues, it is often useful to focus on error messages generated by the kernel. By filtering the output to only show error messages, you can quickly identify and address critical issues.

Explanation: The ‘–level’ option is used to filter messages based on their severity level. In this case, we specify ’err’ to only display error messages.

Example output:

[   25.371535] gnome-shell[7023]: segfault at 0 ip 00007f8a4e53e2a3 sp 00007ffcb9d2fb30 error 4 in libmutter-6.so.0.0.0[7f8a4e2b9000+2e0000]
[   32.289541] nfs: server nfs-server-svc not responding, still trying
[   36.570726] nouveau 0000:01:00.0: bus: MMIO write of 80000059 FAULT at 10eb14 [ IBUS ]

Use case 3: Show kernel messages and keep reading new ones

Code:

dmesg -w

Motivation: This use case is particularly useful when you want to monitor the system in real-time and stay updated with any new kernel messages being generated. It is similar to the ’tail -f’ command for log files.

Explanation: The ‘-w’ option tells ‘dmesg’ to start in watch mode, continuously displaying new kernel messages as they are generated.

Example output:

[ 1862.686631] usb 1-5: new high-speed USB device number 4 using xhci_hcd
[ 1862.838392] usb 1-5: New USB device found, idVendor=1058, idProduct=25a3, bcdDevice=40.90
[ 1862.838401] usb 1-5: New USB device strings: Mfr=2, Product=3, SerialNumber=1
[ 1862.838406] usb 1-5: Product: My Passport 25A3
[ 1862.838410] usb 1-5: Manufacturer: Western Digital
[ 1862.838414] usb 1-5: SerialNumber: 575831314133324C345A574B

Use case 4: Show how much physical memory is available on this system

Code:

dmesg | grep -i memory

Motivation: This use case allows you to quickly check the amount of physical memory available on your system. It can be helpful for monitoring memory usage and identifying any potential memory-related issues.

Explanation: The ‘|’ symbol is used to pipe the output of one command to another command. In this case, we are piping the output of ‘dmesg’ to ‘grep’ to search for lines containing the keyword ‘memory’. The ‘-i’ option in ‘grep’ ignores case sensitivity.

Example output:

[    0.000000] Memory: 8193340K/8380412K available (14339K kernel code, 2558K rwdata, 4884K rodata, 2704K init, 5584K bss)

Use case 5: Show kernel messages 1 page at a time

Code:

dmesg | less

Motivation: The ‘dmesg’ command can produce a lot of output, and sometimes it’s easier to read the messages one page at a time, rather than scrolling through the entire output. The ’less’ command allows you to navigate through the output in a more controlled manner.

Explanation: The ‘|’ symbol is used to pipe the output of ‘dmesg’ to ’less’, which is a pager program. ’less’ allows you to scroll through the output one page at a time using the arrow keys or other navigation keys.

Example output: The output will be similar to the output in Use case 1, but it will be shown in a viewer that allows you to scroll and navigate through the content.

Use case 6: Show kernel messages with a timestamp

Code:

dmesg -T

Motivation: Timestamps can be useful for correlating events or measuring time intervals between kernel messages. This use case adds timestamps to the kernel messages, providing a more comprehensive view of the system’s behavior.

Explanation: The ‘-T’ option activates the timestamp feature in ‘dmesg’. It prefixes each kernel message with a timestamp, indicating the time the message was generated.

Example output:

[Sun Dec 20 10:30:22 2020] ACPI BIOS Error (bug): Could not resolve [\_SB.PCI0.LPC0.EC0], AE_NOT_FOUND (20190816/psargs-330)
[Sun Dec 20 10:30:22 2020] ACPI Error: Aborting method \_SB.PCI0.LPC0.EC0.RPET due to previous error (AE_NOT_FOUND) (20190816/psparse-529)
[Sun Dec 20 10:30:22 2020] ACPI Error: Aborting method \_SB.PCI0.LPC0.EC0._Q8D due to previous error (AE_NOT_FOUND) (20190816/psparse-529)
...

Use case 7: Show kernel messages in human-readable form

Code:

dmesg -H

Motivation: By default, kernel messages are displayed in a machine-readable format. This use case converts the output to a human-readable format, making it easier to understand the information provided by the kernel messages.

Explanation: The ‘-H’ option enables the human-readable format in ‘dmesg’. It converts numerical values and other system details into a more readable format, improving the accessibility of the kernel messages.

Example output:

[    0.000000] Linux version 5.4.0-54-generic (buildd@lgw01-amd64-056) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #60-Ubuntu SMP Fri Nov
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-54-generic root=UUID=d82e8e92-26bf-4591-8984-1639921318da ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   is a placeholder
[    0.000000]   Centaur CentaurHauls
[    0.000000]   is a placeholder
...

Use case 8: Colorize output

Code:

dmesg -L

Motivation: Colorizing the output of kernel messages can make it easier to spot important details or differentiate between different types of messages. This use case adds color to the output, improving its readability.

Explanation: The ‘-L’ option enables the colorization feature in ‘dmesg’. It applies different colors to various elements of the kernel messages, such as timestamps, log levels, and error messages.

Example output:

Colored output, with different elements of the kernel messages highlighted in different colors for improved readability.

[    0.000000] Linux version 5.4.0-54-generic (buildd@lgw01-amd64-056) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04))
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-54-generic root=UUID=d82e8e92-26bf-4591-8984-1639921318da ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
[    0.000000]   Centaur CentaurHauls
[    0.000000]   zhaoxin   Shanghai  
...

Conclusion:

The ‘dmesg’ command is a versatile tool for accessing and analyzing kernel messages on a Linux system. From reviewing system events and error messages to monitoring memory availability, this command provides valuable insights into the system’s behavior. By leveraging the various options and arguments available, such as filtering by severity level, adding timestamps, or colorizing the output, users can tailor the ‘dmesg’ command to suit their specific needs. Whether troubleshooting issues or gaining a deeper understanding of system activity, the ‘dmesg’ command is an essential tool for Linux administrators and power users.

Related Posts

How to use the command unsquashfs (with examples)

How to use the command unsquashfs (with examples)

Unsquashfs is a command-line tool used to uncompress, extract, and list files in squashfs filesystems.

Read More
Using kmutil (with examples)

Using kmutil (with examples)

1: Finding kexts available on the operating system kmutil find Motivation: This command is useful when you want to list all kernel extensions (kexts) available on your operating system.

Read More
How to use the command cgcreate (with examples)

How to use the command cgcreate (with examples)

The cgcreate command is used to create cgroups, which are used to limit, measure, and control resources used by processes.

Read More