在‘dmesg’命令后跟随‘tail’命令(‘ dmesg | tail -20’)来输出‘dmesg’命令的最后20行日志,当你插入可移动设备时它是非常有用的。
linuxidc@linuxidc:~/linuxidc.com$ dmesg | tail -20
[ 12.162939] RAPL PMU: hw unit of domain dram 2^-0 Joules
[ 12.162939] RAPL PMU: hw unit of domain pp1-gpu 2^-0 Joules
[ 12.162940] RAPL PMU: hw unit of domain psys 2^-0 Joules
[ 12.429921] cryptd: max_cpu_qlen set to 1000
[ 12.536473] AVX2 version of gcm_enc/dec engaged.
[ 12.536474] AES CTR mode by8 optimization enabled
[ 12.732314] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 12.732315] Bluetooth: BNEP filters: protocol multicast
[ 12.732319] Bluetooth: BNEP socket layer initialized
[ 20.858894] [drm:vmw_stdu_crtc_page_flip [vmwgfx]] *ERROR* Page flip error -16.
[ 114.030097] sched: RT throttling activated
[ 114.092905] Bluetooth: RFCOMM TTY layer initialized
[ 114.092909] Bluetooth: RFCOMM socket layer initialized
[ 114.092912] Bluetooth: RFCOMM ver 1.11
[ 115.876999] rfkill: input handler disabled
[ 117.336287] show_signal_msg: 28 callbacks suppressed
[ 117.336290] vmtoolsd[2585]: segfault at 1710 ip 0000000000001710 sp 00007ffc7af6abe8 error 14 in vmtoolsd[5631cce9d000+ac000]
[ 117.336294] Code: Bad RIP value.
[ 121.476306] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 121.487278] ISO 9660 Extensions: RRIP_1991A
5. 搜索包含特定字符串的被检测到的硬件
由于‘dmesg’命令的输出实在太长了,在其中搜索某个特定的字符串是非常困难的。因此,有必要过滤出一些包含‘usb’ ‘dma’ ‘tty’ ‘memory’等字符串的日志行。grep 命令 的‘-i’选项表示忽略大小写。
linuxidc@linuxidc:~/linuxidc.com$ dmesg | grep -i usb
linuxidc@linuxidc:~/linuxidc.com$ dmesg | grep -i dma
[ 0.000000] DMA [mem 0x0000000000001000-0x0000000000ffffff]
[ 0.000000] DMA32 [mem 0x0000000001000000-0x00000000ffffffff]
[ 0.000000] DMA zone: 64 pages used for memmap
[ 0.000000] DMA zone: 21 pages reserved
[ 0.000000] DMA zone: 3997 pages, LIFO batch:0
[ 0.000000] DMA32 zone: 12224 pages used for memmap
[ 0.000000] DMA32 zone: 782288 pages, LIFO batch:31
[ 2.009904] PCI-DMA: Using software bounce buffering for IO (SWIOTLB)
[ 2.116724] ata1: PATA max UDMA/33 cmd 0x1f0 ctl 0x3f6 bmdma 0x1060 irq 14
[ 2.116725] ata2: PATA max UDMA/33 cmd 0x170 ctl 0x376 bmdma 0x1068 irq 15
[ 2.880708] [drm] DMA map mode: Using physical TTM page addresses.
[ 2.884723] [TTM] Initializing DMA pool allocator
[ 2.922240] [drm] Using command buffers with DMA pool.
linuxidc@linuxidc:~/linuxidc.com$ grep -i tty
linuxidc@linuxidc:~/linuxidc.com$ dmesg | grep -i memory
6. 清空dmesg缓冲区日志
我们可以使用如下命令来清空dmesg的日志。该命令会清空dmesg环形缓冲区中的日志。但是你依然可以查看存储在‘/var/log/dmesg’文件中的日志。你连接任何的设备都会产生dmesg日志输出。
linuxidc@linuxidc:~/linuxidc.com$ dmesg -c
[ 0.000000] Linux version 4.18.0-17-generic (buildd@lgw01-amd64-021) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #18~18.04.1-Ubuntu SMP Fri Mar 15 15:27:12 UTC 2019 (Ubuntu 4.18.0-17.18~18.04.1-generic 4.18.20)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.18.0-17-generic root=UUID=21f64724-a9be-43a7-8e83-2e7ffd1ba2a3 ro quiet splash
7. 实时监控dmesg日志输出
在某些发行版中可以使用命令‘tail -f /var/log/dmesg’来实时监控dmesg的日志输出。
linuxidc@linuxidc:~/linuxidc.com$ watch "dmesg | tail -20"
结论:dmesg命令在系统dmesg记录实时更改或产生的情况下是非常有用的。你可以使用man dmesg来获取更多关于dmesg的信息。
2019年,大多数Linux发行版仍然没有限制Dmesg的访问 https://www.linuxidc.com/Linux/2019-04/158222.htm