11
votes

This is a question in two parts:

  • Do GCE instances have a stable public DNS name? The default DNS name for instance with public IP a.b.c.d seems to be d.c.b.a.bc.googleusercontent.com
  • If yes, what's the best way to obtain this information? Here's the hack I've been using thus far:

    EXTERNAL_IP=$(curl -s http://bot.whatismyipaddress.com/)
    EXTERNAL_DNS=$(dig +short -x ${EXTERNAL_IP})
    
2
The d.c.b.a.bc.googleusercontent.com DNS names are effectively IP addresses. Can you share why you're looking for the DNS name instead of IP address?Brian Dorsey
@BrianDorsey some applications (e.g. Puppet) need DNS names and don't work well with IP addresses.Diwaker
For ssh access during development it would be easier to remember and type names (matching the project instance name) rather than IP addresses, without needing to set up separate dyndns.poolie

2 Answers

1
votes

reverse lookup is okay to do, for IP address you would probably prefer using gcutil https://developers.google.com/compute/docs/gcutil/tips

EXTERNAL_IP=$(gcutil getinstance --format=csv --zone=[your_zone] [your_instance] | grep external-ip | cut -d "," -f 2)

0
votes

GCE instances don't currently have a public DNS name for their external IP address. But there is now a gcloud compute config-ssh (docs) command that's a pretty good substitute.

This will insert Host blocks into your ~/.ssh/config file that contain the IP address and configuration for the host key.

Although this only helps with SSH (and SSH-based applications like Mosh and git+ssh), it does have a few advantages over DNS:

  • There is no caching/propagation delay as you might have with DNS

  • It pre-populates the right host key, and the host key is checked the right way even if the ephemeral IP address changes.

Example:

$ gcloud compute config-ssh
...
$ ssh myhost.us-west1-b.surly-koala-232