You can also do a DNS lookup for the metadata server, rather than name it.
For example, when doing dig +short metadata.google.internal inside an instance of Google Compute, the following will be displayed:
[ root@vm-1 ]# dig +short metadata.google.internal 169.254.169.254
If, however, you run the same command ( dig +short metadata.google.internal ) inside a standard server, outside of Google Cloud, you may get an empty answer.
So, to check, all you have to do (e.g. in bash ):
GMETADATA_ADDR=`dig +short metadata.google.internal` if [[ "${GMETADATA_ADDR}" == "" ]]; then echo "I am NOT in a Google VM!" else echo "I AM INSIDE a Google VM! Whoohoo!" fi
source share