判断服务器是否为虚拟机的几种方法
原文链接:https://baiyongjie.com/?p=710
推荐使用dmidecode或者lshw命令来查看,因为dmesg不能区分出云厂商,OpenStack,kvm.
并且在下面的实验中可以看到物理机,阿里云,微软云,kvm,OpenStack,VMware返回结果的区别
dmidecode
dmidecode命令可以在Linux系统下获取有关硬件方面的信息
物理机
# dmidecode -s system-product-name
ProLiant DL388 Gen10
阿里云
# dmidecode -s system-product-name
Alibaba Cloud ECS
OpenStack
# dmidecode -s system-product-name
OpenStack Nova
微软云
# dmidecode -s system-product-name
Virtual Machine
kvm
# dmidecode -s system-product-name
KVM
VMware
# dmidecode -s system-product-name
VMware Virtual platform
lshw
lshw(Hardware Lister)是另外一个可以查看硬件信息的工具
yum -y install lshw
物理机
# lshw -class system
baiyongjie-wuli
description: Rack Mount Chassis
product: ProLiant DL388 Gen10 (P02487-AA1)
vendor: HPE
serial: CN792507GG
64 bits
capabilities: smbios-3.2.0 dmi-3.2.0 smp vsyscall32
configuration: boot=normal chassis=rackmount family=ProLiant sku=P02487-AA1 uuid=50303234-3837-434E-3739-323530374747
*-pnp00:00
product: PnP device PNP0b00
physical id: ab
capabilities: pnp
configuration: driver=rtc_cmos
*-pnp00:01
product: PnP device PNP0c02
physical id: ac
capabilities: pnp
configuration: driver=system
阿里云
# lshw -class system -json
{
"id" : "baiyongjie",
"class" : "system",
"claimed" : true,
"handle" : "DMI:0100",
"description" : "Computer",
"product" : "Alibaba Cloud ECS",
"vendor" : "Alibaba Cloud",
"version" : "pc-i440fx-2.1",
"serial" : "54ff529b-697f-498b-afbd-3ad3cec1539c",
"width" : 64,
"configuration" : {
"boot" : "normal",
"uuid" : "9B52FF54-7F69-8B49-AFBD-3AD3CEC1539C"
},
"capabilities" : {
"smbios-2.8" : "SMBIOS version 2.8",
"dmi-2.8" : "DMI version 2.8",
"vsyscall32" : "32-bit processes"
},
"children" : [
{
"id" : "pnp00:00",
"class" : "system",
"claimed" : true,
"product" : "PnP device PNP0b00",
"physid" : "1",
"configuration" : {
"driver" : "rtc_cmos"
},
"capabilities" : {
"pnp" : true
}
},
]
}
OpenStack
# lshw -class system
baiyongjie-openstack
description: Computer
product: OpenStack Nova
vendor: OpenStack Foundation
version: 17.0.14
serial: 4c4c4544-0044-5410-804b-b9c04f355432
64 bits
capabilities: smbios-2.8 dmi-2.8 smp vsyscall32
configuration: boot=normal family=Virtual Machine uuid=2F8DD735-6BF4-AA41-87C4-551A28992FF6
*-pnp00:00
product: PnP device PNP0b00
physical id: 1
capabilities: pnp
configuration: driver=rtc_cmos
微软云
# lshw -class system
baiyongjie-azure
description: Desktop Computer
product: Virtual Machine
vendor: Microsoft Corporation
version: 7.0
serial: 0000-0000-3909-0980-4111-6125-78
64 bits
capabilities: smbios-2.3 dmi-2.3 smp vsyscall32
configuration: boot=normal chassis=desktop uuid=65CA6C05-4A4D-A24A-8B06-58E97E591DB7
*-pnp00:00
product: PnP device PNP0b00
physical id: 1
capabilities: pnp
configuration: driver=rtc_cmos
*-pnp00:06
product: PnP device PNP0c02
physical id: 48
capabilities: pnp
configuration: driver=system
*-pnp00:07
product: PnP device PNP0c02
physical id: 49
capabilities: pnp
configuration: driver=system
*-pnp00:08
product: PnP device PNP0c01
physical id: 4a
capabilities: pnp
configuration: driver=system
kvm
# lshw -class system
baiyongjie-kvm
description: Computer
product: KVM
vendor: Red Hat
version: RHEL 7.0.0 PC (i440FX + PIIX, 1996)
64 bits
capabilities: smbios-2.4 dmi-2.4 smp vsyscall32
configuration: boot=normal family=Red Hat Enterprise Linux uuid=B4D5D5A9-2789-46AA-BBFA-F40C9C9268C5
*-pnp00:00
product: PnP device PNP0b00
physical id: 1
capabilities: pnp
configuration: driver=rtc_cmos
VMware
baiyongjie-vmware-linux
description: Computer
product: VMware Virtual Platform
vendor: VMware, Inc.
version: None
serial: VMware-56 4d 64 8f 23 e2 ae 95-03 c4 76 fb 31 61 6c 96
64 bits
capabilities: smbios-2.4 dmi-2.4 smp vsyscall32
configuration: administrator_password=enabled boot=normal frontpanel_password=unknown keyboard_password=unknown power-on_password=disabled uuid=564D648F-23E2-AE95-03C4-76FB31616C96
*-pnp00:00
product: PnP device PNP0c02
physical id: 1
capabilities: pnp
configuration: driver=system
*-pnp00:01
product: PnP device PNP0b00
physical id: 2
capabilities: pnp
configuration: driver=rtc_cmos
dmesg
只能区分物理机,kvm和VMware,详细的厂商信息不能区分,不建议使用.
dmesg命令用于显示开机信息。
kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。开机信息亦保存在/var/log目录中,名称为dmesg的文件里。
物理机
物理机会显示为空
# dmesg | grep -i virtual
OpenStack
# dmesg | grep -i virtual
[ 0.000000] Booting paravirtualized kernel on KVM
[ 0.111423] KVM setup paravirtual spinlock
[ 0.709585] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input2
[ 0.709800] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input3
[ 0.722271] systemd[1]: Detected virtualization kvm.
[ 0.747779] systemd[1]: Starting Setup Virtual Console...
kvm
# dmesg | grep -i virtual
[ 0.000000] Booting paravirtualized kernel on KVM
[ 0.111423] KVM setup paravirtual spinlock
[ 0.709585] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input2
[ 0.709800] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input3
[ 0.722271] systemd[1]: Detected virtualization kvm.
[ 0.747779] systemd[1]: Starting Setup Virtual Console...
VMware
vmwar会显示VMware Virtual Platform字样
# dmesg | grep -i virtual
[ 0.000000] DMI: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 09/30/2014
[ 0.000000] Booting paravirtualized kernel on bare hardware
[ 4.394626] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input2
[ 4.394890] input: VirtualPS/2 VMware VMMouse as /devices/platform/i8042/serio1/input/input3
[ 4.419469] systemd[1]: Detected virtualization vmware.
[ 4.430184] systemd[1]: Starting Setup Virtual Console...
[ 4.511855] VMware vmxnet3 virtual NIC driver - version 1.4.7.0-k-NAPI
[ 4.610033] scsi 0:0:0:0: Direct-Access VMware Virtual disk 1.0 PQ: 0 ANSI: 2
[ 4.610424] scsi 0:0:1:0: Direct-Access VMware Virtual disk 1.0 PQ: 0 ANSI: 2
[ 4.773176] ata2.00: ATAPI: VMware Virtual IDE CDROM Drive, 00000001, max UDMA/33