LinuxCommandLibrary

systemd-detect-virt

Detect virtualization environment

TLDR

List detectable virtualization technologies

$ systemd-detect-virt --list
copy

Detect virtualization, print the result and return a zero status code when running in a VM or a container, and a non-zero code otherwise
$ systemd-detect-virt
copy

Silently check without printing anything
$ systemd-detect-virt [[-q|--quiet]]
copy

Only detect container virtualization
$ systemd-detect-virt [[-c|--container]]
copy

Only detect hardware virtualization
$ systemd-detect-virt [[-v|--vm]]
copy

SYNOPSIS

systemd-detect-virt [OPTIONS]

PARAMETERS

--help, -h
    Show a brief help message and exit.

--version
    Show the package version string and exit.

--quiet, -q
    Do not print the detected virtualization type to standard output. Instead, the exit code indicates the result: 0 if virtualization is detected, 1 otherwise.

--vm, -r
    Only detect classic hardware virtualization environments (virtual machines). Containerization technologies are ignored.

--container, -c
    Only detect software containerization environments. Classic hardware virtualization environments are ignored.

--chroot
    Only detect if the system is running inside a chroot() environment. This is considered a specific type of containerization and implies --container.

DESCRIPTION

systemd-detect-virt is a utility provided by the systemd project designed to identify whether the operating system is running inside a virtualized environment or a container. It can pinpoint the specific hypervisor or container technology in use, such as QEMU, KVM, VMware, Microsoft Hyper-V, Oracle VirtualBox, Xen, Docker, LXC, systemd-nspawn, and others.

The command outputs the detected virtualization type to standard output, or 'none' if no virtualization is found. It's an essential tool for scripts and applications that need to adapt their behavior based on the execution environment, allowing for optimizations or specific configurations whether on bare metal, within a VM, or inside a container. It employs various heuristics including DMI information, CPU flags, and device presence for its determination.

CAVEATS

systemd-detect-virt relies on various heuristics which, while generally robust, might not always be foolproof or could potentially be spoofed in highly controlled or malicious environments. The accuracy of detection can also be influenced by the hypervisor's configuration, as some allow guests to appear more like bare metal. In nested virtualization setups, the command might report the outer hypervisor depending on how virtualization is exposed to the inner guest.

EXIT CODES

0: Virtualization or containerization was detected, or no specific type was requested with filtering options.
1: No virtualization or containerization was detected.
2: An invalid command line argument was specified.

HISTORY

systemd-detect-virt emerged as part of the systemd project, which began around 2010 as a modern replacement for the traditional SysVinit daemon in Linux. As systemd's scope expanded to encompass more aspects of operating system management, the need for reliable environment detection became critical, particularly with the widespread adoption of cloud computing, containers, and virtual machines. This utility was developed to provide a standardized and consistent method for systemd components and other applications to query their execution environment, ensuring seamless integration within the systemd ecosystem.

SEE ALSO

Copied to clipboard