一、dmesg命令简介
dmesg是一个Linux系统命令,用来查看内核和硬件的消息。它可以显示系统引导时的所有消息,以及在运行期间发生的任何硬件或内核更改的消息。这个命令可以帮助你查找系统出现问题的原因,或者是为了诊断硬件问题。
$ dmesg
它会输出大量信息,您可以使用管道和grep来过滤输出。
$ dmesg | grep usb [ 0.831358] usbcore: registered new interface driver usbfs [ 0.831366] usbcore: registered new interface driver hub [ 0.831380] usbcore: registered new device driver usb [ 1.105681] usb 1-1: new high-speed USB device number 2 using ehci-pci [ 1.673905] usb 2-1: new high-speed USB device number 2 using ehci-pci [ 1.759759] input: USB Optical Mouse as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0/0003:046D:C077.0001/input/input2
二、查看系统启动过程中的消息
dmesg命令可以用来查看系统启动过程中的消息。这些消息在内核初始化时输出,并且是重要的调试工具。您可以使用命令单独查看启动时的消息:
$ dmesg | grep -i "boot" [ 0.000000] microcode: microcode updated early to revision 0x1e, date = 2018-11-29 [ 0.000000] Linux version 5.4.0-28-generic (buildd@lgw01-amd64-045) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #32-Ubuntu SMP Wed Apr 22 17:40:10 UTC 2020 (Ubuntu 5.4.0-28.32-generic 5.4.30) [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-28-generic root=UUID=81e8a3e5-010c-4d75-9c45-8d1ed97a01c6 ro quiet splash vt.handoff=7
三、查看硬件相关消息
dmesg命令也可以用来查看硬件相关的消息。例如,如果您想查看您的系统是否识别了某个USB设备,您可以使用以下命令:
$ dmesg | grep -i "usb" [ 0.831358] usbcore: registered new interface driver usbfs [ 0.831366] usbcore: registered new interface driver hub [ 0.831380] usbcore: registered new device driver usb [ 1.105681] usb 1-1: new high-speed USB device number 2 using ehci-pci [ 1.673905] usb 2-1: new high-speed USB device number 2 using ehci-pci [ 1.759759] input: USB Optical Mouse as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1:1.0/0003:046D:C077.0001/input/input2
四、查看内核相关消息
dmesg还可以用来查看与内核相关的消息。例如,您可以使用以下命令来查看内核模块的输出:
$ dmesg | grep -i "module" [ 0.000000] Linux version 5.4.0-28-generic (buildd@lgw01-amd64-045) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #32-Ubuntu SMP Wed Apr 22 17:40:10 UTC 2020 (Ubuntu 5.4.0-28.32-generic 5.4.30) [ 0.097157] ACPI: Added _OSI(Module Device) [ 6.667433] snd_hda_codec_hdmi hdaudioC1D0: No i915 binding for Intel HDMI/DP codec [ 7.336413] vboxdrv: module verification failed: signature and/or required key missing - tainting kernel [ 7.438341] tpm_tis 00:04: 1.2 TPM (device-id 0xB, rev-id 16) [ 13.363908] vboxpci: IOMMU not found (not registered)
五、查看内存相关消息
dmesg也可以用于查看有关内存的消息。例如,您可以使用以下命令来查看系统的内存配置:
$ dmesg | grep -i "memory" [ 0.000000] e820: BIOS-provided physical RAM map: [ 0.000000] e820: mapped low 64KB of RAM reserve for EL3/Secure world [ 0.000000] e820: [mem 0x0000000000001000-0x0000000000085fff] usable [ 0.000000] e820: [mem 0x0000000000086000-0x0000000000086fff] reserved [ 0.000000] e820: [mem 0x0000000000087000-0x000000000009dfff] usable [ 0.000000] e820: [mem 0x000000000009e000-0x000000000009ffff] reserved
总结
dmesg是一个非常有用的命令,可以帮助您查看系统内核和硬件相关信息。通过使用不同的选项和过滤器,您可以很容易地找到自己需要的信息。如果您是Linux系统管理员或运维人员,了解和掌握这个命令会让您在日常工作中更加高效。