Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I check with google, I can see my public IP. Is there something on the Ubuntu command-line which will yield me the same answer?

share|improve this question
"having dynamic IP", "SSH using some other system across the internet", "the command which will display the present PUBLIC IP". You see the chicken/egg problem here? How would you be able to run commands on a remote server without knowing its address? You might be more interested in services like / – gertvdijk Jan 9 '13 at 13:11
one cannot SSH without knowing the public IP my friend... dynDNS costs a lot and no-ip tough works but the situation don't allow that... anyway the question has been already answered.. thanks for your suggestion – Z9iT Jan 10 '13 at 8:46
PS (no command line, but no big brother G neither) – Campa Jan 28 '15 at 7:28

20 Answers 20

up vote 269 down vote accepted

If you are behind a router, then your computer will not know about the public IP address as the router does a network address translation.

If you are not behind a router, you can find it out using ifconfig. Else you could request this website using curl or wget and extract the information you need from it, e.g. using a perl script.

curl -s | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'  

or shorter

wget -qO -

should do the trick.

share|improve this answer
ty - right after I posted, I realized that I didn't google for an answer first: looks like this will work curl -s|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' Other possibilities are listed here: – kfmfe04 Jan 16 '12 at 12:01
sure - you can add it to your answer – kfmfe04 Jan 16 '12 at 13:18
This doesn't work anymore. is displaying the IP in an image (<img src='ipimg.php'/>). – fiatjaf Oct 27 '13 at 6:17
Exacly as Giovanni P stated. The OP should change the accepted anwser. – loostro Apr 11 '14 at 21:45
curl -s – chao Oct 7 '15 at 20:38

For finding the external ip, you can either use external web-based services, or use system based methods. The easier one is to use the external service, also the ifconfig based solutions will work in your system only if you're not behind a NAT. the two methods has been discussed below in detail.

Finding external IP using external services

The easiest way is to use an external service via a commandline browser or download tool. Since wget is available by default in Ubuntu, we can use that.
To find your ip, use-

$ wget -qO- ; echo


You could also use lynx(browser) or curl in place of wget with minor variations to the above command, to find your external ip.

Using curl to find the ip:

$ curl

For a better formatted output use:

$ curl ; echo

A faster (arguably the fastest) method using dig with OpenDNS as resolver:

The other answers here all go over HTTP to a remote server. Some of them require parsing of the output, or rely on the User-Agent header to make the server respond in plain text. They also change quite frequently (go down, change their name, put up ads, might change output format etc.).

  1. The DNS response protocol is standardised (the format will stay compatible).
  2. Historically DNS services (OpenDNS, Google Public DNS, ..) tend to survive much longer and are more stable, scalable and generally looked after than whatever new hip HTTP service is hot today.
  3. (for those geeks that care about micro-optimisation), this method should be inherently faster (be it only by a few micro seconds).

Using dig with OpenDNS as resolver:

$ dig +short


Copied from:

Finding external IP without relying on external services

  • If you know your network interface name

Type the following in your terminal:

$ LANG=c ifconfig <interface_name> | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

In the above, replace <interface_name> with the name of your actual interface, e.g: eth0, eth1, pp0, etc...

Example Usage:

$ LANG=c ifconfig ppp0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
  • If you don't know your network interface name

Type the following in your terminal (this gets the name and ip address of every network interface in your system):

$ LANG=c ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'

Example Usage:

$ LANG=c ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
ppp0: 111.222.333.444

N.B: Outputs are indicative and not real.



  1. LANG=c has been added to ifconfig based usages, so that it always gives the english output, irrespective of locale setting.
share|improve this answer
@Z9iT, Sure.. It should work in any linux distribution provided that you have wget installed. As said if you have either curl or lynx already available please use that instead. You would need root permission to install so use sudo apt-get install wget – saji89 Jun 1 '12 at 12:19
The commands with ifconfig do only work, if you are not behind a NAT. – lukassteiner Jan 23 '13 at 15:52
just use -w curl option instead of echo :) curl -w '\n' – drAlberT Jun 11 '14 at 8:17
This proposal using dig is pretty nice… – binaryanomaly Mar 14 '15 at 15:31
@binaryanomaly, thanks for that. I'll add it to the answer. – saji89 Mar 16 '15 at 5:55

My favorite has always been :


simple, easy to type.

You will have to install curl first ;)

share|improve this answer
@Z9iT, I just checked this now. Yes, it would output the external ip in your terminal. – saji89 Jun 1 '12 at 13:13
The response time from seems quite a bit slower than – Drew Noakes Oct 25 '13 at 14:57
If you don't have curl but have wget: wget -U curl -qO- – sch Aug 14 '14 at 20:16
1 does not seem to be responding :( – Asfand Yar Qazi Jan 20 at 11:51
@AsfandYarQazi - working here. You can try one of the alternates , – bodhi.zazen Jan 20 at 12:48 is my favorite.


You can request IPv4 explicitly:


If you don't have curl you can use wget instead:

wget -qO-
share|improve this answer
try – yprez Jan 10 '13 at 11:15
IPv4: curl IPv6: curl – Peter Feb 21 '14 at 10:43
Busybox doesn't have curl, use this instead: wget -qO- – Hengjie Feb 23 '14 at 8:59
here's a bash-only command: exec 3<>/dev/tcp/ && echo -e 'GET / HTTP/1.0\n' >&3 && cat <&3 – J.F. Sebastian Sep 8 '14 at 16:34
1 seems to not work anymore. – Hibou57 Jan 18 at 16:00

I've found everything to be annoying and slow, so I wrote my own. It's simple and fast.

Its API is on


share|improve this answer
Woah, that was really fast! – waldyrious Dec 7 '13 at 4:19
I find the solution faster and it includes a newline in the output. – Tyler Collier Aug 19 '14 at 0:00
Yes, it was indeed faster lately. Just tweaked my solution. – Pierre Carrier Sep 9 '14 at 13:53
Kudos! 2 years and you are still maintaining it. Well done. "IDENTify ME", is what comes to my mind, when I need ip check :) – Mohnish Sep 28 '15 at 1:37

You could use a DNS request instead of HTTP request to find out your public IP:

$ dig +short

It uses dns server to resolve the magical hostname to your ip address.

share|improve this answer
This is really fast. I did one warmup execution, then 10 executions each of this and curl Average for the curl version: 174ms. Average for the DNS-only version: 9ms. ~19x faster. See also: – Adam Monsen Mar 10 '15 at 21:06
@AdamMonsen Thank you for the link. The point of using DNS (as the answer that you've linked says) is that the response is standard (and unlikely to change) and the service (OpenDNS) might stick around longer than most of its http alternatives. The time it takes to make the request might be shadowed by the command start up time. – J.F. Sebastian Mar 11 '15 at 0:40
Yep. I wouldn't be surprised if curl itself is slower than dig. Even if they were rewritten to be as similar as possible, curl would still be slower; it uses HTTP (including DNS) and dig only uses DNS. – Adam Monsen Mar 11 '15 at 4:16

The one i'm using is :

wget -O - -q

Yes, you can have ip :-)

share|improve this answer
I prefer curl sometimes wget is the only one available, but sometimes no wget is available as well and curl is your only option (like OS/X). Either way curl is almost as easy as curl but much funnier ;-) – TryTryAgain Aug 25 '12 at 20:38

Type in this exactly, press Enter where indicated:

telnet 80Enter
GET /plain HTTP/1.1Enter
HOST: Enter
BROWSER: web-kitEnter

This manually submits a HTTP request, which will return your IP at the bottom of a HTTP/1.1 200 OK reply

Example output:

$ telnet 80
Connected to
Escape character is '^]'.
GET /plain HTTP/1.1
BROWSER: web-kit

HTTP/1.1 200 OK
Date: Tue, 02 Jul 2013 07:11:42 GMT
Server: Apache
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: no-cache
Pragma: no-cache
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html

share|improve this answer
Nice, this worked well, not having to install curl was an advantage for me: one liner: printf "GET /plain HTTP/1.1\nHOST:\nBROWSER: web-kit\n\n" | nc 80 – Ozone Mar 19 '14 at 5:32

Another fast one (might well be the fastest, relatively)

share|improve this answer

I have a stupid service for this by telnet. Something like this:


Your IPv4:
Your IPv6: ::ffff:xxxx:xxxx

Feel free to use it.

share|improve this answer

You can read a web page using only bash, without curl, wget:

$ exec 3<> /dev/tcp/ && # open connection
  echo 'GET /' >&3 &&                   # send http 0.9 request
  read -u 3 && echo $REPLY &&           # read response
  exec 3>&-                             # close fd
share|improve this answer
That's cool! Very hacker-ish way. :) – Tomofumi Jan 30 '15 at 8:28

For this, STUN was invented. As a client you can send a request to a publicly available STUN server and have it give back the IP address it sees. Sort of the low level as it uses no HTTP and no smartly crafted DNS servers but the blazingly fast STUN protocol.

Using stunclient

If you have stunclient installed (apt-get install stuntman-client on debian/ubuntu) you can simply do:

Binding test: success
Local address: A.B.C.D:42541
Mapped address: W.X.Y.Z:42541

where A.B.C.D is the IP address of your machine on the local net and W.X.Y.Z is the IP address servers like websites see from the outside (and the one you are looking for). Using sed you can reduce the output above to only an IP address:

stunclient |
    sed -n -e "s/^Mapped address: \(.*\):.*$/\1/p"

However, your question was how to find it using the command line, which might exclude using a STUN client. So I wonder...

Using bash

A STUN request can be handcrafted, sent to an external STUN server using netcat and be post-processed using dd, hexdump and sed like so:

$echo -en '\x00\x01\x00\x08\xc0\x0c\xee\x42\x7c\x20\x25\xa3\x3f\x0f\xa1\x7f\xfd\x7f\x00\x00\x00\x03\x00\x04\x00\x00\x00\x00' |
    nc -u -w 2 3478 |
    dd bs=1 count=4 skip=28 2>/dev/null |
    hexdump -e '1/1 "%u."' |
    sed 's/\.$/\n/'

The echo defines a binary STUN request (0x0001 indicates Binding Request) having length 8 (0x0008) with cookie 0xc00cee and some pasted stuff from wireshark. Only the four bytes representing the external IP are taken from the answer, cleaned and printed.

Working, but not recommended for production use :-)

P.S. Many STUN servers are available as it is a core technology for SIP and WebRTC. Using one from Mozilla should be safe privacy-wise but you could also use another: STUN server list

share|improve this answer

I like:



  • it returns just the plaintext IP, nothing else
  • it is from a well known provider which is unlikely to go offline anytime soon
share|improve this answer

Use cURL with (IPv4 and IPv6 are supported).





share|improve this answer

For those of us with login access to our routers, using a script to ask the router what its' WAN IP address is is the most efficient way to determine the external IP address. For instance the following python script prints out the external IP for my Medialink MWN-WAPR300N router:

import urllib, urllib2, cookielib
import re
from subprocess import check_output as co

cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))

def get(url, values=None):
  data = None
  if values: data = urllib.urlencode(values)
  req = urllib2.Request(url, data)
  rsp = urllib2.urlopen(req)

router = co(['ip', '-o', 'ro', 'list', '']).split()[2]
url = "http://" + router

get(url+"/LoginCheck", dict(checkEn='0', Username='admin', Password='admin'))
page = get(url+"/system_status.asp")

for line in page.split("\n"):
  if line.startswith("wanIP = "):
    print line.split('"')[1]

Note that this is not very secure (as is the case with plaintext credentials & logging in to most routers), and is certainly not portable (needs to be changed for each router). It is however very fast and a perfectly reasonable solution on a physically secure home network.

To customize the script for another router, I recommend using the tamperdata addon in firefox to determine what HTTP requests to make.

share|improve this answer
You should try getting the IP address of the router grammatically. For example router = subprocess.check_output(['ip', '-o', 'ro', 'list', '']).split()[2]. – Cristian Ciupitu Jun 2 '14 at 2:26

These will get the local IPs:


or for shorter output:

ifconfig | grep inet


ip addr show

and probably:

hostname -I

This should get the external IP

wget -O - -q ; echo

N.B. If you don't mind to installing curl, this as well:

share|improve this answer
ifconfig | sed -nre '/^[^ ]+/{N;s/^([^ ]+).*addr: *([^ ]+).*/\1,\2/p}' will print the local interfaces and corresponding V4 IP's – Hannu Jun 27 '14 at 18:09
ifconfig | sed -nre '/^[^ ]+/{N;N;s/^([^ ]+).*addr: *([^ ]+).*addr: *([^ ]+).*/\1,\2,\3/p}' - v4 and v6 IPs. – Hannu Jun 27 '14 at 18:15

If you have installed lynx in Ubuntu type

share|improve this answer
curl works too. – Tomofumi Jan 30 '15 at 8:26

Simply issue a traceroute for any website or service..

sudo traceroute -I

Line 2 always seems to be my public IP address after it gets past my router gateway.

user@user-PC ~ $ sudo traceroute -I
traceroute to (, 30 hops max, 60 byte packets
 1 (  230.739 ms  231.416 ms  237.819 ms
 2 (  249.136 ms  250.754 ms  253.994 ms**

So, make a bash command.

sudo traceroute -I | awk -F '[ ]' '{ if ( $2 ="2" ) { print $5 } }'

And the output...


I don't think relying on PHP scripts and the sort is good practice.

share|improve this answer
this is interesting, though really slow and doesn't get my external ip like this – rubo77 Oct 5 '15 at 2:46

A command with no dependencies except being a GOogle DNS:

echo $(ip route get | awk '{print $NF; exit}')
share|improve this answer
This only tells you the IP-address of your local outgoing interface. – guntbert Sep 17 '15 at 16:15
the command does show the public ip address if I run it on a cloud server. wasn;t that the question? – Rolf Sep 17 '15 at 20:48
No, where do you see "cloud server"? – guntbert Sep 17 '15 at 20:52

Just go to, and it will tell you.

You can refer this question if you want to know how to browse the internet on the terminal.

share|improve this answer
Please read the question. He wants to ask how to find ip in terminal command line. – Muhammad bin Yusrat Nov 7 '15 at 6:35

protected by Seth Feb 28 '14 at 4:30

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

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.