how to know if you’re running Linux on a virtual machine using the command line


Linux tips

Sometimes on your GNU/Linux machine you need to know if you’re running Linux on a virtual machine or not using the command line.

The virtual machine is hosted from a real machine, but the running virtualized machine is different, at least looking at the disk size, and other times respect to how much RAM’s been used. Any virtualized environment you can use: Xen Server, VM Ware, Hyper-V, it’s installed on a very powerful physical machine. With lots of RAM, maybe with an external Storage Server. Each virtual machine you can create is going to use just a part of all that RAM.

Under GNU/Linux you can know if the running machine is real or a virtualized one.

How?

Just use this BASH script:

#!/bin/bash

checkVar=$(cat /proc/cpuinfo | grep -i hypervisor)

if [ -z "$checkVar" ];
then
 if [ -d "/proc/vz/" ];
 then
   checkVar=$(ls -al /proc/vz/)
 else
   checkVar=$(dmesg | grep -i virtual | grep -i -e vbox -e virtualbox -e "virtual box" -e vmware -e kvm -e bochs -e qemu -e xen -e "virtual hd" -e "virtual cd")
 fi
fi

if [ -z "$checkVar" ];
then
 echo "Real Machine"
else
 echo "Virtualized Machine"
fi
Annunci

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...