You are locating external IP addresses within GCP Server.

Are you working on a project where you need to retrieve external (Internet/Public) IP of respective VM instance for application?

Good news – you can quickly get them.

I am sure you would have tried running ifconfig command. And, you could notice the results contain only internal IP.

GCP and AWS, both have a friendly web interface where you can see the public IP, but if you require to get them on a server directly, then the following commands will help you.

Getting External IP on GCP VM

There are two possible ways I am aware of. The first one is using a gcloud command.

gcloud compute addresses list

The above command will show you all your project IPs. Useful for troubleshooting or have a quick review while you are logged in on VM.

Ex:

[email protected]:~# gcloud compute addresses list
NAME                  ADDRESS/RANGE   TYPE      PURPOSE       NETWORK  REGION    SUBNET   STATUS
instance-1           xx.xx.xx.xx                                   us-west1           IN_USE
geekflare-nexus  xx.xx.xx.xx      INTERNAL  GCE_ENDPOINT           us-west1  default  IN_USE
geekflare               xx.xx.xx.xx                                     us-west1           IN_USE
geekflare-tools          xx.xx.xx.xx                                   us-west1           IN_USE
[email protected]:~#

and, the second one is by using curl command to metadata.

curl -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip

Note: if you have multiple network interface then you got to change 0 after network-interfaces to 1 or 2 accordingly.

Metadata is powerful; you can retrieve many metrics.

[email protected]:~# curl -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/
attributes/
cpu-platform
description
disks/
guest-attributes/
hostname
id
image
licenses/
machine-type
maintenance-event
name
network-interfaces/
preempted
remaining-cpu-time
scheduling/
service-accounts/
tags
virtual-clock/
zone
[email protected]:~#

You see, what I mean?

This can be handy for reporting and automation.

If you also need to get internal IP then use the below command.

curl -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip

Are you learning GCP administration? Check out this fantastic online course.