0
0
Linux CLIscripting~5 mins

dmesg for kernel messages in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your computer's hardware or system software has important messages about what is happening inside. The dmesg command shows these messages so you can understand if something is working or if there is a problem.
When your computer is not recognizing a new USB device and you want to see what the system says about it.
When you want to check if your hard drive or other hardware had any errors during startup.
When you want to see recent kernel messages after plugging in new hardware.
When troubleshooting why a device driver is not loading correctly.
When you want to monitor system messages related to hardware changes or errors.
Commands
This command shows all the kernel messages stored since the system started. It helps you see hardware and system events.
Terminal
dmesg
Expected OutputExpected
[ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Linux version 5.15.0-50-generic (buildd@lcy02-amd64-027) (gcc (Ubuntu 11.3.0-1ubuntu1) 11.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #56-Ubuntu SMP Wed May 18 15:29:35 UTC 2022 [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.15.0-50-generic root=UUID=1234abcd-56ef-7890-gh12-3456ijklmnop 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 [ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers' [ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers' [ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers' [ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256 [ 0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'compacted' format.
This command shows only the last 10 kernel messages. It helps you quickly see the most recent events without scrolling through everything.
Terminal
dmesg | tail -n 10
Expected OutputExpected
[ 123.456789] usb 1-1: new high-speed USB device number 4 using xhci_hcd [ 123.567890] usb 1-1: New USB device found, idVendor=abcd, idProduct=1234 [ 123.567895] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [ 123.567899] usb 1-1: Product: USB Flash Drive [ 123.567902] usb 1-1: Manufacturer: Generic [ 123.567905] usb 1-1: SerialNumber: 1234567890AB [ 123.568000] usb-storage 1-1:1.0: USB Mass Storage device detected [ 123.568123] scsi host6: usb-storage 1-1:1.0 [ 124.567890] scsi 6:0:0:0: Direct-Access Generic Flash Disk 8.07 PQ: 0 ANSI: 6 [ 124.568000] sd 6:0:0:0: Attached scsi generic sg2 type 0
-n - Show only the last specified number of lines
This command shows kernel messages with human-readable timestamps and filters only messages related to USB devices. It helps you find USB events with clear time info.
Terminal
dmesg --ctime | grep usb
Expected OutputExpected
Thu Jun 1 12:34:56 2023 usb 1-1: new high-speed USB device number 4 using xhci_hcd Thu Jun 1 12:34:56 2023 usb 1-1: New USB device found, idVendor=abcd, idProduct=1234 Thu Jun 1 12:34:56 2023 usb 1-1: Product: USB Flash Drive Thu Jun 1 12:34:56 2023 usb-storage 1-1:1.0: USB Mass Storage device detected
--ctime - Show timestamps in human-readable date and time format
Key Concept

If you remember nothing else from this pattern, remember: dmesg shows system and hardware messages that help you understand what your computer is doing or if something is wrong.

Common Mistakes
Running dmesg without sudo and expecting to see all messages.
Some kernel messages require root permission to view, so you might miss important info.
Run dmesg with sudo (sudo dmesg) to see all kernel messages.
Not using filtering or tail commands and getting overwhelmed by too many messages.
The output can be very long and hard to read, making it difficult to find relevant info.
Use commands like 'dmesg | tail -n 10' or 'dmesg | grep keyword' to focus on recent or relevant messages.
Summary
Use 'dmesg' to view all kernel messages since system start.
Use 'dmesg | tail -n 10' to see the most recent kernel messages quickly.
Use 'dmesg --ctime | grep usb' to find USB-related messages with readable timestamps.