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:
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?

1 Answer 1

$ curl

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).



$ 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. Jun 8, 2019 at 7:26
  • 2
    @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 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 Address for AWS? Jun 8, 2019 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.