How can I find my public IP using the terminal? [duplicate]

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?


Solution 1:

If you are not behind a router, you can find it out using ifconfig.

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. You could ask some website what your public IP address is using curl or wget and extract the information you need from it:

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

or shorter

curl https://ipinfo.io/ip

Solution 2:

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

Courtesy:

  • https://hawknotes.blogspot.in/2010/06/finding-your-external-ip-address.html
  • https://ipecho.net/plain

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

For a better formatted output use:

$ curl https://ipecho.net/plain ; 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 whatismyip.com 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 myip.opendns.com @resolver1.opendns.com

111.222.333.444

Copied from: https://unix.stackexchange.com/a/81699/14497

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}'
111.222.333.444
  • 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 }'
lo: 127.0.0.1
ppp0: 111.222.333.444

N.B: Outputs are indicative and not real.

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

UPDATE

  1. LANG=c has been added to ifconfig based usages, so that it always gives the english output, irrespective of locale setting.

Solution 3:

My favorite has always been :

curl ifconfig.me

simple, easy to type.

You will have to install curl first ;)

If ifconfig.me is down try icanhazip.com and or ipecho.net

curl icanhazip.com

or

curl ipecho.net

Solution 4:

icanhazip.com is my favorite.

curl icanhazip.com

You can request IPv4 explicitly:

curl ipv4.icanhazip.com

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

wget -qO- icanhazip.com