Return the private IP of an EC2 instance from within the EC2 instance

6,155
$ curl http://169.254.169.254/latest/meta-data/local-ipv4

The private IPv4 address of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which the device number is 0).

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

Alternately:

$ ec2metadata --local-ipv4
Share:
6,155

Related videos on Youtube

RabT
Author by

RabT

Updated on September 18, 2022

Comments

  • RabT
    RabT almost 2 years

    What specific syntax can be used to return the private IP address of an Amazon Linux EC2 instance from the command line within that instance?

    So far, I have come up with the following:

    # Get private IP from inside the instance by filtering inside the following:
    instanceid=$(curl http://169.254.169.254/latest/meta-data/instance-id)
    echo $instanceid
    aws ec2 describe-instances --instance-id $instanceid --region us-west-2
    

    However, as you can see if you type these commands inside an EC2 instance yourself, the above 3 lines result in a massive output, which would be a mess to filter through manually.

    Can someone suggest one or two lines of code to return simply the private ip of the instance and NO other information?

  • Rui F Ribeiro
    Rui F Ribeiro about 5 years
    It is not guaranteed that it will be this particular address, they are negotiated. Also, linux professionals disable avahi in VM/enterprise settings.
  • Michael - sqlbot
    Michael - sqlbot about 5 years
    @RuiFRibeiro I'm not sure what you are saying is negotiated. It's also unclear why you mention avahi, but I assume you are referring to 169.254.169.254. This is the fixed IP address of the EC2 Metadata service, a service provided by the hypervisor and accessible only from inside the instance. See also What's Special about the 169.254.169.254 Address for AWS?