Vm versus physical linux server

Does anyone have a script that can be run to tell you if you are on either a VM Linux or a physical Linux server?

no need for a script:

vmware QEmu or KVM:
dmesg | grep -i virtual

if these don't work check the config of a hard drive or cd rom and if it says virtual well there's your answer.

if [ $(ifconfig -a|grep HWaddr|grep "00:50"|wc -l) -eq 0 ]
then
 echo "Not VMware or Physical"
else
 echo "VMware"
fi

Are you suggesting that every Linux VM has a network interface whose MAC address contains "00:50"?

if you need a script to identify if your server is vm or not,
I consider that you have at least one vmware and 1 physical server.
you can ifconfig -a on both and change 00:50 if needed scott

You'll have to indulge me. I don't understand what you're saying.

dmidecode should tell you something.

what I am saying is:
vmware mac addresses usually starts with "00:50", but if any other pattern exists, you ifconfig -a on your vmware server and use your own pattern.

---------- Post updated at 03:29 PM ---------- Previous update was at 03:28 PM ----------

dmidecode is not a global answer. It does not exist on all operating systems.

But VMWare MAC addresses don't usually start with "00:50", and in a real-world scenario in which our MAC addresses are dynamically created based on hostname, which ties in with our network team, who assigns an IP address based on the MAC address, your theory doesn't hold water.

Right, but the OP's question is about Linux. What Linux distribution are you referring to that does not have dmidecode?

Here are one VM

dmidecode | more
-bash: dmidecode: command not found
root@SR2 ~ [0:8]#ifconfig -a|grep HWaddr
eth0      Link encap:Ethernet  HWaddr 00:16:3e:93:95:92
root@SR2 ~ [0:8]#uname -a
Linux SR2 2.6.38-8-generic-pae #42-Ubuntu SMP Mon Apr 11 05:17:09 UTC 2011 i686 i686 i386 GNU/Linux

Here from another type

ifconfig -a|grep HWaddr
venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00

uname -a
Linux SR3 2.6.32-042stab076.8 #1 SMP Tue May 14 20:38:14 MSK 2013 i686 i686 i386 GNU/Linux

dmidecode | more
-bash: dmidecode: command not found

none of them has demidecode , nor hw 00:50

@jotne, so sometimes it is not installed by default (or by choice?), thanks. But can't you just apt-get install dmidecode ?

I was able to install, but get this

dmidecode
# dmidecode 2.11
/dev/mem: Operation not permitted

other server

dmidecode
# dmidecode 2.9
# No SMBIOS nor DMI entry point found, sorry.

My old physical server it runs fine.

Most probably there is no general answer which can be given to the problem. There are many different ways of virtualzation and it is not possible to detect all of them with a single command.

There are fully virtualized environments, like VMWare, KVM (Qemu), etc.. These create a virtual hardware onto which a system is generically installed. This can probably detected with "pciwalk" or similar tools because they use one or the other virtualized "hardware", for which a physical counterpart doesn't exist. Some sort of "Virtual Ethernet Card" or so will not be installed in a physical server.

This will have to be done on a product-by-product base because such "hardware" quirks will most probably not be consistent over different virtualization products.

Then there are para-virtualized environments (for instance: OpenVZ), where virtual systems are not real systems (not even virtualized ones) at all: OpenVZ basically is a hacked kernel which is able to run the processes belonging to different virtual systems separated from each other. From "inside" the system it looks like a standalone system, but on the hardware node (=host system) all the processes are visible. This makes the virtualization overhead much smaller and hence allows more virtual systems on the same hardware. Also, the virtual systems can be a lot smaller (web servers with 256MB memory are quite common), because many of a systems operations happen in fact not in the "local" kernel but the kernel of the hardware node. (In fact this is a kind-of chroot -environment with some additional management possibilities.)

It can usually be detected by some specialized commands, which allow to modify the virtual machine somehow, for OpnVZ this would be "vzctl". But like with the other virtualization techniques this will be on a case-by-case basis.

So coming back to your problem, the best you can probably do is to try to detect one after the other virtualization product and if you come up blank your system is most likely hardware (or a virtualization product you haven't recognized).

I hope this helps.

bakunin

Hi.

Dmidecode is known to work on the following systems:
    *  Linux i386, x86-64, ia64
    * FreeBSD i386, amd64
    * NetBSD i386, amd64
    * OpenBSD i386, amd64
    * BeOS i386
    * Cygwin i386
    * Solaris x86
    * Haiku i586

Excerpt from dmidecode

I tried it on several Linux virtual systems including the major branches, Slackware (Absolute), Debian (Debian, Ubuntu, MiNT), RedHat (CentOS, Fedora); as well as FreeBSD, Solaris. The only one I found that did not have demidecode in the repository was Solaris 11.

One seems to need to be root in order to run it. If not, either it cannot be found (often in /usr/sbin/ , not usually part of a non-admin's PATH) or one receives a message like

vm-centos ~ % /usr/sbin/dmidecode 
# dmidecode 2.11
/dev/mem: Permission denied

Best wishes ... cheers, drl

1 Like

Thanx it all helped alot guys