Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Cloud Computing Last updated: August 3, 2022
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

You are locating external IP addresses within GCP Server.

Are you working on a project where you need to retrieve the external (Internet/Public) IP of the respective VM instance for the 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:

root@geekflare:~# 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
root@geekflare:~#

and, the second one is by using the 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.

root@geekflare:~# 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
root@geekflare:~#

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.

  • Chandan Kumar
    Author
    As the founder of Geekflare, I’ve helped millions to excel in the digital realm. Passionate about technology, I’m on a mission to explore the world and amplify growth for professionals and businesses alike.
Thanks to our Sponsors
More great readings on Cloud Computing
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Monday.com is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder