I can find my IP address using ifconfig or hostname -i command.
But how do I find my Public IP?
(I have a static public IP but I want to find it out using unix command)
I can find my IP address using ifconfig or hostname -i command.
But how do I find my Public IP?
(I have a static public IP but I want to find it out using unix command)
curl ifconfig.me
curl ifconfig.me/ip
(for just the ip)curl ifconfig.me/all
(for more info, takes time)For more commands visit: http://ifconfig.me/#cli_wrap
You can request
myip.opendns.com
. from OpenDNS.dig @208.67.222.220 myip.opendns.com
dig @ns1.google.com -t txt o-o.myaddr.l.google.com +short
dig -4 @ns1-1.akamaitech.net -t a whoami.akamai.net +short
dig -4 @resolver1.opendns.com -t a myip.opendns.com +short
Note that the above only works for IPv4 currently (none of these resolvers even seem to have IPv6 currently, but if you omit
-4
and the explicit-t a
, then you risk it breaking down in the future (with the exception of Google'stxt
, which might actually work for IPv6 one day, if properly enabled by Google)).Note that
myip.opendns.com
is only resolvable throughresolver1.opendns.com
, and not withauth1.opendns.com
— so, they seem to be doing some DNS hijacking and man-in-the-middle of their own domain name! Thus, you cannot use it to find the IP of a random resolver, sinceresolver1.opendns.com
is not authoritative formyip.opendns.com
.Note that
o-o.myaddr.l.google.com
looks like the most flexible and future-proof approach; it's even good for testing whether your DNS resolver supports the experimental EDNS0 extension for client subnet (which very few resolvers have the support for):A very simple anwser if you have internet access is:
Bear in mind, trusting third party sources for your IP might be problematic especially if what your doing with that data has special meaning.
A more trustworthy way is to pick a known, trustworthy DNS server (ideally running DNSSEC) and query the hostname of the box with it, providing your DNS server contains such entries;
I wrote a simple and fast webservice for this.
You can ask for your IPv4:
Or IPv6:
It also supports HTTPS, DNS, SSH.
The API is documented on http://api.ident.me/
One way: http://www.whatismyip.com/
If
then you can just parse the output from ifconfig for the IP addresses of the interfaces (the "inet addr:" part) to get the list of IP addresses of all your interfaces. The one IP address that is not in the private range (see http://en.wikipedia.org/wiki/IP_address#IPv4_private_addresses ) is your public IP address.
The same list can also be obtained through
which may be easier to parse.
If you do not have a direct internet connection (NAT etc.), there is no way to find your public IP address without external help (since your computer does not know it). Then you'll have to do it like in the other answers.
I took a little different approach by using the STUN protocol which was designed for NAT Traversal. If you use Ubuntu you can just install the package "stun" by typing:
The package installs a STUN server which you probably wont need, but it also comes with a STUN test client which I used to solve this problem. Now you can retrieve your public IP with one (not so simple) command:
Now, what it does is: stun contacts the public STUN server "stunserver.org" and gets an answer back with your public IP, the rest of the command is just to filter out the IP from the output.
Google now displays your public IP address: http://www.google.com/search?q=ip
Okay...I know that this is WAY after the fact and probably not even worth posting, but here's my working solution.
Nice and simple.