如何在 Linux 中检查环境是否为虚拟机

LinuxLinuxBeginner
立即练习

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

简介

在这个实验中,你将学习如何判断你的 Linux 环境是否运行在虚拟机上。我们将探索三种不同的方法来实现这一目标:使用 dmidecode 命令检查系统信息、利用专门用于虚拟化检测的 virt-what 工具,以及检查 /proc/cpuinfo 文件中的 CPU 标志。在本实验结束时,你将掌握在 Linux 中识别虚拟化环境的实用技术。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/BasicSystemCommandsGroup -.-> linux/man("Manual Access") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/UserandGroupManagementGroup -.-> linux/sudo("Privilege Granting") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") subgraph Lab Skills linux/man -.-> lab-558808{{"如何在 Linux 中检查环境是否为虚拟机"}} linux/cat -.-> lab-558808{{"如何在 Linux 中检查环境是否为虚拟机"}} linux/grep -.-> lab-558808{{"如何在 Linux 中检查环境是否为虚拟机"}} linux/sudo -.-> lab-558808{{"如何在 Linux 中检查环境是否为虚拟机"}} linux/apt -.-> lab-558808{{"如何在 Linux 中检查环境是否为虚拟机"}} end

使用 dmidecode 检查系统信息

在这一步中,你将学习如何使用 dmidecode 命令来获取系统硬件的详细信息。dmidecode 会从 DMI/SMBIOS(桌面管理接口/系统管理 BIOS)表中读取数据,该表包含了系统组件的描述。

这个命令对于了解 Linux 系统所运行的硬件细节非常有用,比如主板、CPU、内存等等。

由于访问 DMI 表需要 root 权限,你需要在使用 dmidecode 时加上 sudo

如果终端尚未打开,请打开它。记住,你可以在桌面左侧找到 Xfce 终端 图标。

现在,输入以下命令并按回车键:

sudo dmidecode

这个命令会输出大量关于系统硬件的信息。滚动输出内容,查看不同的部分,如“BIOS Information”(BIOS 信息)、“System Information”(系统信息)、“Base Board Information”(主板信息)、“Processor Information”(处理器信息)、“Memory Device”(内存设备)等等。

输出可能如下所示(为简洁起见,省略了部分输出内容):

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

Handle 0x0000, DMI type 0, 26 bytes
BIOS Information
        Vendor: SeaBIOS
        Version: 1.16.0-1.fc36
        Release Date: 07/07/2022
        Address: 0xE0000
        Runtime Size: 128 kB
        ROM Size: 64 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.2 MB floppy services are supported (int 13h)
                3.5" / 720 kB floppy services are supported (int 13h)
                3.5" / 1.44 MB floppy services are supported (int 13h)
                8042 keyboard services are supported (int 9h)
                CGA/mono video services are supported (int 10h)
                AT/XT compatible keyboard services are supported (int 16h)
                Printer services are supported (int 17h)
                CGA/mono video is supported (int 10h)
                ACPI is supported
                USB legacy is supported
                BIOS boot specification is supported
                Targeted content distribution is supported
                UEFI is supported
        BIOS Revision: 1.16

Handle 0x0001, DMI type 1, 27 bytes
System Information
        Manufacturer: QEMU
        ProductName: Standard PC (Q35 + ICH9, 2009)
        Version: pc-q35-7.0
        Serial Number: <filtered>
        UUID: <filtered>
        Wake-up Type: Power Switch
        SKU Number: <filtered>
        Family: Virtual Machine

Handle 0x0002, DMI type 2, 17 bytes
Base Board Information
        Manufacturer: QEMU
        ProductName: Standard PC (Q35 + ICH9, 2009)
        Version: pc-q35-7.0
        Serial Number: <filtered>
        Asset Tag: <filtered>
        Features:
                Board is a hosting board
                Board is replaceable
        Location In Chassis: <filtered>
        Chassis Handle: 0x0003
        Type: Motherboard
        Contained Object Handles: 0

... (output continues)

你还可以使用 -t 选项,后跟类型编号或关键字来过滤输出,以查看特定类型硬件的信息。例如,要仅查看 CPU 信息,你可以使用:

sudo dmidecode -t processor

或者,要查看内存设备的信息:

sudo dmidecode -t memory

尝试使用不同的类型,如 biossystembaseboardchassis 等,以查看每个组件的具体细节。

使用 dmidecode 是系统管理员以及任何需要解决硬件问题或了解底层系统配置的人的一项基本技能。

点击 继续 进入下一步。

使用 virt-what 检测虚拟化

在这一步中,你将学习如何判断你的 Linux 系统是运行在虚拟机内还是裸机硬件上。这对于系统配置、性能调优或许可证管理通常很重要。

我们将使用 virt-what 命令,它是专门为此目的设计的。它会检查虚拟化的迹象,并打印出检测到的虚拟机管理程序(hypervisor)的名称。

首先,你需要安装 virt-what 软件包。像在上一个实验中安装 htop 那样使用 apt 进行安装。

打开终端并运行以下命令来安装 virt-what

sudo apt update
sudo apt install virt-what -y

-y 标志会自动对安装过程中的任何提示回答“yes”,使安装过程无需人工交互。

安装完成后,你可以运行 virt-what 命令:

virt-what

该命令的输出会告诉你系统是否被虚拟化,如果是,还会显示正在使用的虚拟化技术。

在 LabEx 环境(运行在虚拟化环境中)中,你应该会看到类似以下的输出:

qemu
kvm

这表明系统运行在 QEMU/KVM 虚拟机内。如果系统运行在物理硬件上,virt-what 通常不会产生任何输出。

了解系统是否被虚拟化对于许多系统管理任务来说是至关重要的信息。virt-what 提供了一种简单可靠的方式来获取此信息。

点击 继续 进入下一步。

检查 /proc/cpuinfo 中的 CPU 标志

在这一步中,你将探索 /proc/cpuinfo 文件,它是 Linux 内核提供的一个虚拟文件,包含了你系统中 CPU 的相关信息。这个文件是了解处理器详细信息的重要来源,包括其型号、速度、缓存大小和支持的特性(标志)。

/proc 文件系统是一个伪文件系统,它提供了访问内核数据结构的接口。/proc 中的文件并不存储在磁盘上,而是在你访问它们时由内核动态生成。

要查看 /proc/cpuinfo 的内容,你可以使用像 catless 这样的命令行工具。cat 会将整个内容打印到终端,而 less 允许你逐页滚动查看内容。由于 /proc/cpuinfo 可能很长,less 通常更方便。

打开终端并输入以下命令,使用 cat 查看 CPU 信息:

cat /proc/cpuinfo

你将看到系统中每个 CPU 核心的详细信息。输出将包含诸如 processorvendor_idmodel namecpu MHzcache sizeflags 等行。

以下是你可能看到的输出示例(为简洁起见,省略了部分输出):

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 158
model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
stepping        : 13
microcode       : 0xffffffff
cpu MHz         : 2904.000
cache size      : 16384 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
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 ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves arch_capabilities
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds mmio_stale_data retbleed
bogomips        : 5808.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 158
model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
stepping        : 13
microcode       : 0xffffffff
cpu MHz         : 2904.000
cache size      : 16384 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
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 ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves arch_capabilities
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds mmio_stale_data retbleed
bogomips        : 5808.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

flags 行特别值得关注,因为它列出了你的 CPU 支持的各种特性和扩展。这些标志表示了诸如虚拟化支持(vmxsvm)、特定指令集(如 sseavx)和安全特性等功能。

你还可以使用 grep/proc/cpuinfo 中搜索特定信息。例如,要查找型号名称:

grep "model name" /proc/cpuinfo

这将输出每个 CPU 核心的型号名称:

model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz

或者,要检查是否存在特定标志,如 vmx(英特尔 VT-x 虚拟化支持):

grep "vmx" /proc/cpuinfo

如果输出中存在 vmx,则表示你的 CPU 支持英特尔 VT-x 虚拟化。

探索 /proc/cpuinfo 是从内核层面直接了解系统处理器能力的基本方法。

点击 继续 完成本实验。

总结

在本实验中,你学习了如何使用多种方法检查 Linux 环境是否为虚拟机。你首先使用 sudo 权限运行 dmidecode 命令,以访问 DMI/SMBIOS 表并获取详细的系统硬件信息,例如 BIOS、系统和处理器的详细信息。该命令能全面概述底层硬件组件。

接下来,你将探索其他检测虚拟化的技术,包括使用 virt-what 命令以及检查 /proc/cpuinfo 文件中的 CPU 标志。这些方法通过检查特定的系统特征和配置,提供了识别虚拟化环境是否存在的其他途径。