操作系统可以检测它们是否正在VM中运行吗? 为什么抛出这个问题呢, 是因为很多软件会自动检测自己是否是运行在虚拟机环境下, 软件本身对运行环境是由要求的。 所以当一个软件无法运行的时候, 可以考虑是不是虚拟机环境造成的。 本文分别在Windows和Linux下检测一下操作系统是否运行在虚拟机中。 Windows下 在电脑属性中看不到任何虚拟机的信息: 使用systeminfo可以查看更加详细的信息,从下图可以看出系统制造商是QEMU: 下面介绍Linux系统下识别VM的九种方法 Linux下第一种方法: dmes 物理机: root@server01:~# dmesg | grep "Hypervisor" 无返回 虚拟机: root@server02:~# dmesg | grep "Hypervisor" [ 0.000000] Hypervisor detected: KVM Linux下第二种方法: lscpu 物理机: root@server01:~# lscpu | grep Hypervisor 无返回 虚拟机: root@server02:~# lscpu | grep Hypervisor Hypervisor vendor: KVM linux下第三种方法: virt-what 物理机: root@server01:~# virt-what 无返回 虚拟机: root@server02:~# virt-what kvm linux下第四种方法: imvirt 物理机: root@server01:~# imvirt Physical 虚拟机: root@server02:~# imvirt KVM linux下第五种方法: systemd-detect-virt 物理机: root@server01:~# systemd-detect-virt none 虚拟机: root@server02:~# systemd-detect-virt qemu linux下第六种方法: facter 物理机: root@server01:~# facter virtual physical 虚拟机: root@server02:~# facter virtual kvm linux下第七种方法: hostnamectl 物理机: root@server01:~# hostnamectl Static hostname: server01 Icon name: computer-desktop Chassis: desktop Machine ID: f909adea404642a6a39aa69bdb978fd1 Boot ID: f9dddf54f03747eab54337123bfbd079 Operating System: Debian GNU/Linux 9 (stretch) Kernel: Linux 4.15.18-10-pve Architecture: x86-64 虚拟机: root@server02:~# hostnamectl Static hostname: server02 Icon name: computer-vm Chassis: vm Machine ID: f909adea404642a6a39aa69bdb978fd1 Boot ID: c3e2b56b6cbf425c8daf8dbc9ba46a44 Virtualization: qemu Operating System: Debian GNU/Linux 9 (stretch) Kernel: Linux 4.15.18-10-pve Architecture: x86-64 linux下第八种方法: dmidecode 物理机: root@server01:~# dmidecode -s system-manufacturer Hewlett-Packard root@server01:~# dmidecode -s bios-vendor AMI 虚拟机: root@server02:~# dmidecode -s system-manufacturer QEMU root@server02:~# dmidecode -s bios-vendor SeaBIOS 注明:虚拟机的Vendor一般是OVMF或者SeaBIOS linux下第九种方法: lshw 物理机: root@server01:~# lshw -class system server01 description: Desktop Computer product: HP ProDesk 490 G2 MT (L0J69PA#AB2) vendor: Hewlett-Packard serial: 6CR5126GKF width: 64 bits capabilities: smbios-2.8 dmi-2.8 smp vsyscall32 configuration: administrator_password=disabled boot=normal chassis=desktop family=103C_53307F G=D frontpanel_password=disabled keyboard_password=disabled power-on_password=disabled sku=L0J69PA#AB2 uuid=2F6D2D65-A077-4A0F-1AFA-A85FD9C09739 虚拟机: root@server02:~# lshw -class system server02 description: Computer product: Standard PC (i440FX + PIIX, 1996) vendor: QEMU version: pc-i440fx-3.0 width: 64 bits capabilities: smbios-2.8 dmi-2.8 smp vsyscall32 configuration: boot=normal uuid=71BAA06F-406A-0B49-ACA3-6EF9D267C75B