Skip to content
Advertisement

Linux command for public ip address

I want command to get Linux machine(amazon) external/public IP Address.

I tried hostname -I and other commands from blogs and stackoverflow like

ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*.){3}[0-9]*).*/2/p'

and many more. But they all are giving me internal IP Address.

Then I found some sites which provides API for this.

Example : curl http://ipecho.net/plain; echo

But I don’t want to rely on third party website service. So, is there any command line tool available to get external IP Address?

Advertisement

Answer

As others suggested, we have to rely on third party service which I don’t feel safe using it. So, I have found Amazon API on this answer :

$ curl http://169.254.169.254/latest/meta-data/public-ipv4

54.232.200.77

For more details, https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-retrieval

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement