Linux lshw命令–获取Linux硬件信息
时间:2020-02-23 14:39:51 来源:igfitidea点击:
Linux lshw命令列出了系统的所有硬件配置。
我们可以从lshw命令提取的一些常见信息是:
- 内存内存信息
- CPU配置
- 主板信息
- 硬盘信息
- 网络设备信息
Linux lshw命令功能
能够产生各种格式的输出,例如XML,JSON和HTML。
我们可以使用重定向运算符轻松地将输出转储到文件中。
默认输出包含很多信息,我们可以使用各种选项来获取特定的硬件信息。
也支持GUI,仅对桌面环境有用。
在大多数情况下,我们在没有GUI软件的Linux服务器上运行这些命令。
lshw命令限制
到目前为止,它还没有检测到Firewire设备。
它必须以超级用户(root或者sudo)身份运行,以获取正确的硬件详细信息。
我发现-dump选项无法将输出保存到文件中。
Linux硬件信息示例
我们来看一些使用lshw命令获取linux硬件信息的示例。
1.获取完整的Linux硬件信息
root@localhost:~# sudo lshw
localhost
description: Computer
product: Standard PC (Q35 + ICH9, 2009)
vendor: QEMU
version: pc-q35-3.1
width: 64 bits
capabilities: smbios-2.8 dmi-2.8 vsyscall32
configuration: boot=normal
*-core
description: Motherboard
physical id: 0
*-firmware
description: BIOS
vendor: SeaBIOS
physical id: 0
version: rel-1.12.0-0-ga698c8995f-prebuilt.qemu.org
date: 04/01/2014
size: 96KiB
*-cpu
description: CPU
product: AMD EPYC 7501 32-Core Processor
vendor: Advanced Micro Devices [AMD]
physical id: 400
bus info: cpu@0
version: pc-q35-3.1
slot: CPU 0
size: 2GHz
capacity: 2GHz
width: 64 bits
capabilities: fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp x86-64 rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core ssbd ibpb vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xgetbv1 virt_ssbd arat
configuration: cores=1 enabledcores=1 threads=1
*-memory
description: System Memory
physical id: 1000
size: 2GiB
capacity: 2GiB
capabilities: ecc
configuration: errordetection=multi-bit-ecc
*-bank
description: DIMM 内存
vendor: QEMU
physical id: 0
slot: DIMM 0
size: 2GiB
*-pci
description: Host bridge
product: 82G33/G31/P35/P31 Express D内存 Controller
vendor: Intel Corporation
physical id: 100
bus info: pci@0000:00:00.0
version: 00
width: 32 bits
clock: 33MHz
root@localhost:~#
如您所见,其中包含很多信息,如果您需要快速的硬件信息,请使用-short选项运行它。
root@localhost:~# sudo lshw -short
H/W path Device Class Description
=============================================
system Standard PC (Q35 + ICH9, 2009)
/0 bus Motherboard
/0/0 memory 96KiB BIOS
/0/400 processor AMD EPYC 7501 32-Core Processor
/0/1000 memory 2GiB System Memory
/0/1000/0 memory 2GiB DIMM 内存
/0/100 bridge 82G33/G31/P35/P31 Express D内存 Controller
/0/100/1 display VGA compatible controller
/0/100/2 storage Virtio SCSI
/0/100/2/0 generic Virtual I/O device
/0/100/3 storage Virtio SCSI
/0/100/3/0 generic Virtual I/O device
/0/100/4 network Virtio network device
/0/100/4/0 eth0 network Ethernet interface
/0/100/1f bridge 82801IB (ICH9) LPC Interface Controller
/0/100/1f.2 storage 82801IR/IO/IH (ICH9R/DO/DH) 6 port SATA Controller [AHCI mode]
/0/100/1f.3 bus 82801I (ICH9 Family) SMBus Controller
/0/1 scsi0 storage
/0/1/0.0.0 /dev/sda volume 49GiB QEMU HARDDISK
/0/2 scsi1 storage
/0/2/0.1.2 /dev/sdb volume 512MiB QEMU HARDDISK
root@localhost:~#
2.获取Linux CPU信息
我们可以运行lshw -C class_name来获取特定的硬件类信息。
让我们看看如何获取CPU信息。
root@localhost:~# sudo lshw -C cpu
*-cpu
description: CPU
product: AMD EPYC 7501 32-Core Processor
vendor: Advanced Micro Devices [AMD]
physical id: 400
bus info: cpu@0
version: pc-q35-3.1
slot: CPU 0
size: 2GHz
capacity: 2GHz
width: 64 bits
capabilities: fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp x86-64 rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core ssbd ibpb vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xgetbv1 virt_ssbd arat
configuration: cores=1 enabledcores=1 threads=1
root@localhost:~#
如果您有多个CPU,则该命令将提供所有CPU的信息。
lshw -C processor命令还将打印CPU信息。
如果不确定类名,请在lshw short输出中查看它。
3.获取Linux内存信息
root@localhost:~# sudo lshw -C memory
*-firmware
description: BIOS
vendor: SeaBIOS
physical id: 0
version: rel-1.12.0-0-ga698c8995f-prebuilt.qemu.org
date: 04/01/2014
size: 96KiB
*-memory
description: System Memory
physical id: 1000
size: 2GiB
capacity: 2GiB
capabilities: ecc
configuration: errordetection=multi-bit-ecc
*-bank
description: DIMM 内存
vendor: QEMU
physical id: 0
slot: DIMM 0
size: 2GiB
root@localhost:~#
4.获取Linux硬盘信息
root@localhost:~# sudo lshw -C storage
5.获取Linux主板信息
root@localhost:~# sudo lshw -C bus
*-core
description: Motherboard
physical id: 0
*-serial UNCLAIMED
description: SMBus
product: 82801I (ICH9 Family) SMBus Controller
vendor: Intel Corporation
physical id: 1f.3
bus info: pci@0000:00:1f.3
version: 02
width: 32 bits
clock: 33MHz
configuration: latency=0
resources: ioport:700(size=64)
root@localhost:~#
6.获取Linux网络接口信息
root@localhost:~# sudo lshw -C network
*-network
description: Ethernet controller
product: Virtio network device
vendor: Red Hat, Inc.
physical id: 4
bus info: pci@0000:00:04.0
version: 00
width: 64 bits
clock: 33MHz
capabilities: msix bus_master cap_list rom
configuration: driver=virtio-pci latency=0
resources: irq:20 ioport:c0c0(size=32) memory:febd3000-febd3fff memory:fe008000-fe00bfff memory:feb80000-febbffff
*-virtio2
description: Ethernet interface
physical id: 0
bus info: virtio@2
logical name: eth0
serial: f2:3c:92:d4:76:9e
capabilities: ethernet physical
configuration: autonegotiation=off broadcast=yes driver=virtio_net driverversion=1.0.0 ip=45.79.122.188 link=yes multicast=yes
root@localhost:~#
7. HTML,XML和JSON格式的Linux硬件信息
lshw命令的默认输出为纯文本。
通过指定相应的选项,我们可以获得HTML,XML和JSON格式的输出。
XML:
root@localhost:~# sudo lshw -C bus -xml <?xml version="1.0" standalone="yes" ?> <!-- generated by lshw-B.02.18 --> <!-- GCC 7.3.0 --> <!-- Linux 4.15.0-88-generic #88-Ubuntu SMP Tue Nov 11 20:11:34 UTC 2017 x86_64 --> <!-- GNU libc 2 (glibc 2.27) --> <list> <node id="core" claimed="true" class="bus" handle=""> <description>Motherboard</description> <physid>0</physid> <node id="serial" class="bus" handle="PCI:0000:00:1f.3"> <description>SMBus</description> <product>82801I (ICH9 Family) SMBus Controller</product> <vendor>Intel Corporation</vendor> <physid>1f.3</physid> <businfo>pci@0000:00:1f.3</businfo> <version>02</version> <width units="bits">32</width> <clock units="Hz">33000000</clock> <configuration> <setting id="latency" value="0" </configuration> <resources> <resource type="ioport" value="700(size=64)" </resources> </node> </node> </list> root@localhost:~#
JSON:
root@localhost:~# sudo lshw -C memory -json
{
"id" : "firmware",
"class" : "memory",
"claimed" : true,
"description" : "BIOS",
"vendor" : "SeaBIOS",
"physid" : "0",
"version" : "rel-1.12.0-0-ga698c8995f-prebuilt.qemu.org",
"date" : "04/01/2014",
"units" : "bytes",
"size" : 98304
},
{
"id" : "memory",
"class" : "memory",
"claimed" : true,
"handle" : "DMI:1000",
"description" : "System Memory",
"physid" : "1000",
"units" : "bytes",
"size" : 2147483648,
"capacity" : 2147483648,
"configuration" : {
"errordetection" : "multi-bit-ecc"
},
"capabilities" : {
"ecc" : "Multi-bit error-correcting code (ECC)"
},
"children" : [
{
"id" : "bank",
"class" : "memory",
"claimed" : true,
"handle" : "DMI:1100",
"description" : "DIMM 内存",
"vendor" : "QEMU",
"physid" : "0",
"slot" : "DIMM 0",
"units" : "bytes",
"size" : 2147483648
}
]
},
root@localhost:~#
HTML:
root@localhost:~# sudo lshw -C memory -html
8.将Linux硬件信息保存到文件中
我尝试将-dump选项与lshw命令配合使用,但对我而言不起作用。
但是,我们始终可以使用重定向运算符(>)将其保存到新文件中。
# sudo lshw -html > hardware_info.html

