Linux 中的 dmesg 命令用于获取驱动程序消息
dmesg命令也称为“驱动程序消息”或“显示消息”,用于检查内核环形缓冲区并打印内核的消息缓冲区。此命令的输出包含设备驱动程序生成的消息。
dmesg 的用法:
当计算机启动时,系统启动过程中会产生很多消息(日志)。
因此,您可以使用 dmesg 命令阅读所有这些消息。内核环形缓冲区的内容也存储在/var/log/dmesg文件中。
当系统在启动过程中遇到任何问题时,dmesg 命令会很有用,因此通过阅读 dmesg 命令的内容,您实际上可以找出问题发生的位置(因为系统启动顺序有很多步骤)。
句法 :
dmesg [options]
选项 :
-C –clear : clear the ring buffer.
-c –read-clear : clear the ring buffer after printing its contents.
-D –console-off : disable the messages printing to console.
-E –console-on : Enable printing messages to console.
-F –file file : read the messages from given file.
-h –help : display help text.
-k –kernel : print kernel messages.
-t –notime : do not print kernel’s timestamps.
-u –userspace : print userspace messages.
您可以在此处查看更多选项
由于dmesg命令的输出非常大,因此为了在 dmesg 输出中查找特定信息,最好使用 dmesg 命令和 less 或 grep 命令。
dmesg | less
or
dmesg | grep "text_to_search"
例如 :
这是当我插入 USB 驱动器然后拔下它时dmesg 命令的输出。
这是 dmesg 命令输出的一部分,由于输出非常大,您可以在 Linux 终端上尝试
[ 6982.128179] usb 2-2: New USB device found, idVendor=0930, idProduct=6544
[ 6982.128185] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6982.128188] usb 2-2: Product: DataTraveler 2.0
[ 6982.128190] usb 2-2: Manufacturer: Kingston
[ 6982.128193] usb 2-2: SerialNumber: C86000886407C141DA1401A2
[ 6982.253866] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 6982.254035] scsi host3: usb-storage 2-2:1.0
[ 6982.254716] usbcore: registered new interface driver usb-storage
[ 6982.265103] usbcore: registered new interface driver uas
[ 6983.556572] scsi 3:0:0:0: Direct-Access Kingston DataTraveler 2.0 1.00 PQ: 0 ANSI: 4
[ 6983.557750] sd 3:0:0:0: Attached scsi generic sg1 type 0
[ 6983.557863] sd 3:0:0:0: [sdb] 30310400 512-byte logical blocks: (15.5 GB/14.5 GiB)
[ 6983.558092] sd 3:0:0:0: [sdb] Write Protect is off
[ 6983.558095] sd 3:0:0:0: [sdb] Mode Sense: 45 00 00 00
[ 6983.558314] sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn’t support DPO or FUA
[ 6983.560061] sdb: sdb1
[ 6983.563403] sd 3:0:0:0: [sdb] Attached SCSI removable disk
[ 7045.431954] wlp2s0: disassociated from a0:55:4f:27:bd:01 (Reason: 1)
[ 7049.003277] wlp2s0: authenticate with a0:55:4f:27:bd:01
[ 7049.006680] wlp2s0: send auth to a0:55:4f:27:bd:01 (try 1/3)
[ 7049.015786] wlp2s0: authenticated
[ 7049.021441] wlp2s0: associate with a0:55:4f:27:bd:01 (try 1/3)
[ 7049.038590] wlp2s0: RX AssocResp from a0:55:4f:27:bd:01 (capab=0x431 status=0 aid=140)
[ 7049.043217] wlp2s0: associated
[ 7049.063811] wlp2s0: Limiting TX power to 30 (30 – 0) dBm as advertised by a0:55:4f:27:bd:01
[ 7129.257920] usb 2-2: USB disconnect, device number 3
由于输出总是很大,建议将 dmesg 命令与 grep 命令一起使用。
例如 :
dmesg | grep "usb"
它给出输出
[ 5944.925979] usb 2-1: new low-speed USB device number 2 using xhci_hcd
[ 5945.085658] usb 2-1: New USB device found, idVendor=04d9, idProduct=1702
[ 5945.085663] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 5945.085666] usb 2-1: Product: USB Keyboard
[ 5945.085669] usb 2-1: Manufacturer:
[ 5945.222536] input: USB Keyboard as /devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.0/0003:04D9:1702.0003/input/input19
[ 5945.282554] hid-generic 0003:04D9:1702.0003: input,hidraw2: USB HID v1.10 Keyboard [ USB Keyboard] on usb-0000:00:14.0-1/input0
[ 5945.284803] input: USB Keyboard as /devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.1/0003:04D9:1702.0004/input/input20
[ 5945.342340] hid-generic 0003:04D9:1702.0004: input,hidraw3: USB HID v1.10 Device [ USB Keyboard] on usb-0000:00:14.0-1/input1
[ 6981.985310] usb 2-2: new high-speed USB device number 3 using xhci_hcd
[ 6982.128179] usb 2-2: New USB device found, idVendor=0930, idProduct=6544
[ 6982.128185] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6982.128188] usb 2-2: Product: DataTraveler 2.0
[ 6982.128190] usb 2-2: Manufacturer: Kingston
[ 6982.128193] usb 2-2: SerialNumber: C86000886407C141DA1401A2
[ 6982.253866] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 6982.254035] scsi host3: usb-storage 2-2:1.0
[ 6982.254716] usbcore: registered new interface driver usb-storage
[ 6982.265103] usbcore: registered new interface driver uas
[ 7129.257920] usb 2-2: USB disconnect, device number 3
带选项的输出:
例如 :
dmesg -t
-t 指定带有时间戳的输出。
输出 :
usb 2-2: new high-speed USB device number 3 using xhci_hcd
usb 2-2: New USB device found, idVendor=0930, idProduct=6544
usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 2-2: Product: DataTraveler 2.0
usb 2-2: Manufacturer: Kingston
usb 2-2: SerialNumber: C86000886407C141DA1401A2
usb-storage 2-2:1.0: USB Mass Storage device detected
scsi host3: usb-storage 2-2:1.0
usbcore: registered new interface driver usb-storage
usbcore: registered new interface driver uas
scsi 3:0:0:0: Direct-Access Kingston DataTraveler 2.0 1.00 PQ: 0 ANSI: 4
sd 3:0:0:0: Attached scsi generic sg1 type 0
sd 3:0:0:0: [sdb] 30310400 512-byte logical blocks: (15.5 GB/14.5 GiB)
sd 3:0:0:0: [sdb] Write Protect is off
sd 3:0:0:0: [sdb] Mode Sense: 45 00 00 00
sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn’t support DPO or FUA
sdb: sdb1
sd 3:0:0:0: [sdb] Attached SCSI removable disk
wlp2s0: disassociated from a0:55:4f:27:bd:01 (Reason: 1)
wlp2s0: authenticate with a0:55:4f:27:bd:01
wlp2s0: send auth to a0:55:4f:27:bd:01 (try 1/3)
wlp2s0: authenticated
wlp2s0: associate with a0:55:4f:27:bd:01 (try 1/3)
wlp2s0: RX AssocResp from a0:55:4f:27:bd:01 (capab=0x431 status=0 aid=140)
wlp2s0: associated
wlp2s0: Limiting TX power to 30 (30 – 0) dBm as advertised by a0:55:4f:27:bd:01
usb 2-2: USB disconnect, device number 3
参考 :
1) http://www.linfo.org/dmesg.html
2)维基百科dmesg
– 曼迪普·辛格