How to find out if a machine is an instance of Google Compute Engine

Is there a way to find out from the command line if I'm on a Google Compute Engine computer or somewhere else (development machine) now?

+6
source share
3 answers

In metadata documents :

You can easily determine if your applications or scripts are running on a Compute Engine instance using a metadata server. When you submit a request to the server, any response from the metadata server will contain the Metadata-Flavor: Google header. You can find this title for reliable detection if you are running the Compute Engine.

For example, the following curl query returns the Metadata-Flavor: Google header, indicating that the query is being executed from an instance of the Compute Engine.

 me@my-inst :~$ curl metadata.google.internal -i HTTP/1.1 200 OK Metadata-Flavor: Google Content-Type: application/text Date: Thu, 10 Apr 2014 19:24:27 GMT Server: Metadata Server for VM Content-Length: 22 X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN 0.1/ computeMetadata/ 
+4
source

You can also use the dmidecode utility to test virtual hardware if you do not want to make a network call:

 my@myinst :~$ sudo dmidecode -s bios-vendor | grep Google Google 
+4
source

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 
+2
source

Source: https://habr.com/ru/post/989336/


All Articles