震驚!同為硬件信息查看“神器”,lshw 和 dmidecode 竟有這么多不同
在Linux中,了解硬件信息的兩個重要工具是lshw和dmidecode。很多人不清楚它們的區(qū)別。今天,我們將解釋這兩個工具的不同之處,幫助你更好地選擇使用哪一個。
一、情報出處大不同
dmidecode就像是一位“直接挖掘者”,它直接從系統(tǒng)的DMI(桌面管理接口)表中“掏”出硬件信息。這個DMI表可厲害啦,硬件制造商、型號、序列號等詳細(xì)內(nèi)容都在里面藏著。而lshw呢,是個“多渠道收集者”,它不僅參考DMI表,還會從/proc目錄下的文件里抓取信息。這好比一個專注一處找線索,一個廣泛收集各處消息,自然得到的信息豐富度會有所差異。比如:
/proc/cpuinfo # 顯示CPU信息
/proc/bus/pci # 顯示pci信息
/proc/scsi # 顯示scsi信息
/proc/net/dev # 顯示網(wǎng)絡(luò)設(shè)備信息
/proc/kcore # 從內(nèi)存映像讀取相關(guān)信息
/proc/ide # 顯示IDE設(shè)備信息
/proc/devices
/proc/mounts
/proc/fstab
默認(rèn)情況下,系統(tǒng)里是沒有裝lshw的。如果你想用這個工具,得先自己安裝一下。lshw可以在各種Linux系統(tǒng)上用,比如Redhat、Centos、Ubuntu、Debian和Arch Linux這些。而dmidecode系統(tǒng)是默認(rèn)安裝的。
二、細(xì)致程度分高低
dmidecode在信息展示上,更側(cè)重于系統(tǒng)級別。BIOS、系統(tǒng)、主板、處理器、內(nèi)存這些方面的信息它能說得頭頭是道。但是,它給的信息范圍相對固定在這些大板塊。lshw可不一樣,那叫一個“事無巨細(xì)”。不僅涵蓋了CPU、內(nèi)存、主板、硬盤、顯卡這些常規(guī)項目,而且輸出的格式十分詳細(xì),就像一位嚴(yán)謹(jǐn)?shù)氖饭伲敿?xì)記錄硬件的各種“大事小情”,甚至還能專門生成一份硬件配置報告,告訴你整個硬件布局的完整故事。
1. lshw用法
lshw [指定格式][指定參數(shù)]
(1) 參數(shù)選項
-class CLASS 僅顯示某一類硬件
-disable TEST 禁用測試(如pci、isapnp、cpuid等)
-enable TEST 啟用測試(如pci、isapnp、cpuid等)
-quiet 不顯示狀態(tài)
-sanitize 凈化輸出(刪除序列等敏感信息)
-numeric 輸出數(shù)字ID(用于PCI、USB等)
-notime 從輸出中排除易失性屬性(時間戳)
-version 顯示lshw的版本并退出。
-X 啟動X11 GUI(如果可用)。
-dump 將收集的信息轉(zhuǎn)儲到文件(SQLite數(shù)據(jù)庫)中。
(2) class列表
memory 內(nèi)存
generic 通用的
display 顯示器
multimedia 多媒體
network 網(wǎng)絡(luò)
storage 存儲
volume 存儲卷
bus 總線
processor 中央處理總線
bridge 網(wǎng)橋
input 輸入
disk 磁盤
system 系統(tǒng)
communication 通訊
(3) 案例演示
顯示硬件設(shè)備列表,輸出總線信息:
root@wtrpro:~# lshw -businfo
Bus info Device Class Description
============================================================
system WTR PRO (Default string)
bus WTR PRO
memory 64KiB BIOS
memory 16GiB System Memory
memory 16GiB SODIMM DDR4 Synchronous 3200 MHz (0.3 ns)
memory 128KiB L1 cache
memory 256KiB L1 cache
memory 2MiB L2 cache
memory 6MiB L3 cache
cpu@0 processor Intel(R) N100
pci@0000:00:00.0 bridge Intel Corporation
pci@0000:00:02.0 display Alder Lake-N [UHD Graphics]
pci@0000:00:0d.0 bus Alder Lake-N Thunderbolt 4 USB Controller
usb@1 usb1 bus xHCI Host Controller
usb@2 usb2 bus xHCI Host Controller
pci@0000:00:14.0 bus Alder Lake-N PCH USB 3.2 xHCI Host Controller
usb@3 usb3 bus xHCI Host Controller
usb@4 usb4 bus xHCI Host Controller
......
顯示內(nèi)存相關(guān)的硬件信息:
root@wtrpro:~# lshw -c memory
*-firmware
description: BIOS
vendor: American Megatrends International, LLC.
physical id: 0
version: HCN100-MI2
date: 06/25/2024
size: 64KiB
capabilities: pci upgrade shadowing cdboot bootselect socketedrom edd acpi biosbootspecification uefi
*-memory
description: System Memory
physical id: 27
slot: System board or motherboard
size: 16GiB
*-bank
description: SODIMM DDR4 Synchronous 3200 MHz (0.3 ns)
product: M471A2K43DB1-CWE
vendor: Samsung
physical id: 0
serial: 8C874D03
slot: Controller0-ChannelA-DIMM0
size: 16GiB
width: 64 bits
clock: 3200MHz (0.3ns)
*-cache:0
description: L1 cache
physical id: 32
slot: L1 Cache
.....
2. dmidecode用法
要用 dmidecode 命令查看 DMI 信息,你通常得用管理員權(quán)限(也就是 root 權(quán)限)來運行它?;镜挠梅ㄊ沁@樣的:
sudo dmidecode [OPTIONS]
(1) 常用參數(shù)
- -t 或 --type TYPE:顯示特定類型的信息,例如處理器、內(nèi)存等。
- -s 或 --string KEYWORD:僅顯示指定 DMI 字符串的值。
- -q 或 --quiet:以安靜模式運行,減少輸出信息。
- -d 或 --dev-mem FILE:從指定的設(shè)備文件讀取內(nèi)存,默認(rèn)是 /dev/mem。
- -V 或 --version:顯示版本信息并退出。
(2) 案例演示
查看Bios:
root@wtrpro:~# dmidecode -t bios
# dmidecode 3.4
Getting SMBIOS data from sysfs.
SMBIOS 3.6.0 present.
# SMBIOS implementations newer than version 3.5.0 are not
# fully supported by this version of dmidecode.
Handle 0x0000, DMI type0, 26 bytes
BIOS Information
Vendor: American Megatrends International, LLC.
Version: HCN100-MI2
Release Date: 06/25/2024
Address: 0xF0000
Runtime Size: 64 kB
ROM Size: 0 MB
Characteristics:
PCI is supported
BIOS is upgradeable
BIOS shadowing is allowed
Boot from CD is supported
Selectable boot is supported
BIOS ROM is socketed
EDD is supported
ACPI is supported
BIOS boot specification is supported
Targeted content distribution is supported
UEFI is supported
BIOS Revision: 5.27
Handle 0x0011, DMI type13, 22 bytes
....
三、輸出格式各千秋
輸出格式上,二者風(fēng)格迥異。dmidecode輸出格式比較“中規(guī)中矩”,以一種相對固定的表格形式呈現(xiàn)信息,簡單直接,一目了然。lshw就靈活多啦,它詳細(xì)的輸出格式不僅能以直觀的方式呈現(xiàn),還很“貼心”地支持生成HTML或XML格式的報告。這樣一來,無論是深入分析還是方便地分享給其他小伙伴,都變得輕而易舉。如下是lshw支持輸出的格式:
-html 將硬件樹輸出為HTML
-xml 將硬件樹輸出為XML
-json 將設(shè)備樹輸出為JSON對象(JavaScript對象表示法)。
-short 輸出顯示硬件路徑的設(shè)備樹,與HP-UX的ioscan的輸出非常相似。
-businfo 輸出顯示總線信息的設(shè)備列表,詳細(xì)說明SCSI、USB、IDE和PCI地址。
以下的案例是將硬件配置信息以html格式輸出
[root@localhost ~]# lshw -html
……
<div class="indented">
<div class="indented">
<table width="100%"class="node"summary="attributes of input:4">
<thead><tr><td class="first">id:</td><td class="second"><div class="id">input:4</div></td></tr></thead>
<tbody>
<tr><td class="first">product: </td><td class="second">spice vdagent tablet</td></tr>
<tr><td class="first">physical id: </td><td class="second"><div class="id">5</div></td></tr>
<tr><td class="first">logical name: </td><td class="second"><div class="id">input8</div></td></tr>
<tr><td class="first">logical name: </td><td class="second"><div class="id">/dev/input/event5</div></td></tr>
<tr><td class="first">logical name: </td><td class="second"><div class="id">/dev/input/js0</div></td></tr>
<tr><td class="first">logical name: </td><td class="second"><div class="id">/dev/input/mouse2</div></td></tr>
</tbody></table></div>
</div>
……
四、擅長場景不一樣
從適用場景的維度來看,dmidecode就像一個“系統(tǒng)級信息專家”,當(dāng)你只需要快速了解系統(tǒng)級別的硬件信息,比如想知道BIOS版本是否要更新、CPU和內(nèi)存的基本參數(shù)時,它就能迅速提供精準(zhǔn)信息。lshw則像是一位“硬件全景大師”,要是你想全面深入地了解整個硬件系統(tǒng)的配置信息,尤其是需要看到詳細(xì)的硬件樹狀圖,來洞悉硬件之間復(fù)雜的架構(gòu)關(guān)系時,那lshw絕對是不二之選。
五、總結(jié)
dmidecode和lshw雖然都是為獲取硬件信息而生,但各有各的“拿手好戲”。前者聚焦系統(tǒng)級信息,簡潔高效;后者放眼整個硬件配置,詳實全面。小伙伴們在實際工作中,就可以根據(jù)自己具體需求,精準(zhǔn)“點兵”啦,讓這兩款工具為咱們的運維管理工作提供最有力的支持,在處理硬件相關(guān)問題時更加得心應(yīng)手。