Sign up ×
Server Fault is a question and answer site for 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 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

24 Answers 24

up vote 38 down vote accepted


curl (for just the ip)

curl (for more info, takes time)

For more commands visit:

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

You can request from OpenDNS. dig @

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

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;

dig +short `hostname`
share|improve this answer

dig -t txt +short

dig -4 -t a +short

dig -4 -t a +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 is only resolvable through, and not with — 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 is not authoritative for

Note that 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 @ -t txt +noall +answer +stats | tail -8
;; global options:  printcmd 60     IN      TXT     "" 60     IN      TXT     "edns0-client-subnet"
;; Query time: 13 msec
;; WHEN: Sun Dec  8 20:38:46 2013
;; MSG SIZE  rcvd: 114
share|improve this answer
@Matt3o12 < I found OpenDNS: dig +short AAAA @2620:0:ccc::2. – bufh Jun 11 at 21:36

One way:

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

I wrote a simple and fast webservice for this.


You can ask for your IPv4:


Or IPv6:


And the API is documented on

share|improve this answer


  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 ) 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

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

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
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
+1 for a page that doesn't need grepping. Also wget -q -O - – xofer Nov 10 '11 at 2:28

Google now displays your public IP address:

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. – Jake Wilson 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

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


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
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 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 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 "" 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

The simpliest way is to use, as suggested.

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

For IP:


For public Hostname:


For all informations in a XML file:


etc... just check out

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:

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

A simple shell script solution can be found here:

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

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 - | 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

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

    also working with ipv6:

    $ curl

    With netcat:

    $ echo "GET /" | nc 80

    On a Cisco router:

  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 23
    $ nc 23

    works the same with a router:


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

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

exec 5<>/dev/tcp/"${TCP_HOST}"/"${TCP_PORT}"
echo -e "GET / HTTP/1.0\nHOST:${TCP_HOST}\n" >&5
while read -r line 
    case "$line" in
        *"Your local IP address is"* ) 
            line="${line#*Your local IP address is&nbsp;}"
            echo "Your ip is: $line"
            exec >&5-
done <&5


share|improve this answer
lynx --dump | grep -o '[0-9].*\.[0-9].*\.[0-9].*\.[0-9].*'
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
He wants his public IP address. Not the local. – Jake Wilson Nov 10 '11 at 0:01
wget -q -O -|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. This will give you a DNS entry like

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`

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

Use curl to hit's ip service

curl -s


share|improve this answer
Broken. 301 Moved Permanently – mralexgray Jan 30 at 1:31
My bad, everything has been updated to use https – Chris Montanaro Jan 30 at 16:57

On OS X here are two simple solutions to get both the private and public IP (with bonus code if you use LaunchBar).

Private IP

$ ipconfig getifaddr $1
# $1=en0 || en1 || en*

LaunchBar Script


title="$USER@$(HOSTNAME -s)"
text=$(ipconfig getifaddr en1)

open "x-launchbar:large-type?font-name=TerminalDosis-Light&string=$text&title=$title"

Public IP

$ dig +time=1 +tries=1 +retry=1 +short
# ||    
$ curl $1
# $1= || || || || || || ||

LaunchBar Script


title="$USER@$(HOSTNAME -s)"
text=$(dig +time=1 +tries=1 +retry=1 +short

open "x-launchbar:large-type?font-name=TerminalDosis-Light&string=$text&title=$title"
share|improve this answer

protected by Sven Jul 23 at 15:50

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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