See if your linux server is running on a virtual machine or not

Virtual machines try to trick their operating systems into thinking they are running on natural hardware. See how you can tell from the linux command line if the computer is physical or virtul.

matrix red pill blue nio

Virtual Machines and Hypervisors

A traditional computer is a physical object. It's a collection of different pieces of hardware that are connected and bolted together so you can load one system, install applications, launch and use them.

The material is expensive. Limiting it to one operating system per physical computer means that the operating costs of many operating systems become prohibitive. The best solution would be to allow a single physical computer to run a selection of operating systems at the same time, each believing that it is running on its own unique hardware.

A hypervisor makes this possible. A hypervisor (also called a virtual machine manager or virtual machine monitor) is software that allows you to create virtual machines. They behave as if they were individual, physical computers, even though they run on the same physical server, sharing hard disk space, memory, and CPU cores.

Of course, the host must be powerful enough to handle the demands of all the virtual machines, but given enough RAM and lots of processing power on the host, the virtual machines can run at speeds almost as if they were to separately shit.

Since the release of kernel 2.6.20 in 2007, Linux has had direct support Virtual Machine based on Kernel. Linux has several hypervisors available, such as VirtualBox, GNOME Boxes and QEMU-KVM. They use the inherent KVM feature of Linux, relying on native kernel functionality, and adding additional features, such as the ability to take a snapshot of a virtual machine.

Virtual machines bring cost savings, efficiency, simplified solutions and (with the right provision) security benefits. They also facilitate scalability. New servers can be created automatically as demand for a service increases and terminate when demand falls. This makes them extremely popular both in the cloud and in the internal infrastructure of a data center.

If you are managing a Linux server remotely you need to know if it is a virtual machine or a physical box. But also other information, such as on what type of platform it runs. Here are some ways in which you can determine if your computer is physical or virtual.

The dmidecode command

The dmidecode command supports a large number of options and switches. Interrogates Desktop Management Interface (DMI) tables and prints the information in the ssh terminal window.

You can use it with the -s switch and thus request the system product name. Note that you must use sudo.

The command is configured as follows: sudo dmidecode -s system--name

This command in a VirtualBox VM running Ubuntu 22.04 will give:
ubuntu dmidecode
The platform is correctly identified as VirtualBox.

While in a QEMU-KVM VM running Fedora 35, you will see.
fedora dmidecode
Although referred to as a standard computer, it is a standard QEMU Q35 virtual machine. Thus the platform is correctly recognized as a virtual machine.

If you run the same command on a physical computer, you will receive some information about the manufacturer.
hw dmidecode
This computer is a custom build based on the Micro-Star International Company Limited motherboard, with product code MS-7B86.

The lshw command

The lshw command lists the details of a wide range of computer hardware. We can choose for which material category we want lshw to report.

Here we will use the -class option with the system modifier. Using sudo in this command ensures that you see all the details.

The command is configured as follows: sudo lshw -class system

If you run it on a VirtualBox running Ubuntu.
ubuntu lshw

  • Το πεδίο "description" (περιγραφή) έχει μια γενική καταχώριση "υπολογιστής".
  • Το πεδίο "product" (προϊόν) λέει ότι πρόκειται για μια εικονική μηχανή που τρέχει στο VirtualBox.
  • Το πεδίο "vendor" (πωλητής) περιέχει το όνομα της γερμανικής s that created VirtualBox, Innotek GmbH. Innotek was acquired by Oracle Corporation in 2010 as part of its acquisition of Sun Microsystems, Inc.

Similarly in Fedora running on GNOME Boxes you will receive
fedora lshw

  • Και πάλι, το πεδίο "description" έχει μια γενική καταχώριση "υπολογιστής".
  • Το πεδίο "product" δίνει τις ίδιες τυπικές πληροφορίες υπολογιστή QEMU που είδαμε με την εντολή dmidecode.
  • Το πεδίο "vendor" περιέχει το "QEMU", το οποίο δηλώνει ξεκάθαρα ότι πρόκειται για εικονική μηχανή.

This is the result of executing the same command on our physical computer.
hw lshw 1

You can see that this is a physical computer, with a Micro-Star motherboard.

  • The hardware is identified as a desktop computer.
  • Το πεδίο "product" δίνει τον τύπο μητρικής πλακέτας, MS-7B86.
  • Το πεδίο "vendor" περιέχει το όνομα του κατασκευαστή.

The hostnamectl command

This command has the advantage that you do not need to have sudo permissions to execute it. However, it is only available in distributions with systemd. The majority of modern distributions use systemd.

The command is configured as follows: hostnamectl

This is the answer to running the command in VirtualBox VM with Ubuntu.
ubuntu hostnamectl

  • Στο πεδίο "icon-name" έχει προσαρτηθεί το "-vm".
  • Το πεδίο "Chassis" (Πλαίσιο) περιέχει το "vm".
  • Το πεδίο "Virtualization" περιέχει το "oracle".
  • Το πεδίο "Hardware Vendor" (Πωλητής υλικού) περιέχει "innotek GmbH".
  • Το πεδίο "Hardware Model" (Μοντέλο υλικού) περιέχει "VirtualBox".

The results from the Feroda's GNOME Boxes VM are similar.
fedora hostnamectl

  • The "icon-name" field displays "-vm".
  • In the field "Chassis" there is "vm".
  • The "Virtualization" field contains "kvm".
  • In the field "Hardware Vendor" there is "QEMU"
  • "Standard PC (Q35 + ICH9, 2009)" appears in the "Hardware Model" field.

If you use the hostnamectl command on a physical computer, you will see that the "Virtualization" bar does not appear in the results.
hw hostnamectl

If you do not see the "Virtualization" field, then you have a physical computer in front of you.

The systemd-detect-virt command

If you want to get as short an answer as possible, the systemd-detect-virt command is probably what you are looking for. Again this requires a distribution equipped with systemd, but does not require sudo privileges.

The command is configured as follows: systemd-detect-virt

This is the result of running the command in VirtualBox with Ubuntu.
ubuntu systemd detect virt

Respectively in the GNOME boxes running Feroda
fedora systemd detect virt

Η εκτέλεση της εντολής systemd-detect-virt στο φυσικό μηχάνημα έχει ως αποτέλεσμα να εκτυπωθεί "nome" (κανένα) στο τερματικό.
hw systemd detect virt

Στην εντολή αυτή θα μπορούσατε να προσθέσετε περισσότερους διακόπτες για μεγαλύτερη αναζήτηση. Μπορείτε να δείτε την πλήρη λίστα της εντολής χρησιμοποιώντας την επιλογή --list.

And to make it easier to see the list provided you put in the command and the column.
systemd-detect-virt --list | column

The above command will return you:
system detect virt list

Take the red pill
These techniques let you know when a linux distribution is running on physical hardware and when it is inside a virtual machine.

Like Nio in the Matrix, you will know what is real and what is not.

iGuRu.gr The Best Technology Site in Greecefgns

every publication, directly to your inbox

Join the 2.087 registrants.
linux, virtual, machine, systemd, hostnamectl, dmidecode

Written by Dimitris

Dimitris hates on Mondays .....

Leave a reply

Your email address is not published. Required fields are mentioned with *

Your message will not be published if:
1. Contains insulting, defamatory, racist, offensive or inappropriate comments.
2. Causes harm to minors.
3. It interferes with the privacy and individual and social rights of other users.
4. Advertises products or services or websites.
5. Contains personal information (address, phone, etc.).