44

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)

3
  • are you trying to do this programatically ? Oct 6, 2009 at 5:39
  • yes in shell script
    – shantanuo
    Oct 6, 2009 at 6:15
  • 1
    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)? Apr 5, 2010 at 22:50

23 Answers 23

52

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

6
  • 1
    What about IPv6 addresses?
    – Matt3o12
    Aug 14, 2014 at 20:01
  • 1
    That link is dead. You can use this instead: curl http://checkip.amazonaws.com/
    – Farhan.K
    Mar 3, 2017 at 13:03
  • @Farhan.K please post this as an answer since it is probably best answer. If you do not, I will and mention your name suggesting it
    – Aliostad
    Jun 26, 2017 at 20:50
  • @Aliostad feel free to post it :)
    – Farhan.K
    Jun 27, 2017 at 9:26
  • @Farhan.K argh, protected :/
    – Aliostad
    Jun 29, 2017 at 10:48
28

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

2
  • 8
    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. Apr 6, 2012 at 17:53
  • 2
    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. Sep 23, 2014 at 8:34
19

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
2
  • 3
    @Matt3o12 < I found OpenDNS: dig +short AAAA myip.opendns.com @2620:0:ccc::2.
    – bufh
    Jun 11, 2015 at 21:36
  • Nowadays the method getting TXT records from Google supports both IPv4 and IPv6 so you should specify -4 or -6 if you need a specific IP version.
    – Martin
    Sep 29, 2018 at 12:06
12

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

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

It also supports HTTPS, DNS, SSH.

The API is documented on http://api.ident.me/

7

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

4
5

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_address#IPv4_private_addresses ) 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.

4

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.

1
  • stun.l.google.com is faster :D
    – Zibri
    Apr 27, 2019 at 15:47
3

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

6
  • It serves plain text with a type of text/html...
    – bortzmeyer
    Nov 28, 2009 at 18:22
  • isn't it displaying your IP in your browser ?
    – Biranchi
    Nov 28, 2009 at 20:08
  • 2
    But with the wrong type.
    – bortzmeyer
    Dec 14, 2009 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, 2010 at 22:35
  • 3
    +1 for a page that doesn't need grepping. Also wget -q -O - http://wgetip.com/
    – xofer
    Nov 10, 2011 at 2:28
3

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

5
  • That's not working for me, either in a browser or via curl.
    – Ladadadada
    Jun 1, 2012 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. Jun 1, 2012 at 17:56
  • Worked on my 3G connection. Learn more points here.
    – Ladadadada
    Jun 1, 2012 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, 2013 at 0:02
  • curl -qs "google.com/search?q=blowme"|tr "()" "\n"|grep Client|cut -d ":" -f 2|tr -d " "
    – Zibri
    Apr 27, 2019 at 15:54
3

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.

1
  • 2
    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, 2013 at 9:17
3

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

2

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.

2

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.

1

Duplicate of many other questions (hence my -1 vote).

1

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

1

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

0

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

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

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

1
  • 3
    He wants his public IP address. Not the local. Nov 10, 2011 at 0:01
0
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
0

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.

0

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

#!/bin/sh

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 myip.opendns.com @resolver1.opendns.com
# ||    
$ curl $1
# $1=http://wtfismyip.com/text || http://ip-addr.es || http://icanhazip.com || http://wgetip.com || http://ident.me || http://ifconfig.me || https://shtuff.it/myip/short || http://curlmyip.com

LaunchBar Script

#!/bin/sh

title="$USER@$(HOSTNAME -s)"
text=$(dig +time=1 +tries=1 +retry=1 +short myip.opendns.com @resolver1.opendns.com)

open "x-launchbar:large-type?font-name=TerminalDosis-Light&string=$text&title=$title"
1
  • FWIW ident.me supports IPv4 on v4.ident.me and IPv6 on v6.ident.me; otherwise you'll get whichever is preferred. It also supports dig. Details @ api.ident.me (disclaimer: I'm its maintainer) Mar 3, 2022 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .