What is dmesg?
dmesg, short for 'display message' or 'driver message', outputs messages stored in the Linux kernel's Ring Buffer. It chronologically shows how hardware was recognized, how drivers were loaded, or what errors occurred from system boot until now.
Key Functions and Uses
dmesg is used to diagnose what happens at the lowest level of the system (the kernel).
- View Boot Messages: See all kernel messages output during system startup.
- Hardware Diagnostics: Check logs when new hardware (USB, disk, etc.) is connected or removed.
- Driver Errors: Trace the causes of driver loading failures or kernel-level errors (Kernel Panic).
- Ring Buffer: The buffer has a fixed size, so older logs are overwritten by new ones.
Main Options (Shell)
dmesg is often used in conjunction with other commands (grep, less) via pipes (|), but it also has its own useful options.
1. Output Format and Time
2. Filtering and Real-time Monitoring
3. Buffer/Console Control (Caution Required)
Generated command:
Try combining the commands.
Description:
`dmesg` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Scenario Examples (Shell)
dmesg is often used with grep to filter out only the desired information.
View All Messages (Paging)
dmesg | less
Most basic usage. Scroll comfortably with less.
Check New Messages in Real-time (Human-readable)
dmesg -wH
Combines -w (real-time) and -H (human-readable time).
Filter USB-related Messages Only
dmesg | grep -i usb
grep's -i option ignores case.
Filter Disk (SATA/NVMe) Related Messages
dmesg | grep -iE 'sd[a-z]|nvme'
Use grep -E (extended regex) to find logs containing 'sd[a-z]' or 'nvme'.
Filter Only Errors or Warnings
dmesg -H -l err,warn
Specify log levels with the -l option.
View Only the Last 10 Lines of Messages
dmesg | tail -n 10
Combine with tail to quickly check recent messages.
View with Facility/Level Tags
dmesg -xT | less
The -x option decodes facility/level to aid understanding.
Save Logs to a File
dmesg -T > ~/dmesg-$(date +%F-%H%M).log
Preserve the entire dmesg output immediately after boot.
Installation
dmesg is included in the util-linux package and is provided by default in most Linux distributions.
Installed by Default
No separate installation is required. If it's missing, install the 'util-linux' package for your distribution.
Tips & Cautions
dmesg vs journalctl
- systemd Environment: In modern systemd systems, 'journalctl -k' (kernel logs) or 'journalctl -b' (logs for the current boot) allows integrated viewing of system logs beyond dmesg's scope.
- Time Format: The default dmesg output time is [elapsed time in seconds since boot]. Use the '-H' or '-T' options to see actual timestamps.
- Buffer Overwriting: Due to the nature of the ring buffer, older messages can disappear. If you need to preserve early boot logs, check with 'journalctl -b 0' or save dmesg output to a file.
- Permissions/Security: Some distributions restrict general user access to dmesg with '/proc/sys/kernel/dmesg_restrict=1'. In such cases, use 'sudo dmesg' or 'sudo journalctl -k'.
- Clearing Logs (-c, -C): 'dmesg -c' and '-C' are useful for debugging, but once cleared, logs cannot be recovered. Use them with caution in production environments.