Using the dmesg Command (with examples)

Using the dmesg Command (with examples)

The dmesg command in Unix-like operating systems is used to display kernel messages. These messages provide information about the current state of the system and can be useful for troubleshooting hardware or software issues. In this article, we will explore three different use cases of the dmesg command and provide code examples for each.

Use Case 1: Show kernel messages

The following code example demonstrates how to use the dmesg command to display kernel messages:

dmesg

Motivation: By running dmesg, you can view the latest kernel messages, which can help you understand the current state of your system. This can be useful for diagnosing issues or monitoring system activity.

Explanation: Running dmesg without any arguments will write the kernel messages to the standard output (stdout). This allows you to view the messages directly in your terminal.

Example Output:

[    0.000000] Linux version 5.4.0-70-generic (buildd@lgw01-amd64-054) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 (Ubuntu 5.4.0-70.78-generic 5.4.95)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-70-generic root=UUID=3ba274e1-012d-43bd-8944-38a4161ac972 ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
...

Use Case 2: Show how much physical memory is available on this system

The following code example demonstrates how to use the dmesg command along with grep to show the amount of physical memory available on the system:

dmesg | grep -i memory

Motivation: Knowing the amount of physical memory available on a system is important for determining if there is enough capacity for running applications or if there are any memory-related issues.

Explanation: In this example, we pipe the output of dmesg to grep in order to filter the kernel messages for lines that contain the word “memory” (case-insensitive). This allows us to extract information specifically related to memory.

Example Output:

[    0.000000] efi: mem00: type=7, attr=0xf, range=[0x0000000000000000-0x0000000000004000) (0MB)
[    0.000000] efi: mem01: type=0, attr=0xf, range=[0x0000000000004000-0x0000000000005000) (0MB)
[    0.000000] efi: mem02: type=2, attr=0xf, range=[0x0000000000005000-0x0000000000008000) (0MB)
[    0.000000] efi: mem03: type=7, attr=0xf, range=[0x0000000000008000-0x0000000001000000) (15MB)
[    0.000000] efi: mem04: type=2, attr=0xf, range=[0x0000000001000000-0x000000001f6a8000) (494MB)
[    0.000000] efi: mem05: type=7, attr=0xf, range=[0x000000001f6a8000-0x0000000020000000) (41MB)
...

Use Case 3: Show kernel messages 1 page at a time

The following code example demonstrates how to use the dmesg command along with less to view the kernel messages one page at a time:

dmesg | less

Motivation: When the output of dmesg is too long to fit in a single screen, it can be difficult to read and navigate through the messages. By piping the output to less, you can easily view and scroll through the messages page by page.

Explanation: In this example, we pipe the output of dmesg to less command. less is a pager utility that allows you to view long text files or command output interactively. It provides features such as scrolling, searching, and navigation.

Example Output: (Output similar to Use Case 1)

[    0.000000] Linux version 5.4.0-70-generic (buildd@lgw01-amd64-054) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 (Ubuntu 5.4.0-70.78-generic 5.4.95)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-70-generic root=UUID=3ba274e1-012d-43bd-8944-38a4161ac972 ro quiet splash
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
...

Conclusion

The dmesg command is a powerful tool for accessing kernel messages in Unix-like operating systems. In this article, we explored three different use cases of the dmesg command and provided code examples for each. By familiarizing yourself with these different use cases, you can leverage the dmesg command to gain insights into your system’s behavior and troubleshoot issues more effectively.

Related Posts

How to use the command git merge-repo (with examples)

How to use the command git merge-repo (with examples)

Git is a popular version control system that allows developers to track changes and collaborate on projects.

Read More
How to use the command `atrm` (with examples)

How to use the command `atrm` (with examples)

The atrm command allows users to remove jobs that have been scheduled using the at or batch commands.

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

How to use the command gemtopbm (with examples)

The gemtopbm command is used to convert images from the GEM format to the PBM (Portable BitMap) format.

Read More