How to find IP address of Google Cloud VM instance on command line
Problem:
You have a VM instance (my-instance
in our example) for which you want to get the external or internal IP using the gcloud
command line tool.
Solution
If you just want to see the external IP of the instance (remember to replace my-instance
by your instance name!), use
gcloud compute instances list --filter="name=my-instance" --format "[box]"
This will format the output nicely and show you more information about your instance. Example output:
┌─────────────┬────────────────┬─────────────────────────────┬─────────────┬─────────────┬───────────────┬─────────┐
│ NAME │ ZONE │ MACHINE_TYPE │ PREEMPTIBLE │ INTERNAL_IP │ EXTERNAL_IP │ STATUS │
├─────────────┼────────────────┼─────────────────────────────┼─────────────┼─────────────┼───────────────┼─────────┤
│ my-instance │ europe-west3-c │ custom (16 vCPU, 32.00 GiB) │ │ 10.156.0.1 │ 35.207.77.101 │ RUNNING │
└─────────────┴────────────────┴─────────────────────────────┴─────────────┴─────────────┴───────────────┴─────────┘
In this example, the external IP address is 35.207.77.101
.
In case you want to see only the IP address, use this command instead:
gcloud compute instances list --filter="name=my-instance" --format "get(networkInterfaces[0].accessConfigs[0].natIP)"
Example output:
35.207.77.101
In order to see only the internal IP address (accessible only from Google Cloud), use
gcloud compute instances list --filter="name=my-instance" --format "get(networkInterfaces[0].networkIP)"
In the linux shell, the result of this command can easily be used as input to other commands. For example, to ping my-instance
, use
ping $(gcloud compute instances list --filter="name=katc-main" --format "get(networkInterfaces[0].accessConfigs[0].natIP)")
Also see our related post How to find zone of Google Cloud VM instance on command line
In order to see what other information about instances you can see in a similar fashion, use
gcloud compute instances list --filter="name=my-instance" --format "text"