How to detect the type of virtualization from the command line

Question: I am trying to write a shell script in which I need to detect whether the Linux server on which the script is running is a virtual machine, a container or actual bare-metal hardware. This is because the script should perform different tasks depending on whether the underlying server is virtual or physical. Is there an easy way to detect (from the command line) whether I am in a virtualized environment, and if so what type of virtualization is used?

If you want to find out whether the Linux OS is running inside a virtualized environment or on a real hardware computer, there are various heuristics you can try, depending on the type of hypervisor/container used underneath. Different hypervisors and container technologies may introduce different fingerprints within their instances (e.g., processor manufacturer, special file in /proc, virtualized NIC driver name). Also the boot sequence shown by dmesg can reveal some clues whether the OS is virtualized.

Fortunately, there are several command-line tools that make it far easier for end users to detect the type of virtualization technology used underneath Linux OS.

Method One: dmidecode

One way to detect the type of underlying virtualization is via dmidecode command, which is originally designed to show information about system's BIOS and hardware components. In particular, use the following command to detect virtualization:

$ sudo dmidecode -s system-manufacturer

If you are running this command on a physical server, the output will be the actual name of the hardware manufacturer (e.g., "Dell Inc."). On the other hand, if you run the command on a virtual machine, it will show the name of virtualization technology (e.g., "QEMU", "Xen", "VirtualBox", "VMware, Inc.").

Note that the dmidecode command does not work within containers as container-based virtualization does not create device node entries in the container's /dev directory which is used by dmidecode.

Method Two: systemd

If you use systemd on your Linux system, you can use a command-line tool called systemd-detect-virt which comes with systemd. This command can detect both hypervisor-based (e.g., KVM, QEMU, VMware, Xen, Oracle VM, VirtualBox, UML) and container-based (e.g., LXC, Docker, OpenVZ) virtualization.

$ systemd-detect-virt

On a physical server, the output of systemd-detect-virt will be "none". When run inside a virtual machine or a container, systemd-detect-virt will show the name of the virtualization technology used (e.g., "lxc", "kvm").

Method Three: virt-what

Another way to detect a virtualized environment within a terminal is by using virt-what. This command is actually a shell-script that uses various heuristics to identify the type of a virtualized environment you are in. It can detect QEMU/KVM, VMware, Hyper-V, VirtualBox, OpenVZ/Virtuozzo, Xen, LXC, IBM PowerVM, Parallels, etc.

To install virt-what on Debian-based system:

$ sudo apt-get install virt-what

To install virt-what on Red Hat-based system:

$ sudo yum install virt-what

To determine whether you are on a physical server or a virtual server, run the command with root privilege:

$ sudo virt-what

If you are on a physical server, the command will print nothing. If you are on a virtual machine or a container, it will print the type of virtualization.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

2 thoughts on “How to detect the type of virtualization from the command line

  1. I just look at the virtual hard disk Model
    # parted /dev/sda print

    Output Example:
    Model: VMware Virtual disk (scsi)

Leave a comment

Your email address will not be published. Required fields are marked *