如何检测 Linux 系统运行在物理机还是虚拟机上 (Virtual)

1. How To Check If A Linux System Is Physical Or Virtual Machine

As a Linux administrator, sometimes you might be wondering whether you are working on a physical or virtual machine. Most of the time, you will be accessing and managing your servers remotely. You may not always have physical access of your servers, and you may not even actually know where the server is located. However, it is possible to check if your Linux system is physical or virtual machine using couple of Linux utilities.

This guide lists all possible ways to check whether the Linux system you’re working on is a physical server or a virtual server.

Check if a Linux system is physical or virtual machine

There can be many ways to find if a system is physical or virtual. I am aware of the following methods at present. I will update if I find any other ways in the days to come.

Method 1 - Using Dmidecode utility

The easiest way to find if we are working on a virtual or physical machine is using dmidecode utility. Dmidecode, DMI table decoder, is used to find your system’s hardware components, as well as other useful information such as serial numbers and BIOS revision.

Dmidecode comes pre-installed with most Linux distributions. Just in case, if it is not installed already, you can install it using your distribution’s package manager. Say for example, the following command will install dmidecode in DEB based systems such as Ubuntu, Linux Mint.

$ sudo apt-get install dmidecode

After installing Dmidecode, run the following command to find out whether your system is a physical or virtual machine:

$ sudo dmidecode -s system-manufacturer

If it is a physical system, you will get an output something like below.

Dell Inc.

If it is virtual system created with Virtualbox, you will get the following output:

innotek GmbH

For those wondering, innotek is a German-based software company that develops PC virtualization software called VirtualBox.

If it is virtual system created with KVM/QEMU, the output will be:

QEMU

As you see in the above output, if it is a physical system, dmidecode will show the manufacturer’s name (i.e Dell Inc.). If it is a virtual system, then it will show the virtualization software/technology (i.e VirtualBox or QEMU).

Also, you can use this command to check if it is physical or virtual system.

$ sudo dmidecode | grep Product

Sample output:

[Physical system]

Product Name: 01HXXJ
Product Name: Inspiron N5050

[Virtual system on VirtualBox]

Product Name: VirtualBox

[Virtual system on KVM/QEMU]

Product Name: Standard PC (Q35 + ICH9, 2009)

Another command to find out if it is a physical or virtual system is:

$ sudo dmidecode -s system-product-name

Sample output:

[Physical system]

Inspiron N5050

[Virtual system on VirtualBox]

VirtualBox

[Virtual system on KVM/QEMU]

Standard PC (Q35 + ICH9, 2009)

Yet another dmidecode command to find the remote system’s type is:

$ sudo dmidecode | egrep -i 'manufacturer|product'

Sample output:

[Physical system]

 Manufacturer: Intel 
 Manufacturer: Sanyo 
 Manufacturer: Not Specified
 Manufacturer: Dell Inc.
 Product Name: 01HXXJ
 Manufacturer: Dell Inc.
 Product Name: Inspiron N5050
 Manufacturer: 014F

[Virtual system on VirtualBox]

 Manufacturer: innotek GmbH
 Product Name: VirtualBox
 Manufacturer: Oracle Corporation
 Product Name: VirtualBox
 Manufacturer: Oracle Corporation

[Virtual system on KVM/QEMU]

Manufacturer: QEMU
Product Name: Standard PC (Q35 + ICH9, 2009)
Manufacturer: QEMU

And, one more dmidecode command is to achieve the same goal:

$ sudo dmidecode | egrep -i 'vendor'

Sample output:

[Physical system]

Vendor: Dell Inc.

[Virtual system on VirtualBox]

Vendor: innotek GmbH

[Virtual system on KVM/QEMU]

Vendor: EFI Development Kit II / OVMF

Method 2 - Using Facter utility

Facter is a command line utility to collect and display a system’s information. Unlike Dmidecode, Facter doesn’t comes pre-installed by default. You may need to install it as shown below depending upon the Linux distribution you use.

In Arch Linux, Manjaro Linux:

$ sudo pacman -S facter

In Fedora:

$ sudo dnf install facter

In CentOS, RHEL:

$ sudo yum install epel-release
$ sudo yum installl facter

In openSUSE:

$ sudo zypper install facter

Once facter installed, run the following command to check if the system is physical or virtual machine:

$ facter 2> /dev/null | grep virtual

If this command doesn’t work, try with sudo privileges:

$ sudo facter 2> /dev/null | grep virtual

Sample output:

[Physical system]

is_virtual => false
virtual => physical

[Virtual system on VirtualBox and KVM/QEMU]

is_virtual => true
virtual => kvm

Alternatively, use the following command:

$ facter virtual

Or,

$ sudo facter virtual

If it is physical machine, the output will be:

physical

If it is virtual machine, you will see output something like below.

kvm

Method 3 - Using lshw utility

The lshw utility is a small command line utility that displays the detailed hardware information of a Unix-like system. It displays all hardware details including memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc.

Some Linux distributions comes pre-installed with lshw. If it is not installed already, you can install it as shown below.

In Arch Linux and derivatives:

$ sudo pacman -S lshw
In Fedora:

$ sudo dnf install lshw
In RHEL and derivatives such as CentOS, scientific Linux:

$ sudo yum install epel-release
$ sudo yum install lshw
In Debian, Ubuntu, Linux Mint:

$ sudo apt-get install lshw
In SUSE/openSUSE:

$ sudo zypper in lshw
After installing lshw, run the following command to find out if your system is either physical or virtual:

$ sudo lshw -class system
Sample output:

[Physical system]

sk 
 description: Portable Computer
 product: Inspiron N5050 (To be filled by O.E.M.)
 vendor: Dell Inc.
 version: Not Specified
 serial: JSQ9PR1
 width: 4294967295 bits
 capabilities: smbios-2.6 dmi-2.6 smp vsyscall32
 configuration: boot=normal chassis=portable sku=To be filled by O.E.M. uuid=44454C4C-5300-1051-8039-CAC04F505231

[Virtual system on VirtualBox]

ubuntuserver 
 description: Computer
 product: VirtualBox
 vendor: innotek GmbH
 version: 1.2
 serial: 0
 width: 64 bits
 capabilities: smbios-2.5 dmi-2.5 vsyscall32
 configuration: family=Virtual Machine uuid=78B58916-4074-42E2-860F-7CAF39B5E6F5

[Virtual system on KVM/QEMU]

centos8uefi.ostechnix.lan   
    description: Computer
    product: Standard PC (Q35 + ICH9, 2009)
    vendor: QEMU
    version: pc-q35-4.2
    width: 64 bits
    capabilities: smbios-2.8 dmi-2.8 smp vsyscall32
    configuration: boot=normal uuid=C40041DE-2E63-094C-8DCF-BBDE29170268
  *-pnp00:00
       product: PnP device PNP0b00
       physical id: 1
       capabilities: pnp
       configuration: driver=rtc_cmos

Method 4 - Using dmesg utility

We can find the system’s type using dmesg utility. dmesg is used to examine or control the kernel ring buffer.

To check if your Linux system is physical or virtual, simply run:

$ sudo dmesg | grep “Hypervisor detected”
If your system is physical, you will not see any output.

If your system is virtual machine, then you will see an output something like below.

[ 0.000000] Hypervisor detected: KVM

Method 5 - Using hostnamectl command

We can find if out system is either virtual or physical using hostnamectl command. It requires systemd to work.

$ hostnamectl status
Or,

$ hostnamectl

Sample output:

[Physical system]

Static hostname: sk
 Icon name: computer-laptop
 Chassis: laptop
 Machine ID: 84e3c8e37e114ac9bc9f69689b49cfaa
 Boot ID: 19cf3572e1634e778b5d494d9c1af6e9
 Operating System: Arch Linux
 Kernel: Linux 4.10.13-1-ARCH
 Architecture: x86-64

[Virtual system on VirtualBox]

Static hostname: ubuntuserver
 Icon name: computer-vm
 Chassis: vm
 Machine ID: 2befe86cf8887ba098b509e457554beb
 Boot ID: 8021c02d65dc46a1885afb25dddcf18c
 Virtualization: oracle
 Operating System: Ubuntu 16.04.1 LTS
 Kernel: Linux 4.4.0-78-generic
 Architecture: x86-64

[Virtual system on KVM/QEMU]

Static hostname: centos8uefi.ostechnix.lan
Icon name: computer-vm
Chassis: vm
Machine ID: de4100c4632e4c098dcfbbde29170268
Boot ID: 6136783bb9c241d08c8901aeecc7c30d
Virtualization: kvm
Operating System: CentOS Linux 8 (Core)
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-80.el8.x86_64
Architecture: x86-64

Method 6 - Using systemd-detect-virt

The systemd-detect-virt tool detects the virtualization technology and can distinguish full machine virtualization from hardware or container virtualization.

Run the following command to check if the system is physical or virtual:

$ systemd-detect-virt
Sample output:

[Physical machine]

none

[Virtual machine on VirtualBox]

oracle

[Virtual machine on KVM/QEMU]

KVM

Method 7 - Using virt-what script

The virt-what is a small shell script developed at Red Hat to find if we are running in a virtual machine or physical machine. virt-what is packaged for all popular Linux distributions, such as RHEL, Fedora, CentOS, Debian, Ubuntu, Arch Linux (AUR).

In Arch Linux, you can install it from AUR using any AUR helpers, for example Yay.

$ yay -S virt-what
In RHEL, Fedora, CentOS:

$ sudo yum install virt-what
On Debian, Ubuntu:

$ sudo apt-get install virt-what
Once installed, run the following command to display to find if your system is either physical or virtual:

$ sudo virt-what
If nothing is printed and the script exits with code 0 (no error), then it means that either system is physical or a type of virtual machine which we don’t know about or cannot detect.

If your system is Virtual, you will see an output like below.

virtualbox
kvm
For more details, refer the project’s homepage.

http://people.redhat.com/~rjones/virt-what/

Method 8 - Using imvirt script

The imvirt is yet another little perl script that helps you to detect if we’re running on a virtual machine.

In Arch Linux, you can install it from AUR using Yay helper program.

$ yay -S imvirt
On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install imvirt
Once installed, run the following command to display to find if your system is either physical or virtual:

$ sudo imvirt
If your system is physical, the output would be:

Physical
if the system is virtual, you will see:

KVM
For more details, refer the project’s homepage.

http://micky.ibh.net/~liske/imvirt.html

Community member: DANNYB2

Here are a few results on Microsoft Hyper-V.

$ sudo dmidecode -s system-manufacturer
Microsoft Corporation

$ sudo dmidecode | grep Product
Product Name: Virtual Machine

$ sudo dmidecode -s system-product-name
Virtual Machine

$ sudo dmidecode | egrep -i ‘manufacturer|product’
Manufacturer: Microsoft Corporation
Product Name: Virtual Machine
Manufacturer: Microsoft Corporation
Product Name: Virtual Machine
Manufacturer: Microsoft Corporation
Manufacturer: Intel(R) Corporation
Manufacturer: None
. . . . many more omitted . . . .
Manufacturer: None
Manufacturer: Microsoft
. . . . many more omitted . . . .
Manufacturer: Microsoft

$ sudo dmidecode | egrep -i ‘vendor’
Vendor: American Megatrends Inc.
Vendor Syndrome: Unknown

$ sudo lshw -class system
danny-lm18
description: Desktop Computer
product: Virtual Machine
vendor: Microsoft Corporation
version: 7.0
serial: 3503-1139-3762-7885-5468-5100-00
width: 64 bits
capabilities: smbios-2.3 dmi-2.3 vsyscall32
configuration: boot=normal chassis=desktop uuid=087CC8B8-04E7-8647-85C7-F79E74FA4A5F

$ sudo dmesg | grep "Hypervisor detected"
[ 0.000000] Hypervisor detected: Microsoft HyperV

Community member: WARREN GOODWIN

I prefer just to query the disk myself…

# parted /dev/sda print
Model: VMware Virtual disk (scsi)

Community member: BERTRAND ZIDLER

Another way with inxi:

inxi -Fxz | grep Machine
Machine: Type: Laptop System: Notebook product: N130BU v: N/A serial: N/A

Community member: BRUCE FERRELL

On a Virtualbox VM, (and I think VMWare) most of the setting being tested for can be manipulated before the VM starts… Especially those gathered by dmidecode

I don’t think the ring buffer can be avoided though.

Community member: MARK

Run

lscpu

And look at the hypervisor and virtualization values

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云满笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值