Is there any way to find out if the OS I'm running (actually installing) is running in a VMWare machine. I need to disable ntp
settings if the automated install is done on a virtual machine but keep them enabled if installing on bare metal.
-
5I don't believe this is a duplicate. The linked question asks how to determine which virtualization technology is used, which is much more difficult and less reliable (per the answers given!) and arguably less useful than merely determining whether the machine is virtual. – Kyle Strand Oct 11 '17 at 17:16
12 Answers
Linux adds the hypervisor
flag to /proc/cpuinfo
if the kernel detects running on some sort of a hypervisor.

- 1,077
-
4This is the most reliable way and should be the accepted answer IMHO. One can therefore use this command:
grep -q ^flags.*\ hypervisor\ /proc/cpuinfo && echo "This machine is a VM"
– dr_ Jan 07 '16 at 09:33 -
@dr01 - This only worked for me in Ubuntu 14.04LTS if I removed the backslash after hypervisor. What is it supposed to do? – Martin Bramwell Jan 23 '16 at 13:47
-
2You're right, there should be two spaces after that backslash. This also works:
grep -q ^flags.*\ hypervisor /proc/cpuinfo && echo "This machine is a VM"
This reads/proc/cpuinfo
and checks if theflags:
line contains the stringhypervisor
, as suggested by Jan. – dr_ Jan 23 '16 at 14:49 -
This won't work on RH 5.x (though the the flag is present in RH6.x and above) – luis.espinal Oct 19 '16 at 15:35
-
-
I like this one, as it can be used in C programs (no external dependency). – pevik Apr 27 '20 at 05:34
-
On Linux you can use the command virt-what
[root@myhost]# virt-what
vmware
Example Output for a linux on Windows HyperV
[root@linuxvm11~] # virt-what
hyperv
Example Output for centos8 on vmware
CentOS:root@box003:~/CM/bin/tools# virt-what
vmware

- 339
-
1This did not work for me in an unprivileged docker container. It hits a
permission denied
error reading/proc/1/environ
, as root! – Martin Bramwell Jan 23 '16 at 14:22
This worked better for me as it gives me specific information about the manufacturer and the product name.
dmidecode -t system|grep 'Manufacturer\|Product'
Output on Dell server:
Manufacturer: Dell Inc.
Product Name: PowerEdge C5220
Output on Virtualbox VM:
Manufacturer: innotek GmbH
Product Name: VirtualBox
Output on KVM/QEMU:
Manufacturer: QEMU
Product Name: Standard PC (i440FX + PIIX, 1996)
This is great for scripts that can parse these out for better identification of servers... but if you use Chef in your infrastructure, you can check the node attribute Virtualization -> system
in the chef server .

- 346
-
2on openstack: ''' Manufacturer: OpenStack Foundation Product Name: OpenStack Nova ''' – gaoithe Dec 14 '17 at 16:35
-
2on vmware ''' Manufacturer: VMware, Inc. Product Name: VMware Virtual Platform ''' – gaoithe Dec 14 '17 at 16:38
Run:
$ dmesg |grep -i hypervisor
Hypervisor detected: KVM

- 93,103
- 40
- 240
- 233

- 371
Using dmidecode
or lshw
and grep
ing seems to be to be the best way to find out.
-
2grep for what? I ran
sudo dmidecode | grep hyper
andsudo dmidecode | grep virt
but none of them works.lshw
's result is exactly the same as/proc/cpuinfo
– phuclv Dec 30 '16 at 06:02 -
@LưuVĩnhPhúc See the linked duplicate question, which explains how to use these functions to find specific virtualization info. – Kyle Strand Oct 11 '17 at 17:43
If all you need is a way to tell whether the OS/host is a virtualized host or not, just you have a perl module Sys::Detect::Virtualization and the script with it virtdetect
. It does all the possible heuristics/guess detections and reports the detected OS environment. Give it a try.
http://search.cpan.org/dist/Sys-Detect-Virtualization/script/virtdetect

- 8,315
The best idea would probably to look at the hardware. At least with VirtualBox you can easily determine that you are on a virtual machine, due to the names of some of the hardware devices (for example /sys/block/sda/device/model
will say "VBOX HARDDISK").
Since all your machines are VMware, just pick one of those things and check that.
-
2
-
The Harddisk was just an example, check for other devices, maybe the harddisk controller or the graphics card (I don't have vmware here so I cannot check) – tante Nov 02 '10 at 08:25
I have done it:
hypervisor=`dmesg --notime | grep -i hypervisor | cut -d ':' -f2 | tr -d " \t\n\r"`
echo "Hypervisor is $hypervisor"
It helps on scripts

- 333,661
All of these answers work in some cases but not others.
For example, you can depend on dmesg
while boot-up log details are still in the ring buffer, but it will likely fail on a machine that has been running for any length of time. Worse, a message might be logged by the bare metal OS concerning a running hypervisor, in which case a naive test like dmesg | grep -i vmware
will return a false positive.
Testing under Docker is quite different. Docker has no /proc/cpuinfo
of its own; instead it passes on the host machine's info. Meanwhile, dmidecode
fails trying to read a directory /dev/mem
not seen by Docker.
virt-what
has detection for Docker containers, but needs to be patched to cope with a recent change in container privileges. It crashes trying to access /proc/1/environ
before it reaches the tests for Docker.
It is important to pay attention to the virt-what
caveat emptor :
Most of the time, using this program is the wrong thing to do. Instead you should detect the specific features you actually want to use.
In my case, publishing a tutorial that installs a ton of crap users may not want after all, I refuse to let it run on bare metal, with this test :
[[ 0 < $(grep -c docker /proc/1/cgroup) ]] || [[ "X$(sudo virt-what)X" != "XX" ]] && export VIRTUALIZED=true;
Note : I realize the OP asks specifically about VMWare in the body of the question, but the title of the question will attract many readers (like me) looking for the more general case.

- 151
-
1Docker is not a virtualization in the classical sense. It is a container, the processes still run inside the same kernel. Thus your answer, while it might be correct, is outside of the scope of the question. – Jan Henke Jan 24 '16 at 18:56
-
In that case, I suggest you jump in quickly and correct Wikipedia's misunderstanding : "Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux." – Martin Bramwell Jan 24 '16 at 20:27
-
1There are a lot of half-truth and easy to misunderstand things written on wikipedia. Do not use it to prove a point and take everything written there with a grain of salt. Also not that "virtualization" is broader term then what we are discussing here. That is why I wrote "in the classical sense" above. Just accept that a docker container is pretty different from a virtual machine (which is the whole point of docker in the first place by the way). – Jan Henke Jan 25 '16 at 07:45
-
I don't need Wikipedia to prove my point. I made a YouTube video Meteor / CircleCI Tutorial -- #0 Create a Docker Machine that explains how to start my Docker image xubuntu-x2go-phusion of Xubuntu and X2Go with full GUI. If I install Java in that machine it does not also get installed in the base OS. Please, watch the video and then clarify for us my "wrong assumptions" that it's a virtual machine. – Martin Bramwell Jan 25 '16 at 16:21
-
2" If I install Java in that machine it does not also get installed in the base OS" that has nothing to do with this discussion. Surely it is not installed in the base OS (as the container has a separate file system). But that does not make a Docker container the same as a virtual machine. I am trying to clarify your wrong assumptions right now, but you do not seem overly interested in listing and understand what I am telling you. – Jan Henke Jan 26 '16 at 11:10
-
2Also the title "create a Docker Machine" is misleading, you create a Docker container, not a Docker machine. The docker image has a separate file system, but processes you start run on the host kernel. You do not run a separate kernel, like a traditional virtual machine. That is why the hypervisor flag is not set, you are not running inside a hypervisor after all. Just do not call other people's answer "inadequate" if you confuse things and make false assumptions. – Jan Henke Jan 26 '16 at 11:16
-
The simple fact is, the OP wants
ntp
adjustments on bare metal only. For that purpose, his code, running under Docker, is not on bare metal, sontp
should not (in fact, cannot) be enabled. Pointless hair-splitting about arbitrary "classical" definitions notwithstanding, your test is adequate for VMware but inadequate for anyone concerned about the more general case (which brought me to this thread in the first place). – Martin Bramwell Jan 26 '16 at 13:42 -
No, it is not more general, but more two specific cases. One is running inside a container, the other is about running inside a virtual machine. You started the discussion by discrediting a perfectly valid answer as "inadequate", because it does not fit your point of view. You started introducing Docker into a topic that was not not concerned about that. Also my answer is still adequate and fully correct. Docker is simply no hypervisor, but most virtual machines are. – Jan Henke Jan 26 '16 at 13:56
-
Your assumption : If it is not 100% virtual; it's bare metal. My assumption : If it is not 100% bare metal; it's virtual. In a world where millions of "classical" virtual machines are being dumped in favour of containers (and probably further innovations and variations on virtualization), building your assumption into software is not inadequate -- it's flat out -- wrong! – Martin Bramwell Jan 26 '16 at 14:52
-
2Your assumption is wrong. I never said it is bare metal. All I said is that containers like Docker are different from virtual machines. And my answer was fully correct, as it only applies to virtual machines. You extended the scope of the question yourself and then called my answer "inadequate", because it applied to the scope of the original question and not your extension. I just pointed out to you, that you have to treat containers like Docker differently from virtual machines, but calling my answer "inadequate" is wrong to begin with, since you base it on your subjective assumptions. – Jan Henke Jan 26 '16 at 15:11
Requires APIC, returns 0 if virtual machine, 1 if physical computer:
grep -q VBOX /sys/firmware/acpi/tables/APIC

- 4,239

- 21
- 1
well, the most intuitive way I always do is:
$ dmesg | grep -i vmware

- 948
- 1
- 10
- 12
-
nice - this works on virtual box:
dmesg | grep -i vbox
-ACPI: RSDP 000e0000 00024 (v02 VBOX )
– Danny Staple May 09 '13 at 14:40