Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am on DHCP, having dynamic IP.

Finding your public IP when using a GUI and a browser is extremely easy. Simply go to Google.com and search for "What is my IP". Google will display it in the very first line.

However I am unable to find my public IP when inside Ubuntu server.

I have created a system with Ubuntu server (LAMP Stack) for my personal use. This system boots up simply in terminal mode. I want to do SSH using some other system across the internet; thus knowing the public IP becomes necessary.

What is the command to display the current public IP adress of that system?

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 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 at 8:46

6 Answers

up vote 32 down vote accepted

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

Update:
Replaced whatismyip.com link, with ipecho.net/plain as whatismyip.com have removed that script from their site. Also added methods to find external ip, without relying on external web based services.

share|improve this answer
Very good suggestion I think - however you can take whichever url that provides your public IP, and in combination with the grep command filter the incoming text. – cuichi Jun 1 '12 at 12:14
@cuichi, Yes, ofcourse the options are endless, I thought to share an easily understandable one. – saji89 Jun 1 '12 at 12:16
will "apt-get install wget" install the "wget" I hope this will simply output the IP in the terminal. Does this hold true for any Linux distro? – Z9iT Jun 1 '12 at 12:16
@saji89: :) You did! – cuichi Jun 1 '12 at 12:17
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
show 5 more comments

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
1  
@Z9iT, I just checked this now. Yes, it would output the external ip in your terminal. – saji89 Jun 1 '12 at 13:13

icanhazip.com is my favorite.

curl icanhazip.com
share|improve this answer
It returns ipv6 for me :'( – HackToHell Jan 10 at 9:41
1  
try ipv4.icanhazip.com – Yuri Prezument Jan 10 at 11:15
simple yet elegant solution – ram Apr 15 at 9:37

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

If you have installed lynx in Ubuntu type

lynx bot.whatismyipaddress.com
share|improve this answer

You may use curl Install curl to find your Public IP using terminal.

You can also check your public IP using sites like IP-Details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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