"having dynamic IP", "SSH using some other system across the internet", "the command which will display the present PUBLIC IP". You see the chicken/egg problem here? How would you be able to run commands on a remote server without knowing its address? You might be more interested in services like no-ip.com / DynDNS.org.
–
gertvdijkJan 9 '13 at 13:11

one cannot SSH without knowing the public IP my friend... dynDNS costs a lot and no-ip tough works but the situation don't allow that... anyway the question has been already answered.. thanks for your suggestion
–
Z9iTJan 10 '13 at 8:46

ty - right after I posted, I realized that I didn't google for an answer first: looks like this will work curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' Other possibilities are listed here: go2linux.org/what-is-my-public-ip-address-with-linux
–
kfmfe04Jan 16 '12 at 12:01

For finding the external ip, you can either use external web-based services, or use system based methods. The easier one is to use the external service, also the ifconfig based solutions will work in your system only if you're not behind a NAT. the two methods has been discussed below in detail.

Finding external IP using external services

The easiest way is to use an external service via a commandline browser or download tool. Since wget is available by default in Ubuntu, we can use that.
To find your ip, use-

You could also use lynx(browser) or curl in place of wget with minor variations to the above command, to find your external ip.

Using curl to find the ip:

curl ipecho.net/plain

For a better formatted output use:

curl ipecho.net/plain ; echo

A faster(arguably the fastest) method using dig with OpenDNS as resolver:

The other answers here all go over HTTP to a remote server. Some of
them require parsing of the output, or rely on the User-Agent header
to make the server respond in plain text. They also change quite
frequently (go down, change their name, put up ads, might change
output format etc.).

The DNS response protocol is standardised (the format will stay compatible).

Historically DNS services (OpenDNS, Google Public DNS, ..) tend to survive much longer and are more stable, scalable and generally looked
after than whatever new hip whatismyip.com HTTP service is hot today.

(for those geeks that care about micro-optimisation), this method should be inherently faster (be it only by a few micro seconds).

@Z9iT, Sure.. It should work in any linux distribution provided that you have wget installed. As said if you have either curl or lynx already available please use that instead. You would need root permission to install so use sudo apt-get install wget
–
saji89Jun 1 '12 at 12:19

5

The commands with ifconfig do only work, if you are not behind a NAT.
–
lukassteinerJan 23 '13 at 15:52

This is really fast. I did one warmup execution, then 10 executions each of this and curl icanhazip.com. Average for the curl version: 174ms. Average for the DNS-only version: 9ms. ~19x faster. See also: unix.stackexchange.com/a/81699/8383
–
Adam MonsenMar 10 at 21:06

@AdamMonsen Thank you for the link. The point of using DNS (as the answer that you've linked says) is that the response is standard (and unlikely to change) and the service (OpenDNS) might stick around longer than most of its http alternatives. The time it takes to make the request might be shadowed by the command start up time.
–
J.F. SebastianMar 11 at 0:40

Yep. I wouldn't be surprised if curl itself is slower than dig. Even if they were rewritten to be as similar as possible, curl would still be slower; it uses HTTP (including DNS) and dig only uses DNS.
–
Adam MonsenMar 11 at 4:16

I prefer curl icanhazip.com sometimes wget is the only one available, but sometimes no wget is available as well and curl is your only option (like OS/X). Either way curl icanhazip.com is almost as easy as curl ifconfig.me but much funnier ;-)
–
TryTryAgainAug 25 '12 at 20:38

For those of us with login access to our routers, using a script to ask the router what its' WAN IP address is is the most efficient way to determine the external IP address. For instance the following python script prints out the external IP for my Medialink MWN-WAPR300N router:

Note that this is not very secure (as is the case with plaintext credentials & logging in to most routers), and is certainly not portable (needs to be changed for each router). It is however very fast and a perfectly reasonable solution on a physically secure home network.

To customize the script for another router, I recommend using the tamperdata addon in firefox to determine what HTTP requests to make.