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)

shantanuo
  • 3,459
  • 8
  • 47
  • 64
  • are you trying to do this programatically ? – Andrew Keith Oct 06 '09 at 05:39
  • yes in shell script – shantanuo Oct 06 '09 at 06: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)? – Stefan Lasiewski Apr 05 '10 at 22:50

23 Answers23

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

tsanand129
  • 644
  • 6
  • 3
28

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

Zoredache
  • 128,755
  • 40
  • 271
  • 413
  • 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`. – Michael Kropat Apr 06 '12 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. – Christopher Chiche Sep 23 '14 at 08: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
cnst
  • 12,948
  • 7
  • 51
  • 75
  • 3
    @Matt3o12 < I found OpenDNS: `dig +short AAAA myip.opendns.com @2620:0:ccc::2`. – bufh Jun 11 '15 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 '18 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`
Matthew Ife
  • 22,927
  • 2
  • 54
  • 71
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/

Pierre Carrier
  • 2,607
  • 17
  • 28
7

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

Michael Todd
  • 300
  • 1
  • 8
  • 15
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.

sleske
  • 9,851
  • 4
  • 33
  • 44
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.

3

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

Jake Wilson
  • 8,494
  • 29
  • 94
  • 121
  • That's not working for me, either in a browser or via curl. – Ladadadada Jun 01 '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 01 '12 at 17:56
  • Worked on my 3G connection. [Learn more points here](http://support.google.com/websearch/bin/answer.py?hl=en&answer=1696588). – Ladadadada Jun 01 '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 04 '13 at 00:02
  • curl -qs "http://www.google.com/search?q=blowme"|tr "()" "\n"|grep Client|cut -d ":" -f 2|tr -d " " – Zibri Apr 27 '19 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.

Skittles
  • 411
  • 7
  • 15
  • 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 '13 at 09: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

Kelindil
  • 713
  • 1
  • 5
  • 7
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

Biranchi
  • 131
  • 3
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.

petrus
  • 5,287
  • 25
  • 42
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.

Hammad Akhwand
  • 151
  • 1
  • 1
  • 5
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).

Stefan Lasiewski
  • 22,949
  • 38
  • 129
  • 184
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

quanta
  • 50,327
  • 19
  • 152
  • 213
1

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

bortzmeyer
  • 3,903
  • 1
  • 20
  • 24
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 "/"

0
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
splattne
  • 28,348
  • 19
  • 97
  • 147
Fathi
  • 1
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.

cgmonroe
  • 1
  • 1
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"
  • 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 @ https://api.ident.me (disclaimer: I'm its maintainer) – Pierre Carrier Mar 03 '22 at 20:12
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