感谢支持
我们一直在努力

Linux系统中‘dmesg’命令处理故障和收集系统信息的7种用法

‘dmesg’命令显示linux内核的环形缓冲区信息,我们可以从中获得诸如系统架构、cpu、挂载的硬件,RAM等多个运行级别的大量的系统信息。当计算机启动时,系统内核(操作系统的核心部分)将会被加载到内存中。在加载的过程中会显示很多的信息,在这些信息中我们可以看到内核检测硬件设备。

dmesg Command Examples

dmesg 命令的使用范例

‘dmesg’命令设备故障的诊断是非常重要的。在‘dmesg’命令的帮助下进行硬件的连接或断开连接操作时,我们可以看到硬件的检测或者断开连接的信息。‘dmesg’命令在多数基于LinuxUnix的操作系统中都可以使用。

下面我们展示一些最负盛名的‘dmesg’命令工具以及其实际使用举例。‘dmesg’命令的使用语法如下。

  1. # dmesg [options…]

1. 列出加载到内核中的所有驱动

我们可以使用如‘more’。 ‘tail’, ‘less ’或者‘grep’文字处理工具来处理‘dmesg’命令的输出。由于dmesg日志的输出不适合在一页中完全显示,因此我们使用管道(pipe)将其输出送到more或者less命令单页显示。

  1. [root@tecmint.com ~]# dmesg | more
  2. [root@tecmint.com ~]# dmesg | less

输出

  1. [0.000000]Initializing cgroup subsys cpuset
  2. [0.000000]Initializing cgroup subsys cpu
  3. [0.000000]Initializing cgroup subsys cpuacct
  4. [0.000000]Linux version 3.11.013generic(buildd@aatxe)(gcc version 4.8.1(Ubuntu/Linaro4.8.110ubuntu8))#20-Ubuntu SMP Wed Oct 23 17:26:33 UTC 2013
  5. (Ubuntu3.11.013.20generic3.11.6)
  6. [0.000000] KERNEL supported cpus:
  7. [0.000000]IntelGenuineIntel
  8. [0.000000] AMD AuthenticAMD
  9. [0.000000] NSC Geodeby NSC
  10. [0.000000]CyrixCyrixInstead
  11. [0.000000]CentaurCentaurHauls
  12. [0.000000]TransmetaGenuineTMx86
  13. [0.000000]TransmetaTransmetaCPU
  14. [0.000000] UMC UMC UMC UMC
  15. [0.000000] e820: BIOSprovided physical RAM map:
  16. [0.000000] BIOSe820:[mem 0x00000000000000000x000000000009fbff] usable
  17. [0.000000] BIOSe820:[mem 0x00000000000f00000x00000000000fffff] reserved
  18. [0.000000] BIOSe820:[mem 0x00000000001000000x000000007dc08bff] usable
  19. [0.000000] BIOSe820:[mem 0x000000007dc08c000x000000007dc5cbff] ACPI NVS
  20. [0.000000] BIOSe820:[mem 0x000000007dc5cc000x000000007dc5ebff] ACPI data
  21. [0.000000] BIOSe820:[mem 0x000000007dc5ec000x000000007fffffff] reserved
  22. [0.000000] BIOSe820:[mem 0x00000000e00000000x00000000efffffff] reserved
  23. [0.000000] BIOSe820:[mem 0x00000000fec000000x00000000fed003ff] reserved
  24. [0.000000] BIOSe820:[mem 0x00000000fed200000x00000000fed9ffff] reserved
  25. [0.000000] BIOSe820:[mem 0x00000000fee000000x00000000feefffff] reserved
  26. [0.000000] BIOSe820:[mem 0x00000000ffb000000x00000000ffffffff] reserved
  27. [0.000000] NX (ExecuteDisable) protection: active
  28. …..

2. 列出所有被检测到的硬件

要显示所有被内核检测到的硬盘设备,你可以使用‘grep’命令搜索‘sda’关键词,如下:

  1. [root@tecmint.com ~]# dmesg | grep sda
  2. [1.280971] sd 2:0:0:0:[sda]488281250512byte logical blocks:(250 GB/232GiB)
  3. [1.281014] sd 2:0:0:0:[sda]WriteProtectis off
  4. [1.281016] sd 2:0:0:0:[sda]ModeSense:003a0000
  5. [1.281039] sd 2:0:0:0:[sda]Write cache: enabled, read cache: enabled, doesn‘t support DPO or FUA
  6. [ 1.359585] sda: sda1 sda2 < sda5 sda6 sda7 sda8 >
  7. [ 1.360052] sd 2:0:0:0: [sda] Attached SCSI disk
  8. [ 2.347887] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)
  9. [ 22.928440] Adding 3905532k swap on /dev/sda6. Priority:-1 extents:1 across:3905532k FS
  10. [ 23.950543] EXT4-fs (sda1): re-mounted. Opts: errors=remount-ro
  11. [ 24.134016] EXT4-fs (sda5): mounted filesystem with ordered data mode. Opts: (null)
  12. [ 24.330762] EXT4-fs (sda7): mounted filesystem with ordered data mode. Opts: (null)
  13. [ 24.561015] EXT4-fs (sda8): mounted filesystem with ordered data mode. Opts: (null)

注解 ‘sda’表示第一块 SATA硬盘,‘sdb’表示第二块SATA硬盘。若想查看IDE硬盘搜索‘hda’或‘hdb’关键词。

3. 只输出dmesg命令的前20行日志

在‘dmesg’命令后跟随‘head’命令来显示开始几行,‘dmesg | head -20′命令将显示开始的前20行。

  1. [root@tecmint.com ~]# dmesg | head 20
  2. [0.000000]Initializing cgroup subsys cpuset
  3. [0.000000]Initializing cgroup subsys cpu
  4. [0.000000]Initializing cgroup subsys cpuacct
  5. [0.000000]Linux version 3.11.013generic(buildd@aatxe)(gcc version 4.8.1(Ubuntu/Linaro4.8.110ubuntu8))#20-Ubuntu SMP Wed Oct 23 17:26:33 UTC 2013 (Ubuntu 3.11.0-13.20-generic 3.11.6)
  6. [0.000000] KERNEL supported cpus:
  7. [0.000000]IntelGenuineIntel
  8. [0.000000] AMD AuthenticAMD
  9. [0.000000] NSC Geodeby NSC
  10. [0.000000]CyrixCyrixInstead
  11. [0.000000]CentaurCentaurHauls
  12. [0.000000]TransmetaGenuineTMx86
  13. [0.000000]TransmetaTransmetaCPU
  14. [0.000000] UMC UMC UMC UMC
  15. [0.000000] e820: BIOSprovided physical RAM map:
  16. [0.000000] BIOSe820:[mem 0x00000000000000000x000000000009fbff] usable
  17. [0.000000] BIOSe820:[mem 0x00000000000f00000x00000000000fffff] reserved
  18. [0.000000] BIOSe820:[mem 0x00000000001000000x000000007dc08bff] usable
  19. [0.000000] BIOSe820:[mem 0x000000007dc08c000x000000007dc5cbff] ACPI NVS
  20. [0.000000] BIOSe820:[mem 0x000000007dc5cc000x000000007dc5ebff] ACPI data
  21. [0.000000] BIOSe820:[mem 0x000000007dc5ec000x000000007fffffff] reserved

4. 只输出dmesg命令最后20行日志

在‘dmesg’命令后跟随‘tail’命令(‘ dmesg | tail -20’)来输出‘dmesg’命令的最后20行日志,当你插入可移动设备时它是非常有用的。

  1. [root@tecmint.com ~]# dmesg | tail 20
  2. parport0: PCstyle at 0x378, irq 7[PCSPP,TRISTATE]
  3. ppdev: userspace parallel port driver
  4. EXT4fs (sda1): mounted filesystem with ordered data mode
  5. Adding2097144k swap on /dev/sda2.Priority:-1 extents:1 across:2097144k
  6. readaheaddisableservice: delaying service auditd
  7. ip_tables:(C)20002006NetfilterCoreTeam
  8. nf_conntrack version 0.5.0(16384 buckets,65536 max)
  9. NET:Registered protocol family 10
  10. lo:DisabledPrivacyExtensions
  11. e1000: eth0 NIC LinkisUp1000MbpsFullDuplex,FlowControl:None
  12. Slow work thread pool:Starting up
  13. Slow work thread pool:Ready
  14. FSCache:Loaded
  15. CacheFiles:Loaded
  16. CacheFiles:Security denies permission to nominate security context: error 95
  17. eth0:noIPv6 routers present
  18. type=1305 audit(1398268784.593:18630): audit_enabled=0 old=1 auid=4294967295 ses=4294967295 res=1
  19. readaheadcollector: starting delayed service auditd
  20. readaheadcollector: sorting
  21. readaheadcollector: finished

5. 搜索包含特定字符串的被检测到的硬件

由于‘dmesg’命令的输出实在太长了,在其中搜索某个特定的字符串是非常困难的。因此,有必要过滤出一些包含‘usb’ ‘dma’ ‘tty’ ‘memory’等字符串的日志行。grep 命令 的‘-i’选项表示忽略大小写。

grep使用简明及正则表达式 http://www.linuxidc.com/Linux/2013-08/88534.htm

  1. [root@tecmint.com log]# dmesg | grep i usb
  2. [root@tecmint.com log]# dmesg | grep i dma
  3. [root@tecmint.com log]# dmesg | grep i tty
  4. [root@tecmint.com log]# dmesg | grep i memory

输出

  1. [0.000000]Scanning1 areas for low memory corruption
  2. [0.000000] initial memory mapped:[mem 0x000000000x01ffffff]
  3. [0.000000]Base memory trampoline at [c009b000]9b000 size 16384
  4. [0.000000] init_memory_mapping:[mem 0x000000000x000fffff]
  5. [0.000000] init_memory_mapping:[mem 0x378000000x379fffff]
  6. [0.000000] init_memory_mapping:[mem 0x340000000x377fffff]
  7. [0.000000] init_memory_mapping:[mem 0x001000000x33ffffff]
  8. [0.000000] init_memory_mapping:[mem 0x37a000000x37bfdfff]
  9. [0.000000]Early memory node ranges
  10. [0.000000] PM:Registered nosave memory:[mem 0x0009f0000x000effff]
  11. [0.000000] PM:Registered nosave memory:[mem 0x000f00000x000fffff]
  12. [0.000000] please try‘cgroup_disable=memory’ option if you don‘t want memory cgroups
  13. [ 0.000000] Memory: 2003288K/2059928K available (6352K kernel code, 607K rwdata, 2640K rodata, 880K init, 908K bss, 56640K reserved, 1146920K highmem)
  14. [ 0.000000] virtual kernel memory layout:
  15. [ 0.004291] Initializing cgroup subsys memory
  16. [ 0.004609] Freeing SMP alternatives memory: 28K (c1a3e000 – c1a45000)
  17. [ 0.899622] Freeing initrd memory: 23616K (f51d0000 – f68e0000)
  18. [ 0.899813] Scanning for low memory corruption every 60 seconds
  19. [ 0.946323] agpgart-intel 0000:00:00.0: detected 32768K stolen memory
  20. [ 1.360318] Freeing unused kernel memory: 880K (c1962000 – c1a3e000)
  21. [ 1.429066] [drm] Memory usable by graphics device = 2048M

6. 清空dmesg缓冲区日志

我们可以使用如下命令来清空dmesg的日志。该命令会清空dmesg环形缓冲区中的日志。但是你依然可以查看存储在‘/var/log/dmesg’文件中的日志。你连接任何的设备都会产生dmesg日志输出。

  1. [root@tecmint.com log]# dmesg c

7. 实时监控dmesg日志输出

在某些发行版中可以使用命令‘tail -f /var/log/dmesg’来实时监控dmesg的日志输出。

  1. [root@tecmint.com log]# watch “dmesg | tail -20”

结论:dmesg命令在系统dmesg记录实时更改或产生的情况下是非常有用的。你可以使用man dmesg来获取更多关于dmesg的信息。

不同应用场景的10个Linux面试问题与解答 http://www.linuxidc.com/Linux/2014-04/99710.htm

10个核心的Linux面试问题与答案 http://www.linuxidc.com/Linux/2014-04/100447.htm

 


Narad Shrestha

  • Twitter profile
  • Facebook profile
  • Google+ profile

他在IT领域拥有超过10年的丰富经验,其中包括各种Linux发行版,开源软件和网络工作。 Narad始终坚持与他人分享IT知识和自如地运用新技术。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105429.htm

赞(0) 打赏
转载请注明出处:服务器评测 » Linux系统中‘dmesg’命令处理故障和收集系统信息的7种用法
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏