网站搜索

在 Linux 上获取 CPU 信息的 9 个有用命令


在上一篇文章中,我们列出了 10 个有用的命令来收集 Linux 中的系统和硬件信息。在本指南中,我们将缩小范围到 CPU/处理器,并向您展示提取有关计算机 CPU 的详细信息的各种方法。

为了给您一个概览,我们将查询 CPU 架构、vendor_id、型号、型号名称、CPU 核心数、每个核心的速度等信息。
本质上,/proc/cpuinfo 包含所有信息,每个其他命令/实用程序都从此文件获取其输出。

话虽如此,下面是获取有关 Linux CPU 信息的 9 个命令。

1.使用cat命令获取CPU信息

您可以通过cat命令查看/proc/cpuinfo文件的内容来简单地查看系统CPU的信息,如下所示:

cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 69
model name	: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
stepping	: 1
microcode	: 0x1c
cpu MHz		: 1700.062
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
bugs		:
bogomips	: 4788.92
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
......

为了更具体一些,您可以使用 grep 命令 - 一个 CLI 工具,用于在纯文本数据中搜索与正则表达式匹配的行。这可以帮助您仅输出供应商名称、型号名称、处理器数量、核心数量等:

cat /proc/cpuinfo | grep 'vendor' | uniq		#view vendor name
cat /proc/cpuinfo | grep 'model name' | uniq		#display model name
cat /proc/cpuinfo | grep processor | wc -l		#count the number of processing units
cat /proc/cpuinfo | grep 'core id'			#show individual cores	

建议阅读: 如何在 Linux 中使用“cat”和“tac”命令以及示例

2. lscpu 命令 – 显示 CPU 架构信息

命令lscpusysfs/proc/cpuinfo打印CPU架构信息,如下所示:

lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 69
Model name:            Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
Stepping:              1
CPU MHz:               1303.687
CPU max MHz:           2700.0000
CPU min MHz:           800.0000
BogoMIPS:              4788.92
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts

3. cpuid 命令 – 显示 x86 CPU

命令 cpuid 转储从 CPUID 指令收集的有关 CPU 的完整信息,并从该信息中发现 x86 CPU 的确切型号。

确保在运行之前安装它。

sudo apt install cpuid        #Debian/Ubuntu systems
sudo yum install cpuid	#RHEL/CentOS systems 
sudo dnf install cpuid	#Fedora 22+ 

安装后,运行 cpuid 来收集有关 x86 CPU 的信息。

cpuid
CPU 0:
   vendor_id = "GenuineIntel"
   version information (1/eax):
      processor type  = primary processor (0)
      family          = Intel Pentium Pro/II/III/Celeron/Core/Core 2/Atom, AMD Athlon/Duron, Cyrix M2, VIA C3 (6)
      model           = 0x5 (5)
      stepping id     = 0x1 (1)
      extended family = 0x0 (0)
      extended model  = 0x4 (4)
      (simple synth)  = Intel Mobile Core i3-4000Y / Mobile Core i5-4000Y / Mobile Core i7-4000Y / Mobile Pentium 3500U/3600U/3500Y / Mobile Celeron 2900U (Mobile U/Y) (Haswell), 22nm
   miscellaneous (1/ebx):
      process local APIC physical ID = 0x0 (0)
      cpu count                      = 0x10 (16)
      CLFLUSH line size              = 0x8 (8)
      brand index                    = 0x0 (0)
   brand id = 0x00 (0): unknown
   feature information (1/edx):
      x87 FPU on chip                        = true
      virtual-8086 mode enhancement          = true
      debugging extensions                   = true
      page size extensions                   = true
      time stamp counter                     = true
      RDMSR and WRMSR support                = true
      physical address extensions            = true
....

4. dmidecode 命令 – 显示 Linux 硬件信息

dmidecode 是一个用于检索任何 Linux 系统的硬件信息的工具。它将计算机的 DMI(又名 SMBIOS)表内容转储为人类可读的格式,以便于检索。 SMBIOS规范定义了各种DMI类型,对于CPU,使用“处理器”如下:

sudo dmidecode --type processor
dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 2.7 present.

Handle 0x0004, DMI type 4, 42 bytes
Processor Information
	Socket Designation: U3E1
	Type: Central Processor
	Family: Core i5
	Manufacturer: Intel(R) Corporation
	ID: 51 06 04 00 FF FB EB BF
	Signature: Type 0, Family 6, Model 69, Stepping 1
	Flags:
		FPU (Floating-point unit on-chip)
		VME (Virtual mode extension)
		DE (Debugging extension)
		PSE (Page size extension)
		TSC (Time stamp counter)
		MSR (Model specific registers)
		PAE (Physical address extension)
		MCE (Machine check exception)
		CX8 (CMPXCHG8 instruction supported)
		APIC (On-chip APIC hardware supported)
		SEP (Fast system call)
		MTRR (Memory type range registers)
		PGE (Page global enable)
		MCA (Machine check architecture)
.....

5. Inxi工具——显示Linux系统信息

Inxi 是一个功能强大的命令行系统信息脚本,适用于控制台和 IRC(互联网中继聊天)。您可以使用它来即时检索硬件信息。

你可以像这样安装:

sudo apt install inxi 	#Debian/Ubuntu systems
sudo yum install inxi		#RHEL/CentOS systems 
sudo dnf install inxi		#Fedora 22+ 

要显示完整的 CPU 信息,包括每个 CPU 时钟速度和 CPU 最大速度(如果可用),请使用 -C 标志,如下所示:

inxi -C
CPU:       Dual core Intel Core i5-4210U (-HT-MCP-) cache: 3072 KB 
           clock speeds: max: 2700 MHz 1: 1958 MHz 2: 1993 MHz 3: 1775 MHz 4: 1714 MHz

6. lshw 工具 – 列出硬件配置

lshw 是一个用于收集有关计算机硬件配置的深入信息的最小工具。您可以使用 -C 选项来选择硬件类别,在本例中为 CPU:

sudo lshw -C CPU
*-cpu                   
       description: CPU
       product: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
       vendor: Intel Corp.
       physical id: 4
       bus info: cpu@0
       version: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
       serial: To Be Filled By O.E.M.
       slot: U3E1
       size: 2626MHz
       capacity: 2700MHz
       width: 64 bits
       clock: 100MHz
       capabilities: x86-64 fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts cpufreq
       configuration: cores=2 enabledcores=2 threads=4

7.hardinfo – 在 GTK+ 窗口中显示硬件信息

hardinfo在GTK+窗口中显示硬件信息,您可以按如下方式安装它:

sudo apt install hardinfo 	#Debian/Ubuntu systems
sudo yum install hardinfo	#RHEL/CentOS systems 
sudo dnf install hardinfo	#Fedora 22+ 

安装后,输入:

hardinfo 

它还允许您通过单击“生成报告”按钮来生成系统硬件信息报告。在下面的界面中,单击“生成”继续。请注意,您可以选择要生成的硬件信息类别。

生成 html 格式的报告后,您可以通过 Web 浏览器查看该报告,如下所示。

8. hwinfo – 显示当前硬件信息

hwinfo 用于提取有关 Linux 系统中存在的硬件的信息。要显示有关 CPU 的信息,请使用 --cpu

hwinfo --cpu
01: None 00.0: 10103 CPU                                        
  [Created at cpu.460]
  Unique ID: rdCR.j8NaKXDZtZ6
  Hardware Class: cpu
  Arch: X86-64
  Vendor: "GenuineIntel"
  Model: 6.69.1 "Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz"
  Features: fpu,vme,de,pse,tsc,msr,pae,mce,cx8,apic,sep,mtrr,pge,mca,cmov,pat,pse36,clflush,dts,acpi,mmx,fxsr,sse,sse2,ss,ht,tm,pbe,syscall,nx,pdpe1gb,rdtscp,lm,constant_tsc,arch_perfmon,pebs,bts,rep_good,nopl,xtopology,nonstop_tsc,aperfmperf,eagerfpu,pni,pclmulqdq,dtes64,monitor,ds_cpl,vmx,est,tm2,ssse3,sdbg,fma,cx16,xtpr,pdcm,pcid,sse4_1,sse4_2,movbe,popcnt,tsc_deadline_timer,aes,xsave,avx,f16c,rdrand,lahf_lm,abm,epb,tpr_shadow,vnmi,flexpriority,ept,vpid,fsgsbase,tsc_adjust,bmi1,avx2,smep,bmi2,erms,invpcid,xsaveopt,dtherm,ida,arat,pln,pts
  Clock: 2080 MHz
  BogoMips: 4788.92
  Cache: 3072 kb
  Units/Processor: 16
  Config Status: cfg=new, avail=yes, need=no, active=unknown
....

9. nproc – 打印处理单元数

nproc 命令用于显示计算机上存在的处理单元的数量:

nproc

有关其他使用信息和选项,请阅读这些命令的手册页,如下所示:

man commandname

另外,请查看:

  1. Cpustat – 通过在 Linux 中运行进程来监控 CPU 利用率
  2. CoreFreq – 适用于 Linux 系统的强大 CPU 监控工具
  3. 根据 Linux 中内存和 CPU 使用率最高的情况查找运行最多的进程
  4. 如何使用“Stress-ng”工具在 Linux 上施加高 CPU 负载和压力测试

现在就是这样!您可以通过下面的反馈表与我们分享在 Linux 中提取 CPU 信息的其他方法。