判断服务器是否为虚拟机的几种方法

推荐使用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
    width: 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
    width: 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
    width: 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)
    width: 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
    width: 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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容