{"id":453223,"date":"2022-05-26T20:00:43","date_gmt":"2022-05-26T17:00:43","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/"},"modified":"2022-05-26T20:00:43","modified_gmt":"2022-05-26T17:00:43","slug":"how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/","title":{"rendered":"#How to Let Linux Scripts Detect They\u2019re Running in Virtual Machines"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e6708f2ba0\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e6708f2ba0\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#%E2%80%9CHow_to_Let_Linux_Scripts_Detect_Theyre_Running_in_Virtual_Machines%E2%80%9D\" >&#8220;How to Let Linux Scripts Detect They\u2019re Running in Virtual Machines&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#Virtual_Machines_and_Hypervisors\" >Virtual Machines and Hypervisors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#The_dmidecode_Command\" >The dmidecode Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#The_lshw_Command\" >The lshw Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#The_hostnamectl_Command\" >The hostnamectl Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#The_systemd-detect-virt_Command\" >The systemd-detect-virt Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#A_Platform-Sensitive_Script\" >A Platform-Sensitive Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/#Take_the_Red_Pill\" >Take the Red Pill<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Let_Linux_Scripts_Detect_Theyre_Running_in_Virtual_Machines%E2%80%9D\"><\/span>&#8220;How to Let Linux Scripts Detect They\u2019re Running in Virtual Machines&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage wp-image-803871 size-full\" data-pagespeed-lazy-srcset=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/shutterstock_1164968740.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/shutterstock_1164968740.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/shutterstock_1164968740.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Two hands, one holding a red pill and the other a blue pill.\" width=\"1200\" height=\"675\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/red-pill-blue-concept-right-choice-1164968740\" data-credittext=\"diy13\/Shutterstock.com\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/red-pill-blue-concept-right-choice-1164968740\">diy13\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Virtual machines try really hard to convince their operating systems that they\u2019re running on physical hardware. So can you tell from the Linux command line if the computer is physical or virtual?<\/p>\n<h2 id=\"virtual-machines-and-hypervisors\"><span class=\"ez-toc-section\" id=\"Virtual_Machines_and_Hypervisors\"><\/span>Virtual Machines and Hypervisors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A traditional computer is a physical object. It\u2019s a collection of different pieces of hardware that are plugged and bolted together so that you can load an operating system, install <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications, launch them, and use them.<\/p>\n<p>Hardware is expensive.\u00a0Being restricted to one operating system per physical computer means the cost of running several operating systems soon becomes prohibitive. A better solution would be to allow a single physical computer to run a selection of operating systems at the same time, with each one thinking it\u2019s running in its own, unique hardware.<\/p>\n<p>A hypervisor makes this possible. A hypervisor\u2014also called a virtual machine manager or virtual machine monitor\u2014is software that lets you create virtual machines. These behave as though they were individual, physical computers although they run on the same physical host, sharing its hard drive space, memory, and CPU cores.<\/p>\n<p>Of course, the host computer has to be powerful enough to cope with the demands of the collection of virtual machines, but, given sufficient RAM and processing power in the host, virtual machines can run at near bare-metal speeds.<\/p>\n<p>Since the release of the 2.6.20 kernel in 2007, Linux has had\u00a0<strong>K<\/strong>ernel-based\u00a0<strong>V<\/strong>irtual\u00a0<strong>M<\/strong>achine support baked right in. Linux has several hypervisors available to it, such as\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.virtualbox.org\/\">VirtualBox<\/a>,\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/help.gnome.org\/users\/gnome-boxes\/stable\/\">GNOME Boxes<\/a>, and\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.qemu.org\/\">QEMU-KVM<\/a>. They make use of the native KVM capability of Linux, building upon the native kernel functionality by adding user interfaces and functionality such as being able to take a snapshot of a virtual machine.<\/p>\n<p>Virtual machines bring cost savings, efficiencies, simplified deployments, and\u2014provisioned correctly\u2014security benefits. They also facilitate scalability. New servers can be automatically spun up as demand for a service increases and shut down when demand drops. This makes them hugely popular both in the cloud and in on-premise infrastructure.<\/p>\n<p>Perhaps you\u2019re remotely administering a Linux server and you need to know whether it is a virtual machine or a physical box. Or you have a script that needs to know what type of platform it is executing on. Here are several ways you can detect if the computer you\u2019re working on is physical or virtual.<\/p>\n<h2 id=\"the-dmidecode-command\"><span class=\"ez-toc-section\" id=\"The_dmidecode_Command\"><\/span>The dmidecode Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>dmidecode<\/code> command supports a large number of options and modifiers. It interrogates the Desktop Management Interface (DMI) tables, and prints the information in the terminal window.<\/p>\n<p>We\u2019ll use it with the <code>-s<\/code> (display a single string) option, and ask for the system product name. Note that we must use <code>sudo<\/code>.<\/p>\n<p>We\u2019ll run the command on a VirtualBox VM running Ubuntu 22.04.<\/p>\n<pre>sudo dmidecode -s system-product-name<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803841\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-dmidecode.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The dmidecode command correctly identifying a VirtualBox VM\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The platform is correctly identified as VirtualBox.<\/p>\n<p>On a QEMU-KVM VM running Fedora 35, we get this output.<\/p>\n<pre>sudo dmidecode -s system-product-name<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803842\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/fedora-dmidecode.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The dmidecode command correctly identifying a GNOME Boxes VM\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Although this is reported as a standard PC, it is a standard QEMU virtual PC, of type Q35. So the platform is correctly recognized as a virtual machine.<\/p>\n<p>If we run the same command on a physical computer we get some information about the manufacturer.<\/p>\n<pre>sudo dmidecode -s system-product-name<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803843\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/hw-dmidecode.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The dmidecode command returning information about a physical computer\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This computer is a custom-build based on a Micro-Star International Company Limited motherboard, with the product code of MS-7B86.<\/p>\n<h2 id=\"the-lshw-command\"><span class=\"ez-toc-section\" id=\"The_lshw_Command\"><\/span>The lshw Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>lshw<\/code> command lists the details for a wide range of computer hardware. We can choose which class of hardware we want <code>lshw<\/code> to report on.<\/p>\n<p>We\u2019re going to use the <code>-class<\/code> option with the <code>system<\/code> modifier. Using <code>sudo<\/code> with this command ensures we see all of the detail.<\/p>\n<p>We\u2019ll run this command on our Ubuntu VirtualBox VM.<\/p>\n<pre>sudo lshw -class system<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803844\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-lshw.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The lshw command reporting on a VirtualBox VM\" width=\"644\" height=\"270\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<ul>\n<li>The \u201cdescription\u201d field has a generic entry of \u201ccomputer.\u201d<\/li>\n<li>The \u201cproduct\u201d field tells us this is a virtual machine running in VirtualBox.<\/li>\n<li>The \u201cvendor\u201d field contains the name of the German company that created VirtualBox, Innotek GmbH. Innotek was acquired by the Oracle Corporation in 2010 as part of its acquisition of Sun Microsystems, Inc.<\/li>\n<\/ul>\n<p>We had to install <code>lshw<\/code> on Fedora.<\/p>\n<pre>sudo dnf install lshw<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803849\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/fedora-install-lshw.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Installing lshw on Fedora with the dnf command\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Let\u2019s try that command in our Fedora VM running in GNOME Boxes.<\/p>\n<pre>sudo lshw -class system<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803845\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/fedora-lshw.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The lshw command reporting on a GNOME Boxes VM\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<ul>\n<li>Again, the \u201cdescription\u201d field has a generic entry of \u201ccomputer.\u201d<\/li>\n<li>The \u201cproduct\u201d field gives us the same standard QEMU PC information that we saw with the <code>dmidecode<\/code> command.<\/li>\n<li>The \u201cvendor\u201d field contains \u201cQEMU\u201d which quite clearly indicates this is a virtual machine.<\/li>\n<\/ul>\n<p>This is the result of running the same command on our physical computer.<\/p>\n<pre>sudo lshw -class system<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803847\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/hw-lshw-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The lshw command reporting on a physical computer\" width=\"644\" height=\"229\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can see that this is a hardware computer, with a Micro-Star motherboard.<\/p>\n<ul>\n<li>The hardware is identified as a desktop computer.<\/li>\n<li>The \u201cproduct\u201d field gives us the motherboard type, MS-7B86.<\/li>\n<li>The \u201cvendor\u201d field contains the manufacturer\u2019s name.<\/li>\n<\/ul>\n<h2 id=\"the-hostnamectl-command\"><span class=\"ez-toc-section\" id=\"The_hostnamectl_Command\"><\/span>The hostnamectl Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This command has the advantage that you don\u2019t need to have <code>sudo<\/code> privileges to run it. However, it is only available on <code>systemd<\/code>-enabled distributions. The majority of modern distributions use <code>systemd<\/code>.<\/p>\n<p>This is the response from running the command on our Ubuntu VirtualBox VM.<\/p>\n<pre>hostnamectl<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803851\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-hostnamectl.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The output from the hostnamectl command in a VirtualBox VM with the virtualization line highlighted\" width=\"644\" height=\"280\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<ul>\n<li>The \u201cicon-name\u201d field has \u201c-vm\u201d appended to it.<\/li>\n<li>The \u201cChassis\u201d field contains \u201cvm.\u201d<\/li>\n<li>The \u201cVirtualization\u201d field contains \u201coracle.\u201d<\/li>\n<li>The \u201cHardware Vendor\u201d field contains \u201cinnotek GmbH.\u201d<\/li>\n<li>The \u201cHardware Model\u201d field contains \u201cVirtualBox.\u201d<\/li>\n<\/ul>\n<p>The output on our Fedora VM inside GNOME Boxes is very similar.<\/p>\n<pre>hostnamectl<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803852\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/fedora-hostnamectl.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The output from the hostnamectl command in a GNOME Boxes VM with the virtualization line highlighted\" width=\"644\" height=\"315\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<ul>\n<li>The \u201cicon-name\u201d field has \u201c-vm\u201d appended to it.<\/li>\n<li>The \u201cChassis\u201d field contains \u201cvm.\u201d<\/li>\n<li>The \u201cVirtualization\u201d field contains \u201ckvm.\u201d<\/li>\n<li>The \u201cHardware Vendor\u201d field contains \u201cQEMU\u201d<\/li>\n<li>The \u201cHardware Model\u201d field contains \u201cStandard PC (Q35 + ICH9, 2009).\u201d<\/li>\n<\/ul>\n<p>If we use the hostnamectl command on our physical desktop, the output doesn\u2019t contain a \u201cVirtualization\u201d line.<\/p>\n<pre>hostnamectl<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803853\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/hw-hostnamectl.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The output from the hostnamectl command on a physical computer, with no &quot;Virtualization&quot; information\" width=\"644\" height=\"270\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If there\u2019s no \u201cVirtualization\u201d field, you must be running on bare metal.<\/p>\n<h2 id=\"the-systemd-detect-virt-command\"><span class=\"ez-toc-section\" id=\"The_systemd-detect-virt_Command\"><\/span>The systemd-detect-virt Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you want to get as short an answer as possible, <code>systemd-detect-virt<\/code> is probably what you\u2019re looking for. Again this requires a <code>systemd<\/code>-equipped distribution, but it doesn\u2019t require <code>sudo <\/code> privileges. This\u2014and its terse output\u2014make it well suited for use in scripts.<\/p>\n<p>This is the result of running the command on our Ubuntu VirtualBox VM.<\/p>\n<pre>systemd-detect-virt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803854\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-systemd-detect-virt.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Identifying a VirtualBox VM with systemd-detect-virt\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Our copy of Fedora running in GNOME Boxes is reported as using KVM virtualization.<\/p>\n<pre>systemd-detect-virt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803856\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/fedora-systemd-detect-virt.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Identifying a GNOME Boxes VM with systemd-detect-virt\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Running <code>systemd-detect-virt<\/code> on our hardware machine results in \u201cnone\u201d being printed to the terminal.<\/p>\n<pre>systemd-detect-virt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803855\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/hw-systemd-detect-virt.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A physical computer being correctly identified as having no virtualization\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"A_Platform-Sensitive_Script\"><\/span>A Platform-Sensitive Script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To give a script the ability to detect whether it is running in a virtualized environment or on physical hardware, we can use the <code>systemd-detect-virt<\/code> command and use Bash <code>case<\/code> statements to handle the options.<\/p>\n<p>This is the script we\u2019ll use. Copy this text and save it into a file called \u201cplatform.sh.\u201d<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nshopt -s nocasematch&#13;\n&#13;\ncase $(systemd-detect-virt) in&#13;\n&#13;\n  none)&#13;\n    echo \"Physical Hardware\"&#13;\n    ;;&#13;\n&#13;\n  *)&#13;\n    echo \"Virtual Machine\"&#13;\n    ;;&#13;\nesac<\/pre>\n<p>The script uses <code>shopt<\/code> to choose case-insensitive matching. The <code>systemd-detect-virt<\/code> command is used in the <code>case<\/code> statement. The output from this command is compared with each of the <code>case<\/code> clauses in the body of the <code>case<\/code> statement until a match is found.\u00a0Anything that isn\u2019t matched is captured by the \u201c*)\u201d default clause.<\/p>\n<p>The simplest way is to test if the response from <code>systemd-detect-virt<\/code> is \u201cnone.\u201d If it is, the script is running on physical hardware. For all other cases, the script must be running on a virtual machine.<\/p>\n<p>Before we can run the script we must make it executable, using <code>chmod<\/code>.<\/p>\n<pre>chmod +x platform.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803859\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-chmod.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Making the platform script executable with chmod\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>It correctly identifies our Ubuntu VirtualBox VM as a virtual machine.<\/p>\n<pre>.\/platform.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803860\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-platform.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the platform.sh script in a VirtualBox VM\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>It also correctly detects the GNOME Boxes VM running Fedora.<\/p>\n<pre>.\/platform.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803862\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/fedora-platform.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the platform.sh script in a GNOME Boxes VM\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The script also correctly detects when it is running on a physical machine.<\/p>\n<pre>.\/platform.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803861\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/hw-platform.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the platform.sh script on a physical computer\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The different <code>case<\/code> clauses could set variables that were checked elsewhere in the script to perform different types of processing, or they could call specific functions within your script.<\/p>\n<p>If your script needed to detect and accommodate different types of virtual environments, you could add more <code>case<\/code> clauses, looking for the different strings that <code>systemd-detect-virt<\/code> can return. We can see the complete list of possible responses by using the <code>--list<\/code> option. To make it easier to see them all at once, we\u2019ll pipe the output through the <code>column<\/code> command.<\/p>\n<pre>systemd-detect-virt --list | column<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-803863\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/ubuntu-list.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The complete set of responses that systemd-detect-virt can return\" width=\"644\" height=\"210\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Take_the_Red_Pill\"><\/span>Take the Red Pill<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>These techniques let your scripts know when they\u2019re running on naked hardware and when they\u2019re inside a virtual machine.<\/p>\n<p>Like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Red_pill_and_blue_pill#:~:text=The%20terms%20%22red%20pill%22%20and,the%201999%20film%20The%20Matrix.\">Neo in the Matrix<\/a>, they\u2019ll know what\u2019s real and what\u2019s not.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/803839\/how-to-let-linux-scripts-detect-theyre-running-in-virtual-machines\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Let Linux Scripts Detect They\u2019re Running in Virtual Machines&#8221; diy13\/Shutterstock.com Virtual machines try really hard to convince their operating systems that they\u2019re running on physical hardware. So can you tell from the Linux command line if the computer is physical or virtual? Virtual Machines and Hypervisors A traditional computer is a physical object&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":453224,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/shutterstock_1164968740.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-453223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/453223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=453223"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/453223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/453224"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=453223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=453223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=453223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}