Say I opened an ssh session to an IP address. Can I tell, from within the ssh session, if that ssh session is running on a VirtualBox guest VM, or on a non-VM machine?
Notes:
- OS Host/Guest: CentOS 7
- Virtualbox: 6.0
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityYes, you can use dmidecode
to accomplish this:
sudo yum install dmidecode
sudo dmidecode -s system-manufacturer
For example, in my VMware Workstation CentOS VM it returned this:
VMware, Inc.
In your VirtualBox VM it would return:
innotek GmbH
Source: https://www.ostechnix.com/check-linux-system-physical-virtual-machine/
The below is based on an answer I received on another forum.
One quick way would be to query the NIC maker. The VM has to have a NIC, since we're connecting via SSH.
Here's what I get from a VM that has 4 NICs:
$ ifconfig | grep ether
ether 08:00:27:ae:2c:b5 txqueuelen 1000 (Ethernet)
ether 08:00:27:1d:8b:9f txqueuelen 1000 (Ethernet)
ether 08:00:27:15:c6:f7 txqueuelen 1000 (Ethernet)
ether 08:00:27:64:bd:3b txqueuelen 1000 (Ethernet)
That 08:00:27
part shows that the NIC manufacturer is VirtualBox (i.e., we're in a VM).
Other ways could be getting information of our hardware from the OS, for example:
$ lshw | grep -i virtualbox
WARNING: you should run this program as super-user.
WARNING: output may be incomplete or inaccurate, you should run this program as super-user.
product: VirtualBox Graphics Adapter
product: VirtualBox Guest Service
We can have further evidence in case the Guest Additions
are installed:
$ VBoxControl -version
6.0.0r127566
But I think the first trick (with the MAC addresses) should be the one to use.
Edit 1:
Run these to get the ssh
environment:
if [[ $(ifconfig | grep '08:00:27') ]] ; then echo "We're in Virtualbox VM" ; fi
lshw | grep -i virtualbox
VBoxControl -version
if [[ "innotek GmbH" == "$( sudo dmidecode -s system-manufacturer)" ]] ; then echo "We're in Virtualbox VM" ; fi
If the VM is not configured to lie to you, then the techniques presented in other answers here are fine.
But if the VM is configured to lie to you (e.g. MAC addresses can be arbitrary) then you would need to work harder. You may be able to find inconsistencies in /proc/* (e.g. different number of cores available than would be expected for the CPU model).