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.