☁️ How to check if a Linux system is a physical or virtual machine

How do you know if you are working on a physical or virtual machine? Most of the time, we access and manage our servers remotely. You may often not have physical access to your servers, and you may not even know where the server is. However, you can check if your Linux system is a physical or virtual machine using a couple of Linux utilities. This guide lists all the possible ways to check if the Linux system you are running on is a physical or virtual server.

How to determine if a Linux system is a physical or virtual machine

There are many ways to determine if a system is physical or virtual.

I am currently aware of the following methods.

I’ll let you know if I find other ways in the coming days.

Method 1 – Using the Dmidecode Utility

The easiest way to find out if we are running in a virtual or physical machine is to use the dmidecode utility.

Dmidecode, the DMI table decoder, is used to find the hardware components of your system, as well as other useful information such as serial numbers and BIOS version.

How to get system information on Ubuntu Linux

Dmidecode is preinstalled on most Linux distributions.

Just in case it is not already installed, you can install it using your distribution’s package manager.

Let’s say for example the following command will install dmidecode on DEB based systems like Ubuntu, Linux Mint.

                      $ sudo apt-get install dmidecode
                    

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

                      $ sudo dmidecode -s system-manufacturer
                    

If it is a physical system, you will get an output similar to the one below.

                      Dell Inc.
                    

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

                      innotek GmbH
                    

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

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

                      QEMU
                    

As you can see from the above output, if it is a physical system, dmidecode will show the manufacturer’s name (e.g. Dell Inc.).

If it is a virtual system, then it will show the virtualization software / technology (eg VirtualBox or QEMU).

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

                      $ sudo dmidecode | grep Product
                    

Output examples:

                      
                        [Физическая машина]
                      
                      

Product Name: 01HXXJ
Product Name: Inspiron N5050


                      
                        [VirtualBox]
                      
                      

Product Name: VirtualBox
Product Name: VirtualBox


                      
                        [KVM/QEMU]
                      
                      

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

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

                      $ sudo dmidecode -s system-product-name
                    

Output examples:

                      
                        [Физическая машина]
                      
                      

Inspiron N5050


                      
                        [VirtualBox]
                      
                      

VirtualBox


                      
                        [KVM/QEMU]
                      
                      

Standard PC (Q35 + ICH9, 2009)
                    

Another dmidecode command to determine the type of remote system:

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

Output examples:

                      
                        [Физическая машина]
                      
                      

 Manufacturer: Intel 
 Manufacturer: Sanyo 
 Manufacturer: Not Specified

                      
                         Manufacturer: Dell Inc.
                      
                      
 Product Name: 01HXXJ
 Manufacturer: Dell Inc.
 Manufacturer: Dell Inc.
 Product Name: Inspiron N5050
 Manufacturer: 014F


                      
                        [VirtualBox]
                      
                      

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


                      
                        [KVM/QEMU]
                      
                      
                        Manufacturer: QEMU
                      
                      
Product Name: Standard PC (Q35 + ICH9, 2009)
Manufacturer: QEMU
Manufacturer: QEMU
Manufacturer: QEMU
Manufacturer: QEMU
                    

And yet another dmidecode command is designed to achieve the same goal:

                      $ sudo dmidecode | egrep -i 'vendor'
                    

Output examples:

                      
                        [Физическая машина]
                      
                    
                      Vendor: Dell Inc.


                      
                        [VirtualBox]
                      
                      

Vendor: innotek GmbH


                      
                        [KVM/QEMU]
                      
                      

Vendor: EFI Development Kit II / OVMF
                    

Method 2 – Using the Facter Utility

Facter is a command line utility for collecting and displaying system information.

Unlike Dmidecode, Facter is not pre-installed by default.

You may need to install it as shown below, depending on the Linux distribution you are using.

                      $ sudo pacman -S facter
                    

On Fedora:

                      $ sudo dnf install facter
                    

On CentOS , RHEL:

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

On openSUSE:

                      $ sudo zypper install facter
                    

After installing facter, run the following command to check if the system is a 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
                    

Output examples:

                      
                        [Физическая машина]
                      
                    
                      is_virtual => false

                      
                        virtual => physical
                      
                      
                        [VirtualBox и KVM/QEMU]
                      
                      

is_virtual => true

                      
                        virtual => kvm
                      
                    

Alternatively, use the following command:

                      $ facter virtual
                    

or

                      $ sudo facter virtual
                    

If it is a physical machine, the output would be:

                      physical
                    

If it is a virtual machine, you will see the output as shown below:

                      kvm
                    

Method 3 – Using the lshw utility

The lshw utility is a small command line utility that displays detailed information about the hardware of a Unix-like system.

It displays all the hardware details, including memory configuration, firmware version, motherboard configuration, processor version and speed, cache configuration, bus speed, etc.

Some Linux distributions come with lshw preinstalled.

If not already installed, you can install it as shown below.

On Arch Linux and derivatives:

                      $ sudo pacman -S lshw
                    

Fedora:

                      $ sudo dnf install lshw
                    

In RHEL and derivatives like CentOS, scientific Linux:

                      $ sudo yum install epel-release
                    
                      $ sudo yum install lshw
                    

On Debian , Ubuntu, Linux Mint:

                      $ sudo apt-get install lshw
                    

On SUSE / openSUSE:

                      $ sudo zypper in lshw
                    

After installing lshw, run the following command to find out if your system is physical or virtual:

                      $ sudo lshw -class system
                    

Output examples:

                      
                        [Физическая машина]
                      
                    
                      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


                      
                        [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


                      
                        [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 the dmesg utility

You can find out the type of system using the dmesg utility.

dmesg is used to check or manipulate 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 a virtual machine, you will see an output similar to the one below.

                      [ 0.000000] Hypervisor detected: KVM
                    

Method 5 – Using the hostnamectl Command

We can find out if our system is virtual or physical using the hostnamectl command.

Requires systemd to work.

                      $ hostnamectl status
                    

or

                      $ hostnamectl
                    

Output examples:

                      
                        [Физическая машина]
                      
                    
                      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


                      
                        [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


                      
                        [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 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
                    

Output examples:

                      
                        [Физическая машина]
                      
                    
                      none


                      
                        [Virtual machine on VirtualBox]
                      
                      

oracle


                      
                        [Virtual machine on KVM/QEMU]


                      
                      KVM
                    

Method 7 – Using the virt-what script

Virt-what is a small shell script developed by Red Hat to determine if we are running in a virtual or physical machine.

virt-what is compiled for all popular Linux distributions such as RHEL, Fedora, CentOS, Debian, Ubuntu, Arch Linux (AUR).

On Arch Linux, you can install it from the AUR using any AUR helpers like Yay.

                      $ yay -S virt-what
                    

On RHEL, Fedora, CentOS:

                      $ sudo yum install virt-what
                    

On Debian, Ubuntu:

                      $ sudo apt-get install virt-what
                    

After installation, run the following command to display if your system is physical or virtual:

                      $ sudo virt-what
                    

If nothing is printed and the screen returns with code 0 (no errors), this means that either the system is physical or it is a type of virtual machine that we do not know about or cannot detect.

If your system is virtual, you will see the output as shown below.

                      virtualbox
kvm
                    

Method 8 – Using the imvirt script

Imvirt is another small Perl script that helps you determine if we are running in a virtual machine.

On Arch Linux, you can install it from the AUR using the Yay helper.

                      $ yay -S imvirt
                    

On Debian, Ubuntu, Linux Mint:

                      $ sudo apt-get install imvirt
                    

After installation, run the following command to display if your system is physical or virtual:

                      $ sudo imvirt
                    

If your system is physical, the output will be:

                      Physical
                    

if the system is virtual, you will see:

                      KVM
                    

Related Posts