歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux系統中‘dmesg’命令處理故障和收集系統信息的7種用法

Linux系統中‘dmesg’命令處理故障和收集系統信息的7種用法

日期:2017/2/28 14:29:01   编辑:Linux教程

‘dmesg’命令顯示linux內核的環形緩沖區信息,我們可以從中獲得諸如系統架構、cpu、掛載的硬件,RAM等多個運行級別的大量的系統信息。當計算機啟動時,系統內核(操作系統的核心部分)將會被加載到內存中。在加載的過程中會顯示很多的信息,在這些信息中我們可以看到內核檢測硬件設備。

dmesg 命令的使用范例

‘dmesg’命令設備故障的診斷是非常重要的。在‘dmesg’命令的幫助下進行硬件的連接或斷開連接操作時,我們可以看到硬件的檢測或者斷開連接的信息。‘dmesg’命令在多數基於LinuxUnix的操作系統中都可以使用。

下面我們展示一些最負盛名的‘dmesg’命令工具以及其實際使用舉例。‘dmesg’命令的使用語法如下。

  1. # dmesg [options...]

1. 列出加載到內核中的所有驅動

我們可以使用如‘more’。 ‘tail’, ‘less ’或者‘grep’文字處理工具來處理‘dmesg’命令的輸出。由於dmesg日志的輸出不適合在一頁中完全顯示,因此我們使用管道(pipe)將其輸出送到more或者less命令單頁顯示。

  1. [[email protected] ~]# dmesg | more
  2. [[email protected] ~]# 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.0-13-generic(buildd@aatxe)(gcc version 4.8.1(Ubuntu/Linaro4.8.1-10ubuntu8))#20-Ubuntu SMP Wed Oct 23 17:26:33 UTC 2013
  5. (Ubuntu3.11.0-13.20-generic3.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: BIOS-provided physical RAM map:
  16. [0.000000] BIOS-e820:[mem 0x0000000000000000-0x000000000009fbff] usable
  17. [0.000000] BIOS-e820:[mem 0x00000000000f0000-0x00000000000fffff] reserved
  18. [0.000000] BIOS-e820:[mem 0x0000000000100000-0x000000007dc08bff] usable
  19. [0.000000] BIOS-e820:[mem 0x000000007dc08c00-0x000000007dc5cbff] ACPI NVS
  20. [0.000000] BIOS-e820:[mem 0x000000007dc5cc00-0x000000007dc5ebff] ACPI data
  21. [0.000000] BIOS-e820:[mem 0x000000007dc5ec00-0x000000007fffffff] reserved
  22. [0.000000] BIOS-e820:[mem 0x00000000e0000000-0x00000000efffffff] reserved
  23. [0.000000] BIOS-e820:[mem 0x00000000fec00000-0x00000000fed003ff] reserved
  24. [0.000000] BIOS-e820:[mem 0x00000000fed20000-0x00000000fed9ffff] reserved
  25. [0.000000] BIOS-e820:[mem 0x00000000fee00000-0x00000000feefffff] reserved
  26. [0.000000] BIOS-e820:[mem 0x00000000ffb00000-0x00000000ffffffff] reserved
  27. [0.000000] NX (ExecuteDisable) protection: active
  28. .....

2. 列出所有被檢測到的硬件

要顯示所有被內核檢測到的硬盤設備,你可以使用‘grep’命令搜索‘sda’關鍵詞,如下:

  1. [[email protected] ~]# dmesg | grep sda
  2. [1.280971] sd 2:0:0:0:[sda]488281250512-byte 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. [[email protected] ~]# 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.0-13-generic(buildd@aatxe)(gcc version 4.8.1(Ubuntu/Linaro4.8.1-10ubuntu8))#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: BIOS-provided physical RAM map:
  16. [0.000000] BIOS-e820:[mem 0x0000000000000000-0x000000000009fbff] usable
  17. [0.000000] BIOS-e820:[mem 0x00000000000f0000-0x00000000000fffff] reserved
  18. [0.000000] BIOS-e820:[mem 0x0000000000100000-0x000000007dc08bff] usable
  19. [0.000000] BIOS-e820:[mem 0x000000007dc08c00-0x000000007dc5cbff] ACPI NVS
  20. [0.000000] BIOS-e820:[mem 0x000000007dc5cc00-0x000000007dc5ebff] ACPI data
  21. [0.000000] BIOS-e820:[mem 0x000000007dc5ec00-0x000000007fffffff] reserved

4. 只輸出dmesg命令最後20行日志

在‘dmesg’命令後跟隨‘tail’命令(‘ dmesg | tail -20’)來輸出‘dmesg’命令的最後20行日志,當你插入可移動設備時它是非常有用的。

  1. [[email protected] ~]# dmesg | tail -20
  2. parport0: PC-style at 0x378, irq 7[PCSPP,TRISTATE]
  3. ppdev: user-space parallel port driver
  4. EXT4-fs (sda1): mounted filesystem with ordered data mode
  5. Adding2097144k swap on /dev/sda2.Priority:-1 extents:1 across:2097144k
  6. readahead-disable-service: delaying service auditd
  7. ip_tables:(C)2000-2006NetfilterCoreTeam
  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. FS-Cache: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. readahead-collector: starting delayed service auditd
  20. readahead-collector: sorting
  21. readahead-collector: finished

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

由於‘dmesg’命令的輸出實在太長了,在其中搜索某個特定的字符串是非常困難的。因此,有必要過濾出一些包含‘usb’ ‘dma’ ‘tty’ ‘memory’等字符串的日志行。grep 命令 的‘-i’選項表示忽略大小寫。

grep使用簡明及正則表達式 http://www.linuxidc.com/Linux/2013-08/88534.htm

  1. [[email protected] log]# dmesg | grep -i usb
  2. [[email protected] log]# dmesg | grep -i dma
  3. [[email protected] log]# dmesg | grep -i tty
  4. [[email protected] log]# dmesg | grep -i memory

輸出

  1. [0.000000]Scanning1 areas for low memory corruption
  2. [0.000000] initial memory mapped:[mem 0x00000000-0x01ffffff]
  3. [0.000000]Base memory trampoline at [c009b000]9b000 size 16384
  4. [0.000000] init_memory_mapping:[mem 0x00000000-0x000fffff]
  5. [0.000000] init_memory_mapping:[mem 0x37800000-0x379fffff]
  6. [0.000000] init_memory_mapping:[mem 0x34000000-0x377fffff]
  7. [0.000000] init_memory_mapping:[mem 0x00100000-0x33ffffff]
  8. [0.000000] init_memory_mapping:[mem 0x37a00000-0x37bfdfff]
  9. [0.000000]Early memory node ranges
  10. [0.000000] PM:Registered nosave memory:[mem 0x0009f000-0x000effff]
  11. [0.000000] PM:Registered nosave memory:[mem 0x000f0000-0x000fffff]
  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. [[email protected] log]# dmesg -c

7. 實時監控dmesg日志輸出

在某些發行版中可以使用命令‘tail -f /var/log/dmesg’來實時監控dmesg的日志輸出。

  1. [[email protected] 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知識和自如地運用新技術。

Copyright © Linux教程網 All Rights Reserved