Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)

share|improve this question

migrated from stackoverflow.com Nov 29 '09 at 5:25

This question came from our site for professional and enthusiast programmers.

    
are you trying to do this programatically ? –  Andrew Keith Oct 6 '09 at 5:39
    
yes in shell script –  shantanuo Oct 6 '09 at 6:15
    
Are you using Linux, MacOSX, FreeBSD, etc? The output of 'ifconfig' is different for these different OSes, and the 'ip' command doesn't exist on MacOSX. Are you looking for the IP of your computer (You'll need a shell script to parse ifconfig, or something), or your router (The websites below might work)? –  Stefan Lasiewski Apr 5 '10 at 22:50

23 Answers 23

up vote 27 down vote accepted
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

share|improve this answer
    
What about IPv6 addresses? –  Matt3o12 Aug 14 at 20:01

One way: http://www.whatismyip.com/

share|improve this answer
1  
second way: ipchicken.com –  bobby Oct 6 '09 at 5:26
    
Or, if you want an XML API, ip-address.domaintools.com/myip.xml. I saw one out there that returns plain-text. –  ceejayoz Apr 5 '10 at 22:27
6  
Fourth way: moanmyip.com –  MikeyB Nov 10 '11 at 0:47
1  
Follow the rules or you won't get anything. whatismyip.com/faq/automation.asp –  Ladadadada Jun 1 '12 at 10:51

If

  1. you only have one public IP address and
  2. you are directly connected to the internet (no NAT / proxy / VPN etc)

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%5Faddress#IPv4%5Fprivate%5Faddresses ) is your public IP address.

The same list can also be obtained through

ip addr show

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.

share|improve this answer

If what you want is to find the external ip address on your router, you either ask the router itself for its wan address, or ask someone outside to get it for you..

for a manual way you can browse any of the above given sites that will return the ip of the incomming request.

For an automated way, you can try :

wget -q -O - http://www.ipaddressworld.com | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}'\. 

which will get you the line that contains the ip address on the http response, then parse it out with sed, awk , etc

share|improve this answer

One way is , you can make a request to the page at

http://www.biranchi.com/ip.php

it returns the IP address of your system

share|improve this answer
    
It serves plain text with a type of text/html... –  bortzmeyer Nov 28 '09 at 18:22
    
isn't it displaying your IP in your browser ? –  Biranchi Nov 28 '09 at 20:08
2  
But with the wrong type. –  bortzmeyer Dec 14 '09 at 21:18
    
@bortzmeyer Uh, what's the "right type"? text/html is fine for what that pseudo-API returns - it's certainly not XML or JSON. text/plain maybe, but it's entirely usable as text/html. –  ceejayoz Apr 5 '10 at 22:35
3  
+1 for a page that doesn't need grepping. Also wget -q -O - http://wgetip.com/ –  xofer Nov 10 '11 at 2:28

you can use just the shell to check your external ip, also using external providers

#!/bin/bash
TCP_HOST="checkmyip.com"
TCP_PORT=80              
exec 5<>/dev/tcp/"${TCP_HOST}"/"${TCP_PORT}"
echo -e "GET / HTTP/1.0\nHOST:${TCP_HOST}\n" >&5
while read -r line 
do  
    case "$line" in
        *"Your local IP address is"* ) 
            line="${line#*Your local IP address is&nbsp;}"
            line=${line%%</p>*}
            echo "Your ip is: $line"
            exec >&5-
            exit
            ;;            
    esac
done <&5

output

share|improve this answer

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:

sudo apt-get install stun

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:

stun -v stunserver.org 2>&1 1>/dev/null | grep MappedAddress | sed -e 's/.*MappedAddress = //' -e 's/:.*//' | uniq

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.

share|improve this answer

A simple shell script solution can be found here:

http://bash.cyberciti.biz/misc-shell/read-local-ip-address/

Works on Linux, FreeBSD, SunOS and Apple Darwin (with a minor modification).

share|improve this answer
lynx --dump http://www.whatismyip.com/ | grep -o '[0-9].*\.[0-9].*\.[0-9].*\.[0-9].*'
-m1 
share|improve this answer

I do this. It just gives me the IP without any third-party involvement..

ip addr show | grep eth0 | grep inet | tr -s " " | cut -f3 -d " " | cut -f1 -d "/"

share|improve this answer
2  
He wants his public IP address. Not the local. –  Jakobud Nov 10 '11 at 0:01

Google now displays your public IP address: http://www.google.com/search?q=ip

share|improve this answer
    
That's not working for me, either in a browser or via curl. –  Ladadadada Jun 1 '12 at 10:46
    
That's strange. Mine says "Your public IP address is XX.XX.XXX.XX - Learn more" at the top before the first serp. –  Jakobud Jun 1 '12 at 17:56
    
Worked on my 3G connection. Learn more points here. –  Ladadadada Jun 1 '12 at 22:25
    
Google for some strange reason only show your IP-address based on which User-Agent string is supplied to the page. With Firefox, it works; but SeaMonkey with the "Firefox" string disabled (through "general.useragent.compatMode.firefox" set to false), it suddenly doesn't. Have absolutely no clue why Google explicitly enables stuff like this only for Firefox instead of just any Gecko, since there are many other desktop browsers based on Gecko that are equally compatible, including SeaMonkey. –  cnst Jan 4 '13 at 0:02

A very simple anwser if you have internet access is:

curl icanhazip.com

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;

dig @trustworthysource.com +short `hostname`
share|improve this answer

You can request myip.opendns.com. from OpenDNS. dig @208.67.222.220 myip.opendns.com

share|improve this answer
6  
That’s just cool. For any easier to remember version, you can put in dig @resolver1.opendns.com myip.opendns.com. Or on Windows: nslookup myip.opendns.com resolver1.opendns.com. –  Michael Kropat Apr 6 '12 at 17:53
1  
What worked best for me: dig @208.67.222.220 myip.opendns.com +short in order to get directly the ip without having to go through the whole answer. –  Christopher Chiche Sep 23 at 8:34

I'm doing this a lot, and from a lot of devices, so I made my own two services on a server :

  1. php file at the root of a webserver :

    user@host:~$ cat index.php`
    <?php echo $_SERVER['REMOTE_ADDR']; echo "\n" ?>
    

    Usage on a shell:

    $ curl -4 mydomain.com
    79.22.192.12
    

    also working with ipv6:

    $ curl mydomain.com
    2a01:e34:ee7d:180::3
    

    With netcat:

    $ echo "GET /" | nc myserver.com 80
    2a01:e34:ee7d:180::3
    

    On a Cisco router:

    router#more http://myserver.com/index.php
    79.22.192.12
    
  2. quick hack with a custom telnet server: xinetd spawning /usr/bin/env:

    service telnet
    {
       server           = /usr/bin/env
       socket_type      = stream
       protocol         = tcp
       flags            = IPv6
       wait             = no
       port             = 23
       cps              = 3 30
       passenv          = %a
    }
    

    and then telnet to it :

    $ nc -4 myserver.com 23
    REMOTE_HOST=::ffff:79.22.192.12
    
    $ nc myserver.com 23
    REMOTE_HOST=2a01:e34:ee7d:180::3
    

    works the same with a router:

    router#telnet myserver.com
    79.22.192.12
    

This way you can make it work on your internal network, if some nat or proxy is involved in the communication and you'd like to know from what IP you appear.

It doesn't require any third party service.

share|improve this answer

Okay...I know that this is WAY after the fact and probably not even worth posting, but here's my working solution.

#!/bin/sh

IP="$(ifconfig | egrep 'inet ' | sed -e 's/inet //' -e 's/addr://' -e 's/ Bcast.*//' -e 's/127.*//')"
echo $IP

Nice and simple.

share|improve this answer
1  
This returns the internal IP address which was not asked. But, for your information, you can also do this with the command hostname -I (capital 'i'). –  Redsandro Mar 18 '13 at 9:17

I wrote a simple and fast webservice for this.

curl ident.me

You can ask for your IPv4:

curl v4.ident.me

Or IPv6:

curl v6.ident.me

And the API is documented on http://api.ident.me/

share|improve this answer

The simpliest way is to use http://ifconfig.me/, as suggested.

On this page, you will know what command to use for what information you want to retrieve.

For IP:

curl ifconfig.me
or
curl ifconfig.me/ip

For public Hostname:

curl ifconfig.me/host

For all informations in a XML file:

curl ifconfig.me/all.xml

etc... just check out http://ifconfig.me

share|improve this answer
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
share|improve this answer

Here is another alternative that depends on hosts who's business resolves around managing dynamic IP rather that "public service" sites that may go away or change format.

1) Register your server at one of the many free dynamic dns services (e.g. no-ip.com) This will give you a DNS entry like xxx.no-ip.org.

2) Install the service's dynamic update tool (reports IP changes to service).

To get the IP address in a script, just do:

$external_ip = `dig +short xxx.no-ip.org`

Great for use in cron job to check if dynamic IP has changed and some configuration entries need to be changed.

share|improve this answer

To avoid relying on external sources, I use expect, to telnet into my router and get the ip address of its public interface. Here's an example expect script:

#!/usr/bin/expect
if { $argc < 3 } {
puts "usage: ./telnet2router.exp router-ip username password"
return -1
}
set ip [lrange $argv 0 0]
set username [lrange $argv 1 1]
set password [lrange $argv 2 2]

spawn telnet $ip
expect "login:" {
    send "$username\r"
}
expect "Password:" {
    send "$password\r"
}
expect "#" {
    send "ifconfig ppp0 | grep inet\r"
    send "exit\r"
}
expect eof

I then execute the above script like this to get the public ip:

./telnet2router.exp <router-ip> <username> <password> | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1

Of course, this is based on the assumption that I have admin access to the router, and its a linux-based router, with ifconfig command available.

share|improve this answer

Use curl to hit shtuff.it's ip service

curl -s http://shtuff.it/myip/short

source: http://shtuff.it/article/8/Find_your_public_IP_address_in_bash_with_curl_or_wget

share|improve this answer

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's txt, which might actually work for IPv6 one day, if properly enabled by Google)).

Note that myip.opendns.com is only resolvable through resolver1.opendns.com, and not with auth1.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, since resolver1.opendns.com is not authoritative for myip.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):

% dig @8.8.8.8 -t txt o-o.myaddr.l.google.com +noall +answer +stats | tail -8
;; global options:  printcmd
o-o.myaddr.l.google.com. 60     IN      TXT     "74.125.189.16"
o-o.myaddr.l.google.com. 60     IN      TXT     "edns0-client-subnet 88.198.54.0/24"
;; Query time: 13 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Dec  8 20:38:46 2013
;; MSG SIZE  rcvd: 114
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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