如何在 Linux 中检查特定 CPU 特性是否可用

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何检查你的 Linux 系统上可用的特定 CPU 特性。你将探索不同的方法来检查 CPU 信息,首先使用基本的 cat /proc/cpuinfo 命令查看详细的 CPU 规格和标志。

在初步检查之后,你将使用 lscpu 命令来更有条理地概述 CPU 架构和功能。最后,你将使用 dmidecode 深入了解硬件细节,以进一步了解系统的组件,包括 CPU 信息。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/UserandGroupManagementGroup -.-> linux/sudo("Privilege Granting") subgraph Lab Skills linux/cat -.-> lab-558803{{"如何在 Linux 中检查特定 CPU 特性是否可用"}} linux/sudo -.-> lab-558803{{"如何在 Linux 中检查特定 CPU 特性是否可用"}} end

使用 cat /proc/cpuinfo 列出 CPU 特性

在这一步中,你将学习如何使用 cat 命令和 /proc/cpuinfo 文件来检查你的 Linux 系统的 CPU 信息。

/proc 文件系统是 Linux 中的一个虚拟文件系统,它提供有关进程和其他系统信息。/proc/cpuinfo 文件包含了你系统中安装的 CPU 的详细信息。

cat 命令是一个基本的 Linux 实用工具,用于显示文件的内容。

如果终端尚未打开,请打开它。你可以通过点击桌面左侧的 Xfce Terminal 图标来完成此操作。

现在,输入以下命令以显示 /proc/cpuinfo 文件的内容:

cat /proc/cpuinfo

按下回车键。

你将看到大量输出,详细描述了 CPU 的各个方面。输出将类似于以下内容(为简洁和通用起见,省略了部分输出):

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 158
model name      : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
stepping        : 9
microcode       : 0x...
cpu MHz         : ...
cache size      : ... KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : ...
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase bmi1 hle avx2 smep bmi2 erms rtm invpcid mpx rdseed adx smap clflushopt xsaves xsaveopt pcpe bts md_clear flush_l1d arch_capabilities
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds mmio_stale_data retbleed
bogomips        : ...
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
...

此文件提供了每个 CPU 核心的详细信息,包括其制造商、型号名称、速度、缓存大小和支持的特性(列在 flags 下)。

理解 /proc/cpuinfo 的输出有助于进行故障排除和了解系统硬件的功能。

点击 Continue 进入下一步。

使用 lscpu 验证特性

在上一步中,你使用 cat /proc/cpuinfo 查看了详细的 CPU 信息。虽然 /proc/cpuinfo 提供的信息很全面,但输出可能会非常冗长,有时难以快速解析。

lscpu 是一个更用户友好的命令,用于获取 CPU 架构信息的摘要。lscpu 命令从 /proc/cpuinfo 收集 CPU 架构信息,并以格式良好、易于阅读的输出呈现。

让我们使用 lscpu 命令来获取你的 CPU 摘要。

在终端中输入以下命令:

lscpu

按下回车键。

你将看到你的 CPU 架构的简洁摘要。输出将类似于以下内容:

Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         39 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  1
  On-line CPU(s) list:   0
Vendor ID:               GenuineIntel
  Model name:            Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase bmi1 hle avx2 smep bmi2 erms rtm invpcid mpx rdseed adx smap clflushopt xsaves xsaveopt pcpe bts md_clear flush_l1d arch_capabilities
L1d cache:               32K
L1i cache:               32K
L2 cache:                256K
L3 cache:                6M
NUMA node(s):            1
  NUMA node0 CPU(s):     0
Vulnerability Itlb multihit:  KVM: Mitigation: VMX disabled
Vulnerability L1tf:      Mitigation: PTE Inversion; VMX conditional cache flushes, SMT disabled
Vulnerability Mds:       Mitigation: Clear CPU buffers; SMT disabled
Vulnerability Meltdown:  Mitigation: PTI
Vulnerability Mmio stale data: Mitigation: Clear CPU buffers; SMT disabled
Vulnerability Retbleed:  Mitigation: IBPB conditional, IBRS_FW, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Spec store bypass: Mitigation: Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:  Mitigation: Invalidate IBPB conditional, INDIR branch predictor by IBPB_FW, STIBP disabled
Vulnerability Spectre v2:  Mitigation: IBPB conditional, IBRS_FW, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:     Mitigation: Microcode
Vulnerability Tsx async abort: Not affected

注意 lscpu 如何以结构化的格式呈现信息,这使得查找特定细节(如架构、CPU 操作模式、制造商 ID、型号名称和标志摘要(CPU 特性))变得更加容易。

虽然 cat /proc/cpuinfo 为你提供原始数据,但 lscpu 提供了经过解析和整理的视图,这通常更便于快速概览。

点击 Continue 进入下一步。

使用 dmidecode 检查硬件

在前面的步骤中,你使用 cat /proc/cpuinfolscpu 探索了 CPU 信息。现在,让我们来看看一个更通用的用于检查系统硬件的工具:dmidecode

dmidecode 是一个读取计算机的 DMI(桌面管理接口,Desktop Management Interface)或 SMBIOS(系统管理 BIOS,System Management BIOS)表的工具。该表包含了系统硬件组件的信息,如主板、BIOS、内存等。

由于 dmidecode 访问的是底层系统信息,因此它需要 root 权限。你可以使用 sudo 命令以这些权限运行 dmidecode。请记住,在这个环境中,labex 用户可以无密码使用 sudo 权限。

在终端中输入以下命令以显示完整的 DMI 信息:

sudo dmidecode

按下回车键。

你将看到大量输出,详细描述了各种硬件组件。输出被组织成不同的“句柄”或部分,每个部分描述了系统的一个特定部分(例如,BIOS 信息、系统信息、主板信息、内存设备)。

以下是你可能看到的输出片段(输出会根据虚拟机的配置而有所不同):

## dmidecode 3.3
Getting SMBIOS data from sysfs.
SMBIOS 3.3.0 present.

Handle 0x0000, DMI type 0, 26 bytes
BIOS Information
        Vendor: Google
        Version: Google
        Release Date: 01/01/2011
        Address: 0xE8000
        Runtime Size: 96 kB
        ROM Size: 128 kB
        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
                5.25" / 1.2MB floppy services are supported (int 13h)
                3.5" / 720kB floppy services are supported (int 13h)
                3.5" / 2.88MB floppy services are supported (int 13h)
                Print screen service is supported (int 5h)
                8042 keyboard services are supported (int 9h)
                Serial services are supported (int 14h)
                Printer services are supported (int 17h)
                CGA/mono video services are supported (int 10h)
                NEC PC-98
                ACPI is supported
                USB legacy is supported
                BIOS boot specification is supported
                Targeted content distribution is supported
                UEFI is supported
        BIOS Revision: 0.0

Handle 0x0001, DMI type 1, 27 bytes
System Information
        Manufacturer: Google
        Product Name: Google Compute Engine
        Version: pc-i440fx-xenial
        Serial Number: ...
        UUID: ...
        Wake-up Type: Power Switch
        SKU Number: ...
        Family: Virtual Machine

Handle 0x0002, DMI type 2, 10 bytes
Baseboard Information
        Manufacturer: Google
        Product Name: Google Compute Engine
        Version: pc-i440fx-xenial
        Serial Number: ...
        Asset Tag: No Asset Tag
        Features:
                Board is a hosting board
                Board is replaceable
        Location In Chassis: Not Specified
        Chassis Handle: 0x0003
        Type: Motherboard
        Contained Object Handles: 0

... (more output)

你还可以使用 -t 选项指定你想要查看的信息类型。例如,要仅查看内存信息:

sudo dmidecode -t memory

按下回车键。

这将过滤输出,仅显示与系统内存设备相关的详细信息。

dmidecode 是一个强大的工具,无需打开计算机即可收集详细的硬件规格信息。它在硬件盘点或解决硬件相关问题时特别有用。

点击 Continue 完成此实验。

总结

在本次实验中,你学习了如何在 Linux 系统中检查特定的 CPU 特性。你首先使用 cat 命令显示 /proc/cpuinfo 虚拟文件中包含的详细 CPU 信息,该文件提供了 CPU 特性和支持的标志的完整列表。这第一步让你了解了如何通过 /proc 文件系统访问系统信息。