Contents

确定虚拟化技术的简单方法

1. 概述

通常,当我们使用虚拟 Linux 系统工作时,感觉与常规系统没有太大区别。但是,有时候,我们还是想知道一个系统是不是虚拟系统。

此外,我们可能想知道虚拟系统使用的是哪种虚拟化 技术,例如VMwareVirtualBox 、  Linux KVM 等。

在本教程中,我们将讨论如何使用 Linux 命令确定虚拟化技术。

2. 使用dmidecode命令

dmidecode 命令 可以根据SMBIOS / DMI 标准报告有关我们系统 BIOS 的信息。

由于 dmidecode命令试图读取硬件和固件信息,我们需要root访问权限才能执行该命令

当我们使用非虚拟化系统时,我们将通过查询关键system-product-name来获取主板信息:

$ sudo dmidecode -s system-product-name
H87-D3H

上面的输出显示系统运行在带有(技嘉)H87-D3H主板的机器上。

但是,如果系统在虚拟机中运行,则相同的命令将打印虚拟化产品的名称。

让我们用一些常见的虚拟化技术测试这个命令,看看它会报告什么。

首先,让我们在 VirtualBox 中的虚拟系统上执行命令:

$ sudo dmidecode -s system-product-name
VirtualBox

它报告正确的虚拟化环境。

接下来我们在KVM和VMware虚拟机上试试命令:

$ sudo dmidecode -s system-product-name
KVM
$ sudo dmidecode -s system-product-name
VMware Virtual Platform

如果我们有root访问权限,使用dmidecode命令是获取虚拟化信息的一种非常简单的方法。

3. 使用lshw命令

lshw 命令 是列出硬件配置详细信息的便捷工具。 我们可以使用此工具并查询 system class以获取有关虚拟化的信息。

我们应该注意到,获取虚拟化技术数据需要root访问权限。

让我们从在非虚拟化系统上执行命令开始,看看它打印了什么:

$ sudo lshw -class system
yk-arch
    description: Desktop Computer
    product: H87-D3H (To be filled by O.E.M.)
    vendor: Gigabyte Technology Co., Ltd.
    version: To be filled by O.E.M.
    serial: To be filled by O.E.M.
    width: 64 bits
    capabilities: smbios-2.7 dmi-2.7 smp vsyscall32
    configuration: administrator_password=disabled boot=normal ...

dmidecode类似,  lshw命令在“product”字段中打印主板信息。

接下来,让我们看看它会在各种虚拟系统上报告什么。

首先,让我们在 VirtualBox 的虚拟机中运行它:

$ sudo lshw -class system
myVbox.test
    description: Computer
    product: VirtualBox
    vendor: Oracle
    version: 1.2
    serial: 0
    width: 64 bits
    capabilities: smbios-2.5 dmi-2.5 smp vsyscall32
    configuration: family=Virtual Machine uuid=......

产品 领域现在充满了预期的虚拟化技术信息。

让我们对 KVM 和 VMware 上的系统尝试相同的命令:

$ sudo lshw -class system
myKvm.test
    description: Computer
    product: KVM
    vendor: Red Hat
    version: 1.2
    serial: 0
    width: 64 bits
    capabilities: smbios-2.4 dmi-2.4 smp vsyscall32
    configuration: family=Virtual Machine uuid=......
$ sudo lshw -class system
myVmware.test
    description: Computer
    product: VMware Virtual Platform
    vendor: VMware, Inc.
    version: 1.2
    serial: 0
    width: 64 bits
    capabilities: smbios-2.4 dmi-2.4 smp vsyscall32
    configuration: family=Virtual Machine uuid=......

如果我们有root访问权限,lshw命令是我们解决问题的便捷选项。

4. 使用systemd-detect-virt命令

大多数现代 Linux 系统都使用*systemd *作为系统和服务管理器。systemd软件包附带systemd-detect-virt 实用程序,我们可以使用它来检测虚拟化技术。

值得一提的是,我们不需要root权限来执行这个命令

像往常一样,如果我们在非虚拟化系统上执行命令,让我们看看它会报告什么:

$ systemd-detect-virt
none

如输出所示,如果系统未在虚拟机中运行,我们将得到“ none ”作为结果

接下来,让我们检查在 VirtualBox 中的虚拟机上运行它时得到的输出:

$ systemd-detect-virt
oracle

它列出了“ oracle ”而不是“VirtualBox”之类的东西。这是因为systemd-detect-virt命令只显示 虚拟化技术的ID

在手册页中,有一个表格列出了详细的ID和产品信息。例如ID为“ oracle ”的产品信息为:

 oracle                 Oracle VM VirtualBox   
                        (historically marketed 
                        by innotek and Sun    
                        Microsystems), for     
                        legacy and KVM         
                        hypervisor

现在,让我们在 KVM 和 VMware 的虚拟机上运行命令:

$ systemd-detect-virt
kvm
$ systemd-detect-virt
vmware

正如我们在上面看到的,systemd-detect-virt 命令可以非常方便地获取虚拟化技术的信息。 我们可以使用*–list*选项获取可检测容器和 VM 环境的完整列表:

$ systemd-detect-virt --list
none
kvm
qemu
bochs
xen
uml
vmware
oracle
microsoft
...

5. 使用hostnamectl命令

除了方便的systemd-detect-virt命令外,systemd软件包还提供了hostnamectl 命令。该实用程序允许我们查询和更改系统主机名和相关设置。

我们也可以使用hostnamectl命令来检测虚拟化技术。

systemd-detect-virt命令相同,如果我们想使用hostnamectl命令查询信息,则不需要root权限。

首先,让我们看看 如果在非虚拟化系统上执行hostnamectl会告诉我们什么:

$ hostnamectl
   Static hostname: YK-Arch
         Icon name: computer-desktop
           Chassis: desktop
        Machine ID: d0fdfacb84184cee9507e0e1ac518e73
           Boot ID: 9f88561ec0864abfa51575e8ec74732f
  Operating System: Arch Linux
            Kernel: Linux 5.7.9-arch1-1
      Architecture: x86-64

上面的输出显示了详细的分发名称、操作系统和内核信息。

此外,我们将再次在 VirtualBox 来宾系统上执行相同的命令。让我们检查它是否报告不同的信息:

$ hostnamectl
   Static hostname: myVbox.test
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b4998efc50ae499881b024c258934223
           Boot ID: f885614223a240b3b3b04cef3a934f18
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
            Kernel: Linux 3.10.0-514.10.2.el7.x86_64
      Architecture: x86-64

在上面的输出中,Icon nameChassis告诉我们它是一个“ vm ”(虚拟机)。

而且,我们还可以看到一个新的领域——虚拟化。该字段告诉我们我们的系统运行在哪种虚拟化技术中。在本例中,它是oracle (VirtualBox)。

让我们看看该命令将在 KVM 和 VMware 虚拟机上报告什么:

$ hostnamectl
   Static hostname: myKvm.test
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 55296cb0566a4aaca10b8e3a4b28b432
           Boot ID: 1bb259b0eb064d9eb8a22d112211b334
    Virtualization: kvm
  Operating System: Ubuntu 19.10
            Kernel: Linux 5.3.0-59-generic
      Architecture: x86-64
$ hostnamectl
   Static hostname: myVmware.test
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 18a0752e1ccbeef09da51ad17fab1f1b
           Boot ID: beefdc99969e4a4a8525ff842b383c62
    Virtualization: vmware
  Operating System: Ubuntu 19.10
            Kernel: Linux 5.3.0-59-generic
      Architecture: x86-64