How to check if the environment is a virtual machine in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if your Linux environment is running on a virtual machine. We will explore three different methods to achieve this: using the dmidecode command to inspect system information, utilizing the virt-what tool specifically designed for virtualization detection, and examining CPU flags within the /proc/cpuinfo file. By the end of this lab, you will be equipped with practical techniques to identify virtualized environments in Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux/BasicSystemCommandsGroup -.-> linux/man("Manual Access") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/UserandGroupManagementGroup -.-> linux/sudo("Privilege Granting") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") subgraph Lab Skills linux/man -.-> lab-558808{{"How to check if the environment is a virtual machine in Linux"}} linux/cat -.-> lab-558808{{"How to check if the environment is a virtual machine in Linux"}} linux/grep -.-> lab-558808{{"How to check if the environment is a virtual machine in Linux"}} linux/sudo -.-> lab-558808{{"How to check if the environment is a virtual machine in Linux"}} linux/apt -.-> lab-558808{{"How to check if the environment is a virtual machine in Linux"}} end

Check system info with dmidecode

In this step, you'll learn how to use the dmidecode command to retrieve detailed information about your system's hardware. dmidecode reads data from the DMI/SMBIOS (Desktop Management Interface / System Management BIOS) table, which contains descriptions of the system's components.

This command is incredibly useful for understanding the specifics of the hardware your Linux system is running on, such as the motherboard, CPU, memory, and more.

Since accessing the DMI table requires root privileges, you'll need to use sudo with dmidecode.

Open your terminal if it's not already open. Remember, you can find the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

sudo dmidecode

This command will output a large amount of information about your system's hardware. Scroll through the output to see the different sections, such as "BIOS Information", "System Information", "Base Board Information", "Processor Information", "Memory Device", etc.

The output might look something like this (parts of the output are omitted for brevity):

## dmidecode 3.3
Getting SMBIOS data from sysfs.
SMBIOS 3.3.0 present.

Handle 0x0000, DMI type 0, 26 bytes
BIOS Information
        Vendor: SeaBIOS
        Version: 1.16.0-1.fc36
        Release Date: 07/07/2022
        Address: 0xE0000
        Runtime Size: 128 kB
        ROM Size: 64 kB
        Characteristics:
                PCI is supported
                BIOS is upgradeable
                BIOS shadowing is allowed
                Boot from CD is supported
                Selectable boot is supported
                BIOS ROM is socketed
                EDD is supported
                5.25" / 1.2 MB floppy services are supported (int 13h)
                3.5" / 720 kB floppy services are supported (int 13h)
                3.5" / 1.44 MB floppy services are supported (int 13h)
                8042 keyboard services are supported (int 9h)
                CGA/mono video services are supported (int 10h)
                AT/XT compatible keyboard services are supported (int 16h)
                Printer services are supported (int 17h)
                CGA/mono video is supported (int 10h)
                ACPI is supported
                USB legacy is supported
                BIOS boot specification is supported
                Targeted content distribution is supported
                UEFI is supported
        BIOS Revision: 1.16

Handle 0x0001, DMI type 1, 27 bytes
System Information
        Manufacturer: QEMU
        ProductName: Standard PC (Q35 + ICH9, 2009)
        Version: pc-q35-7.0
        Serial Number: <filtered>
        UUID: <filtered>
        Wake-up Type: Power Switch
        SKU Number: <filtered>
        Family: Virtual Machine

Handle 0x0002, DMI type 2, 17 bytes
Base Board Information
        Manufacturer: QEMU
        ProductName: Standard PC (Q35 + ICH9, 2009)
        Version: pc-q35-7.0
        Serial Number: <filtered>
        Asset Tag: <filtered>
        Features:
                Board is a hosting board
                Board is replaceable
        Location In Chassis: <filtered>
        Chassis Handle: 0x0003
        Type: Motherboard
        Contained Object Handles: 0

... (output continues)

You can also filter the output to see information about a specific type of hardware using the -t option followed by the type number or keyword. For example, to see only the CPU information, you can use:

sudo dmidecode -t processor

Or, to see information about the memory devices:

sudo dmidecode -t memory

Experiment with different types like bios, system, baseboard, chassis, etc., to see the specific details for each component.

Using dmidecode is a fundamental skill for system administrators and anyone who needs to troubleshoot hardware issues or simply understand the underlying system configuration.

Click Continue to proceed to the next step.

Detect virtualization with virt-what

In this step, you'll learn how to determine if your Linux system is running inside a virtual machine or on bare metal hardware. This is often important for system configuration, performance tuning, or licensing.

We'll use the virt-what command, which is specifically designed for this purpose. It checks for signs of virtualization and prints the names of the hypervisors it detects.

First, you need to install the virt-what package. Use apt just like you did for htop in the previous lab.

Open your terminal and run the following command to install virt-what:

sudo apt update
sudo apt install virt-what -y

The -y flag automatically answers "yes" to any prompts during the installation, making the process non-interactive.

Once the installation is complete, you can run the virt-what command:

virt-what

The output of this command will tell you if the system is virtualized and, if so, which virtualization technology is being used.

In the LabEx environment, which runs in a virtualized environment, you should see output similar to this:

qemu
kvm

This indicates that the system is running within a QEMU/KVM virtual machine. If the system were running on physical hardware, virt-what would typically produce no output.

Understanding whether your system is virtualized is a crucial piece of information for many system administration tasks. virt-what provides a simple and reliable way to get this information.

Click Continue to move on to the next step.

Inspect CPU flags in /proc/cpuinfo

In this step, you'll explore the /proc/cpuinfo file, which is a virtual file provided by the Linux kernel that contains information about the CPU(s) in your system. This file is a great source of details about your processor, including its model, speed, cache size, and supported features (flags).

The /proc filesystem is a pseudo-filesystem that provides an interface to kernel data structures. Files within /proc are not stored on disk but are generated on the fly by the kernel when you access them.

To view the contents of /proc/cpuinfo, you can use a command-line utility like cat or less. cat will print the entire content to the terminal, while less allows you to scroll through the content page by page. Since /proc/cpuinfo can be quite long, less is often more convenient.

Open your terminal and type the following command to view the CPU information using cat:

cat /proc/cpuinfo

You will see detailed information for each CPU core in your system. The output will include lines like processor, vendor_id, model name, cpu MHz, cache size, and flags.

Here's an example of what you might see (parts of the output are omitted for brevity):

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 158
model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
stepping        : 13
microcode       : 0xffffffff
cpu MHz         : 2904.000
cache size      : 16384 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves arch_capabilities
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds mmio_stale_data retbleed
bogomips        : 5808.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 158
model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
stepping        : 13
microcode       : 0xffffffff
cpu MHz         : 2904.000
cache size      : 16384 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves arch_capabilities
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds mmio_stale_data retbleed
bogomips        : 5808.00
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

The flags line is particularly interesting as it lists the various features and extensions supported by your CPU. These flags indicate capabilities like virtualization support (vmx or svm), specific instruction sets (like sse, avx), and security features.

You can also use grep to search for specific information within /proc/cpuinfo. For example, to find the model name:

grep "model name" /proc/cpuinfo

This will output the model name for each CPU core:

model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
model name      : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz

Or, to check for the presence of a specific flag, like vmx (Intel VT-x virtualization support):

grep "vmx" /proc/cpuinfo

If vmx is present in the output, your CPU supports Intel VT-x virtualization.

Exploring /proc/cpuinfo is a fundamental way to understand the capabilities of your system's processor directly from the kernel's perspective.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a Linux environment is a virtual machine using several methods. You started by using the dmidecode command with sudo to access the DMI/SMBIOS table and retrieve detailed system hardware information, such as BIOS, system, and processor details. This command provides a comprehensive overview of the underlying hardware components.

Next, you will explore additional techniques to detect virtualization, including using the virt-what command and inspecting CPU flags in the /proc/cpuinfo file. These methods offer alternative ways to identify the presence of a virtualized environment by examining specific system characteristics and configurations.