Is there a way to know from a command line shell if I am currently on a Google Compute Engine machine or somewhere else (development machine)?
You can also do a DNS lookup for the Metadata server, instead of actually calling it.
For example, doing dig +short metadata.google.internal
inside a Google Compute instance would output something like this:
[root@vm-1]# dig +short metadata.google.internal
169.254.169.254
If, however, you do the same command (dig +short metadata.google.internal
) inside a standard server, outside of Google Cloud, you could get an empty response.
So to check, all you need to do (in bash
for instance) is:
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