Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
1  
"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 no-ip.com / DynDNS.org. –  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

15 Answers 15

up vote 23 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 wget and extract the information you need from it, e.g. using a perl script.

curl http://whatismyip.org/  

should do the trick, and its easy to remember.

share|improve this answer
20  
ty - right after I posted, I realized that I didn't google for an answer first: looks like this will work curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' Other possibilities are listed here: go2linux.org/what-is-my-public-ip-address-with-linux –  kfmfe04 Jan 16 '12 at 12:01
    
If he agrees I could also put this into my answer. However,I am usually against solving the problem for someone, instead I regard also "pointers into a direction" as answer. Users wont understand the context of what they are doing if they are always faced with complete solutions. –  Michael K Jan 16 '12 at 12:46
1  
sure - you can add it to your answer –  kfmfe04 Jan 16 '12 at 13:18
1  
To clarify: That was a hack, and a very ugly one at that, so I did an edit to make it simpler and something that people can remember. –  jrg Jan 16 '12 at 16:17
8  
This doesn't work anymore. whatsmyip.org is displaying the IP in an image (<img src='ipimg.php'/>). –  fiatjaf Oct 27 '13 at 6:17

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- http://ipecho.net/plain ; echo

Courtesy:

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 ipecho.net/plain

For a better formatted output use:

curl ipecho.net/plain ; echo

Finding external IP without relying on external services

  • If you know your network interface name

Type the following in your terminal:

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:

saji@geek-lap:~$ ifconfig ppp0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
111.222.333.444
  • If you don't know your network interface name

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

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:

saji@geek-lap:~$ ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
lo: 127.0.0.1
ppp0: 111.222.333.444

N.B: Outputs are indicative and not real.

Courtesy: http://www.if-not-true-then-false.com/2010/linux-get-ip-address/

share|improve this answer
1  
@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
3  
The commands with ifconfig do only work, if you are not behind a NAT. –  lukassteiner Jan 23 '13 at 15:52
    
@bayoffire, Thanks for that information, I'll update the answer now. –  saji89 Jan 23 '13 at 16:05
    
just use -w curl option instead of echo :) curl -w '\n' ident.me –  AlberT Jun 11 at 8:17

My favorite has always been :

curl ifconfig.me

simple, easy to type.

You will have to install curl first ;)

share|improve this answer
    
will this display the results inside terminal? –  Z9iT Jun 1 '12 at 12:45
3  
@Z9iT, I just checked this now. Yes, it would output the external ip in your terminal. –  saji89 Jun 1 '12 at 13:13
3  
The response time from ifconfig.me seems quite a bit slower than ipecho.net. –  Drew Noakes Oct 25 '13 at 14:57
    
If you don't have curl but have wget: wget -U curl -qO- ifconfig.me –  sch Aug 14 at 20:16

icanhazip.com is my favorite.

curl icanhazip.com

You can request IPv4 explicitly:

curl ipv4.icanhazip.com

Or IPv6:

curl ipv6.icanhazip.com

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

wget -qO- icanhazip.com
share|improve this answer
    
It returns ipv6 for me :'( –  HackToHell Jan 10 '13 at 9:41
2  
try ipv4.icanhazip.com –  Yuri Prezument Jan 10 '13 at 11:15
    
simple yet elegant solution –  ram Apr 15 '13 at 9:37
3  
IPv4: curl ipv4.icanhazip.com IPv6: curl ipv6.icanhazip.com –  Peter I Feb 21 at 10:43
2  
Busybox doesn't have curl, use this instead: wget -qO- http://icanhazip.com –  Hengjie Feb 23 at 8:59

The one i'm using is :

wget -O - -q icanhazip.com

Yes, you can have ip :-)

share|improve this answer
1  
I prefer curl icanhazip.com 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 icanhazip.com is almost as easy as curl ifconfig.me but much funnier ;-) –  TryTryAgain Aug 25 '12 at 20:38

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

Its API is on http://api.ident.me/

Examples:

curl ident.me
curl v4.ident.me
curl v6.ident.me
share|improve this answer
    
Woah, that was really fast! –  waldir Dec 7 '13 at 4:19
    
I find the icanhazip.com solution faster and it includes a newline in the output. –  Tyler Collier Aug 19 at 0:00

Type in this exactly, press Enter where indicated:

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

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 ipecho.net 80
Trying 146.255.36.1...
Connected to ipecho.net.
Escape character is '^]'.
GET /plain HTTP/1.1
HOST: ipecho.net
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

f
111.222.333.444
0
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: ipecho.net\nBROWSER: web-kit\n\n" | nc ipecho.net 80 –  Ozone Mar 19 at 5:32

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

$ dig +short myip.opendns.com @resolver1.opendns.com

It uses resolver1.opendns.com dns server to resolve the magical myip.opendns.com hostname to your ip address.

share|improve this answer

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

curl ipecho.net/plain
share|improve this answer

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

telnet myip.gelma.net

Your IPv4: xxx.xxx.xxx.xxx
Your IPv6: ::ffff:xxxx:xxxx

Feel free to use it.

share|improve this answer

If you have installed lynx in Ubuntu type

lynx bot.whatismyipaddress.com
share|improve this answer

Just go to whatismyip.com, 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

Use cURL with ipogre.com (IPv4 and IPv6 are supported).

IPv4

curl ipv4.ipogre.com

IPv6

curl ipv6.ipogre.com

http://www.ipogre.com/faqs/linux.php

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))
urllib2.install_opener(opener)

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

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

get(url+"/index.asp")
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]
    exit(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
1  
You should try getting the IP address of the router grammatically. For example router = subprocess.check_output(['ip', '-o', 'ro', 'list', '0.0.0.0/0']).split()[2]. –  Cristian Ciupitu Jun 2 at 2:26

These will get the local IPs:

ifconfig

or for shorter output:

ifconfig | grep inet

also

ip addr show

This should get the external IP

wget http://smart-ip.net/myip -O - -q ; echo

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

curl http://smart-ip.net/myip
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 at 18:09
    
ifconfig | sed -nre '/^[^ ]+/{N;N;s/^([^ ]+).*addr: *([^ ]+).*addr: *([^ ]+).*/\1,\2,\3/p}' - v4 and v6 IPs. –  Hannu Jun 27 at 18:15

protected by Seth Feb 28 at 4:30

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.