dmesg Linux Command Tutorial with Examples

Saturday, Aug 10, 2024 | 8 minutes read | Update at Saturday, Aug 10, 2024

@ Author Name

Linux dmesg command displays kernel messages which contains lots of information about the system. We can get information about network, file, buffers etc. Here are 30 examples of using the dmesg command in Linux, which is used to examine or control the kernel ring buffer:

  1. Display all kernel messages: The dmesg command dislays all messages from the kernel. Be aware of that this prints lots of detailed messages. As the dmesg command will read kernel logs it requires root privileges which can be provided with the sudo .

    sudo dmesg
    

    This command displays all the kernel messages since the system booted. The output is like below.

     [   11.212834] platform eisa.0: Cannot allocate resource for EISA slot 5
     [   11.212868] platform eisa.0: Cannot allocate resource for EISA slot 6
     [   11.212901] platform eisa.0: Cannot allocate resource for EISA slot 7
     [   11.212934] platform eisa.0: Cannot allocate resource for EISA slot 8
     [   11.212967] platform eisa.0: EISA: Detected 0 cards
     [   11.213026] amd_pstate: the _CPC object is not present in SBIOS or ACPI disabled
     [   11.222841] ledtrig-cpu: registered to indicate activity on CPUs
     [   11.260004] drop_monitor: Initializing network drop monitor service
     [   11.263863] NET: Registered PF_INET6 protocol family
     [   14.364852] Freeing initrd memory: 64080K
     [   14.717172] Segment Routing with IPv6
     [   14.717910] In-situ OAM (IOAM) with IPv6
     [   14.718050] NET: Registered PF_PACKET protocol family
     [   14.721594] Key type dns_resolver registered
     [   14.768407] IPI shorthand broadcast: enabled
     [   14.818184] sched_clock: Marking stable (14270612679, 546966444)->(26042770836, -11225191713)
     [   14.821092] registered taskstats version 1
     [   14.960575] Loading compiled-in X.509 certificates
     [   14.965135] Loaded X.509 cert 'Build time autogenerated kernel key: 06ebe891e0babd04dcef5f73bad66b3a3e5eafa8'
     [   14.966344] Loaded X.509 cert 'Canonical Ltd. Live Patch Signing: 14df34d1a87cf37625abec039ef2bf521249b969'
     [   14.967850] Loaded X.509 cert 'Canonical Ltd. Kernel Module Signing: 88f752e560a1e0737e31163a466ad7b70a850c19'
     [   14.967994] blacklist: Loading compiled-in revocation X.509 certificates
     [   14.968339] Loaded X.509 cert 'Canonical Ltd. Secure Boot Signing: 61482aa2830d0ab2ad5af10b7250da9033ddcef0'
    
  2. Display kernel messages with human-readable timestamps: By default timestamps are printed as seconds which is a little bit confusing. We can print timestamps as human-readable format with the -T option.

    dmesg -T
    

    This adds human-readable timestamps to the output.

    [Fri Aug  9 08:56:50 2024] ahci 0000:02:03.0: version 3.0
     [Fri Aug  9 08:56:50 2024] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input4
     [Fri Aug  9 08:56:50 2024] ahci 0000:02:03.0: AHCI 0001.0300 32 slots 30 ports 6 Gbps 0x3fffffff impl SATA mode
     [Fri Aug  9 08:56:50 2024] ahci 0000:02:03.0: flags: 64bit ncq clo only 
     [Fri Aug  9 08:56:50 2024] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input3
     [Fri Aug  9 08:56:50 2024] Fusion MPT SPI Host driver 3.04.20
     [Fri Aug  9 08:56:50 2024] mptbase: ioc0: Initiating bringup
     [Fri Aug  9 08:56:51 2024] ioc0: LSI53C1030 B0: Capabilities={Initiator}
     [Fri Aug  9 08:56:51 2024] scsi host2: ahci
     [Fri Aug  9 08:56:51 2024] scsi host3: ahci
    
  3. Filter output by a specific keyword (e.g., error): If there is an error related with the system we can look to the dmesg messages by grepping the error like below.

    dmesg | grep error
    

    This displays only messages containing the word “error.”

  4. Display kernel messages in reverse order: By default the kernel messages sorted from oldest to the newest. We can reverse the sort from the newest to oldeste with the tac command.

    dmesg | tac
    

    This shows the most recent messages first.

  5. Display messages related to networking: The dmesg messages are displayed with different annotations. We can only display network related messages with the grep command net pattern.

    dmesg | grep -i net
    

    This filters for messages related to networking.

  6. Save dmesg output to a file: In order to send dmesg messages others or save for the future investigation we can save them into the file with the > by providing the file name.

    dmesg > dmesg_output.txt
    

    This saves the kernel messages to dmesg_output.txt.

  7. Clear the kernel ring buffer: We can clear or remove all dmesg messages with the -C option. This will empty the dmesg and start from scratch.

    sudo dmesg -C
    

    This clears the current kernel ring buffer (requires root privileges).

  8. Display messages related to USB devices: We can display USB related errors, infos, kernel messages with the following command.

    dmesg | grep -i usb
    

    This filters for messages related to USB devices.

  9. Display messages related to a specific device (e.g., sda): We can display sda , disk, partition related errors, infos, problems with the following command.

    dmesg | grep sda
    

    This shows messages related to the sda device (commonly a hard drive).

  10. Show memory-related messages: We can list memory related errors, problems and info with the following command.

    dmesg | grep -i memory
    

    This filters for messages related to memory.

  11. Display only critical messages: The dmesg kernel messages can be in different level for their importance. The highest level is critical and critical messages can be displayed by using the -l option and crit pattern.

    dmesg -l crit
    

    This displays only critical kernel messages.

  12. Display only error messages: We can list error messages easily by using the -l err .

    dmesg -l err
    

    This displays only error messages from the kernel log.

    [    0.770373] [Firmware Bug]: cpu 0, try to use APIC520 (LVT offset 2) for vector 0xf4, but the register is already in use for vector 0x0 on this cpu
    [    0.547966] [Firmware Bug]: cpu 1, try to use APIC520 (LVT offset 2) for vector 0xf4, but the register is already in use for vector 0x0 on this cpu
    [    0.547966] [Firmware Bug]: cpu 2, try to use APIC520 (LVT offset 2) for vector 0xf4, but the register is already in use for vector 0x0 on this cpu
    [    0.547966] [Firmware Bug]: cpu 3, try to use APIC520 (LVT offset 2) for vector 0xf4, but the register is already in use for vector 0x0 on this cpu
    [    0.547966] [Firmware Bug]: cpu 4, try to use APIC520 (LVT offset 2) for vector 0xf4, but the register is already in use for vector 0x0 on this cpu
    [    0.547966] [Firmware Bug]: cpu 5, try to use APIC520 (LVT offset 2) for vector 0xf4, but the register is already in use for vector 0x0 on this cpu
    [   27.216786] piix4_smbus 0000:00:07.3: SMBus Host Controller not enabled!
    
  13. Display only warning messages: We can display warning messages from the dmesg kernel logs with the -l warn option like below.

    dmesg -l warn
    

    This shows only warning messages.

  14. Show messages in a continuous, real-time stream: By default the dmesg dislays messages and stops. We can interactively display dmesg messages in readl-time using the -w .

    dmesg -w
    

    This keeps dmesg open, showing new messages as they come in (useful for real-time monitoring).

  15. Filter for messages related to CPU: We can display cpu or processor related message with the following command.

    dmesg | grep -i cpu
    

    This shows kernel messages related to the CPU.

  16. Display messages with a specific facility (e.g., kern):

    dmesg -f kern
    

    This displays messages from the kernel facility.

  17. Display messages by severity level (e.g., emerg):

    dmesg --level=emerg
    

    This displays messages with the “emerg” severity level (highest priority).

  18. Display only messages after a specific time: We can filter messages according to their time. We can use the --sinceby providing the time like “10 minutes ago” to list only messages from last 10 minutes.

    dmesg --since="10 minutes ago"
    

    This shows only messages logged in the last 10 minutes.

  19. Display only messages before a specific time:

    dmesg --until="1 hour ago"
    

    This shows messages logged until one hour ago.

  20. Count the number of kernel messages: We can count displayed message count with the help of wc command.

    dmesg | wc -l
    

    This counts the number of lines (messages) in the dmesg output.

  21. Display boot messages: The dmesg command can be used to display only boot messages with the following command.

    dmesg | grep -i boot
    

    This filters for messages related to the boot process.

  22. Display kernel version information: We can display kernel version information with the following command.

    dmesg | grep -i 'Linux version'
    

    This shows the kernel version and related information.

  23. Display messages related to hardware changes: Dmesg messages contains hardware related logs and hardware changes and problems can be displayed with the following command.

    dmesg | grep -i hardware
    

    This filters for messages related to hardware changes.

  24. Display messages related to I/O errors:

    dmesg | grep -i 'i/o error'
    

    This shows messages related to I/O errors.

  25. Filter messages by PID (process ID): The dmesg messages also provides processes related logs where we can troubleshoot it easily by providing the PID or process ID.

    dmesg | grep '\[PID\]'
    

    This filters messages by a specific process ID (replace [PID] with the actual PID).

  26. Show disk-related messages: Dmesg display disk realted messages with the following command.

    dmesg | grep -i disk
    

    This filters for messages related to disk activity.

  27. Display kernel messages related to modules:

    dmesg | grep -i module
    

    This shows messages related to kernel modules.

  28. Check for any recent OOM (Out of Memory) kills: Out of Memory error can be tracked with the following command. This command provides detailed information about the Out of Memory error and realted components and processes.

    dmesg | grep -i 'out of memory'
    

    This shows if the kernel has killed any processes due to memory exhaustion.

  29. Monitor dmesg in real-time with a delay: While displaying and monitoring dmesg messages interactively we can delay the display as seconds with the -n option like below.

    watch -n 1 dmesg
    

    This runs dmesg every second, allowing real-time monitoring with a delay.

  30. Display messages related to power management: Power related messages can be displayed with the following command.

    dmesg | grep -i power
    

    This shows messages related to power management.

These examples should cover a wide range of use cases for monitoring and troubleshooting with dmesg in Linux.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.