I have a project wherein I need to update configuration files each time an EC2 instance is booted with the Public DNS address of the current instance. I'll be using Perl or Sed for this, so that's not really the question, but the real question is: is there a way that I can determine the instance's public DNS address? Is there an EC2 api that I can access from the instance to determine it?

4 Answers 4


There is. From inside the instance, you can run:


To get the public DNS hostname, you can change that to:


You can get the private IP for the instance, too:


As a side note, you can double-check it against a non-AWS site on the internet, like http://ip4.me


pubip=$( curl http://ip4.me 2>/dev/null | sed -e 's#<[^>]*>##g' | grep '^[0-9]' )

echo $pubip

That will work, generally, to check the "public IP" of any NATed system, or to find your public proxy IP, etc.

And here's a good link to read up on the types of information you can get from Amazon's API: http://www.ducea.com/2009/06/01/howto-update-dns-hostnames-automatically-for-your-amazon-ec2-instances/

  • 1
    PUBLIC_HOSTNAME="$(curl 2>/dev/null)" :) You, sir, are metal. Nov 11, 2011 at 3:26
  • glad to help :) Nov 11, 2011 at 3:28

I define this function inside my .bashrc to retrieve the public ip and dns:

export PUBLIC_DNS=`curl 2>/dev/null`
export PUBLIC_IP=`curl 2>/dev/null`

function get-pub() {
  if [ $# -ne 1 ]; then
    echo "Invalid number of arguments"
    return 1
    case $1 in
        echo $PUBLIC_DNS
        echo $PUBLIC_IP
        echo $"Usage: get-pub {dns|ip}"
        return 2
  return 0

You can install and configure AWS CLI (aws configure) and use the following command:

aws ec2 describe-instances --instance-id i-0xyz --query 'Reservations[].Instances[].PublicDnsName' --output text

This will get the dns name by tag.

aws ec2 describe-instances --region us-east-2 --max-items 1 --filters "Name=tag:Name,Values=MyNameTag" --query 'Reservations[].Instances[].PublicDnsName' --output text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.