2

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?

12 Answers 12

2

You could use this script

# !/bin/bash 
#
echo 'Your external IP is: '
curl -4 icanhazip.com

But that is relying on a third party albeit a reliable one. I don't know if you can get your external IP without asking someone/somesite i.e. some third party for it, but what do I know.

you can also just run:

curl -4 icanhazip.com

This is doing the same thing as a command the -4 is to get the output in Ipv4

2

simplest of all would be to do : curl ifconfig.me

1
  • Yes, it works fine as I have mentioned in my question, But I do not want to depend on this third party service. What if that server stop this service in future? Oct 28, 2017 at 12:08
1

You can use this command to get public ip and private ip(second line is private ip; third line is public ip.)

ip addr | awk '/inet / {sub(/\/.*/, "", $2); print $2}'
1
  • It's the cleanest solution, it's sad that most of articles / answers using external resources to get IP instead of this simple solution
    – bimlas
    Jun 30, 2022 at 11:56
1

I would suggest you to use the command external-ip (sudo apt-get install miniupnpc) as it (I'm almost sure) uses upnp protocol to ask the router instead of asking an external website so it should be faster, but of course the router has to have upnp enabled.

1

You can simply do this :

curl https://ipinfo.io/ip
1

The super-easy way is using the glances tool. you can install it on Ubuntu using:

$ sudo apt install glances

then using it with:

$ glances

and at the top of the terminal, it highlights your public IP address, and so many other information about your system (like what htop does) and network status.

0

It might not work on amazon because you might be using NAT or something for the server to access the rest of the world (and for you to ssh into it also). If you are unable to ssh into the ip that is listed in ifconfig then you are either in a different network or dont have ssh enabled.

0

A cleaner output

ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://'
2
  • Returns Amazon internal IP address Oct 28, 2017 at 12:06
  • That returns whatever is IP address assigned to your eth0 device. So if your server is hosted on Amazon, and was assigned an internal IP address, and since Amazon instances are using NAT, that's what you'd see. If you want a sure reliable way of getting your external, public IP address, then use one of the external service like ifconfig.me.
    – R J
    Oct 28, 2017 at 22:35
0

This is the best I can do (only relies on my ISP):

ISP=`traceroute -M 2 -m 2 -n -q 1 8.8.8.8 | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`
extIP=`ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`
echo $extIP

Or, the functionally same thing on one line:

ISP=`traceroute -M 2 -m 2 -n -q 1 8.8.8.8 | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'` | ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 

to save it to a temporary & hidden file add > .extIP to the end of the last line, then cat .extIP to see it.

If your ISP's address never changes (honestly i'm not sure if it would or not), then you could fetch it once, and then replace $ISP in line two with it

This has been tested on a mac with wonderful success. the only adjustment on linux that I've found so far is the traceroute "-M" flag might need to be "-f" instead and it relies heavily on the ping's "-R" flag, which tells it to send back the "Record Route" information, which isn't always supported by the host. But it's worth a try!

the only other way to do this without relying on any external servers is to get it from curl'ing your modem's status page... I've done this successfully with our frontier DSL modem, but it's dirty, slow, unreliable, and requires hard-coding your modem's password. Here's the "process" for that:

curl http://[user]:[password]@[modem's LAN address]/[status.html] | grep 'WanIPAddress =' | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

That fetches the raw html, searches for any lines containing "WanIpAddress =" (change that so it's appropriate for your modem's results), and then narrows down those results to an IPv4 style address.

Hope that helps!

0

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

0

For a formatted output use :-

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com

it'll give you formatted output like this

"30.60.10.11"

also FYI,

dig is more faster than curl and wget

0

The following works as long as you have ifconfig and curl.

curl ifconfig.me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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