1

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?

3
$ 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
  • It is not guaranteed that it will be this particular address, they are negotiated. Also, linux professionals disable avahi in VM/enterprise settings. – Rui F Ribeiro Jun 8 at 7:26
  • 1
    @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? – Michael - sqlbot Jun 8 at 19:50

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.