How to know if a machine is an Google Compute Engine instance

前端 未结 4 1482
感动是毒
感动是毒 2021-02-13 09:59

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)?

4条回答
  •  野性不改
    2021-02-13 10:29

    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
    

提交回复
热议问题