震惊!同为硬件信息查看“神器”,lshw 和 dmidecode 竟有这么多不同
在Linux
中,了解硬件信息的两个重要工具是lshw
和dmidecode
。很多人不清楚它们的区别。今天,我们将解释这两个工具的不同之处,帮助你更好地选择使用哪一个。
情报出处大不同
dmidecode
就像是一位“直接挖掘者”,它直接从系统的DMI
(桌面管理接口)表中“掏”出硬件信息。这个DMI
表可厉害啦,硬件制造商、型号、序列号等详细内容都在里面藏着。而lshw
呢,是个“多渠道收集者”,它不仅参考DMI
表,还会从/proc
目录下的文件里抓取信息。这好比一个专注一处找线索,一个广泛收集各处消息,自然得到的信息丰富度会有所差异。比如:
/proc/cpuinfo # 显示CPU信息
/proc/bus/pci # 显示pci信息
/proc/scsi # 显示scsi信息
/proc/net/dev # 显示网络设备信息
/proc/kcore # 从内存映像读取相关信息
/proc/ide # 显示IDE设备信息
/proc/devices
/proc/mounts
/proc/fstab
默认情况下,系统里是没有装lshw
的。如果你想用这个工具,得先自己安装一下。lshw
可以在各种Linux
系统上用,比如Redhat
、Centos
、Ubuntu
、Debian
和Arch Linux
这些。而dmidecode
系统是默认安装的。
细致程度分高低
dmidecode
在信息展示上,更侧重于系统级别。BIOS
、系统、主板、处理器、内存这些方面的信息它能说得头头是道。但是,它给的信息范围相对固定在这些大板块。lshw
可不一样,那叫一个“事无巨细”。不仅涵盖了CPU
、内存、主板、硬盘、显卡这些常规项目,而且输出的格式十分详细,就像一位严谨的史官,详细记录硬件的各种“大事小情”,甚至还能专门生成一份硬件配置报告,告诉你整个硬件布局的完整故事。
lshw用法
lshw [指定格式] [指定参数]
参数选项
-class CLASS 仅显示某一类硬件
-disable TEST 禁用测试(如pci、isapnp、cpuid等)
-enable TEST 启用测试(如pci、isapnp、cpuid等)
-quiet 不显示状态
-sanitize 净化输出(删除序列等敏感信息)
-numeric 输出数字ID(用于PCI、USB等)
-notime 从输出中排除易失性属性(时间戳)
-version 显示lshw的版本并退出。
-X 启动X11 GUI(如果可用)。
-dump 将收集的信息转储到文件(SQLite数据库)中。
class列表
memory 内存
generic 通用的
display 显示器
multimedia 多媒体
network 网络
storage 存储
volume 存储卷
bus 总线
processor 中央处理总线
bridge 网桥
input 输入
disk 磁盘
system 系统
communication 通讯
案例演示
显示硬件设备列表,输出总线信息
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
......
显示内存相关的硬件信息
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
.....
dmidecode用法
要用 dmidecode 命令查看 DMI 信息,你通常得用管理员权限(也就是 root 权限)来运行它。基本的用法是这样的:
sudo dmidecode [OPTIONS]
常用参数
- -t 或 --type TYPE:显示特定类型的信息,例如处理器、内存等。
- -s 或 --string KEYWORD:仅显示指定 DMI 字符串的值。
- -q 或 --quiet:以安静模式运行,减少输出信息。
- -d 或 --dev-mem FILE:从指定的设备文件读取内存,默认是 /dev/mem。
- -V 或 --version:显示版本信息并退出。
案例演示
- 查看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 type 0, 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 type 13, 22 bytes
....
输出格式各千秋
输出格式上,二者风格迥异。dmidecode
输出格式比较“中规中矩”,以一种相对固定的表格形式呈现信息,简单直接,一目了然。lshw
就灵活多啦,它详细的输出格式不仅能以直观的方式呈现,还很“贴心”地支持生成HTML
或XML
格式的报告。这样一来,无论是深入分析还是方便地分享给其他小伙伴,都变得轻而易举。如下是lshw
支持输出的格式:
-html 将硬件树输出为HTML
-xml 将硬件树输出为XML
-json 将设备树输出为JSON对象(JavaScript对象表示法)。
-short 输出显示硬件路径的设备树,与HP-UX的ioscan的输出非常相似。
-businfo 输出显示总线信息的设备列表,详细说明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
就像一个“系统级信息专家”,当你只需要快速了解系统级别的硬件信息,比如想知道BIOS
版本是否要更新、CPU
和内存的基本参数时,它就能迅速提供精准信息。lshw
则像是一位“硬件全景大师”,要是你想全面深入地了解整个硬件系统的配置信息,尤其是需要看到详细的硬件树状图,来洞悉硬件之间复杂的架构关系时,那lshw
绝对是不二之选。
总结
dmidecode
和lshw
虽然都是为获取硬件信息而生,但各有各的“拿手好戏”。前者聚焦系统级信息,简洁高效;后者放眼整个硬件配置,详实全面。小伙伴们在实际工作中,就可以根据自己具体需求,精准“点兵”啦,让这两款工具为咱们的运维管理工作提供最有力的支持,在处理硬件相关问题时更加得心应手。