Sei sulla pagina 1di 2

Command for determining my public IP?

Question:
If I check with google, I can see my public IP. Is there something on the Ubuntu command-line
which will yield me the same answer?
"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 /
1 DynDNS.org. gertvdijk Jan 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 Z9iT Jan 10 '13 at 8:46

Here is how you will get it.


1. If you are behind a router, then your computer will not know about the public IP address
as the router does a network address translation.
If you are not behind a router, you can find it out using ifconfig. Else you could
request this website using curl and extract the information you need from it, e.g. using
a perl script.
curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

should do the trick.


1
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 kfmfe04 Jan 16 '12 at 12:01
sure - you can add it to your answer kfmfe04 Jan 16 '12 at 13:18

To clarify: That was a hack, and a very ugly one at that, so I did an edit to make it simpler and something that people
can remember. jrg Jan 16 '12 at 16:17

19

This doesn't work anymore. whatsmyip.org is displaying the IP in an image (<img src='ipimg.php'/>). fiatjaf O
27 '13 at 6:17

2
Exacly as Giovanni P stated. The OP should change the accepted anwser.

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 wgetis available by default in Ubuntu, we can use that.
To find your ip, usewget -qO- http://ipecho.net/plain ; echo
Courtesy:
http://hawknotes.blogspot.in/2010/06/finding-your-external-ip-address.html
http://ipecho.net/plain
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

Finding external IP without relying


on external services
If you know your network interface name
Type the following in your terminal:

ifconfig <interface_name> | grep "inet addr" | awk -F: '{print $2}' | awk
'{print $1}
n the above, replace <interface_name> with the name of your actual interface,
e.g: eth0, eth1, pp0, etc...
Example Usage:
saji@geek-lap:~$ ifconfig ppp0 | grep "inet addr" | awk -F: '{print $2}' | awk
'{print $1}'
111.222.333.444

Potrebbero piacerti anche