How can I get my public IP address from the command line, if I am behind a router?

Assuming your system has 2 ethernet devices, eth0 and eth1 and eth0 is connected to your LAN, say IPs 192.168.1.X and your eth1 device is connected to your ISP (WAN) you're going to want to use the following ifconfig command to get your IP for the WAN side.

NOTE: The 1st 2 ways assume that you're running them against a computer that has 2 ethernet devices and that one of them is connected to your ISP (cable modem and/or DSL modem). In this scenario the ethernet device (eth1) will be configured with your IP address on the internet (WAN IP).

1st way

                          +------------------------+
  +--------+    WAN IP    |   Computer that wants  |  LAN IP
  |Internet|--------------|     to know WAN IP     |------------
  +--------+  54.234.1.33 | +------+      +------+ | 192.168.1.1
                          +-| eth1 |------| eth0 |-+
                            +------+      +------+

% ifconfig eth1 | awk '/inet / { print $2 }' | sed -e s/addr://
54.234.1.33

You can also use the ip command.

% ip addr show eth1 | awk '/inet/ {print $2}' | sed 's#/.*##'
54.234.1.33

2nd way

If you need to find this out from a system that sits only on the LAN you could setup a passphrase-less ssh key and add it to an account on your LAN machine so that it could remotely access the system with the WAN access like so.

                                                            +----------------+
  +--------+    WAN IP      +-------------+      LAN IP     | Computer that  |
  |Internet|----------------|remote-server|-----------------| wants to know  |
  +--------+  54.234.1.33  +----+-----+----+  192.168.1.x  +----+ WAN IP     |
                           |eth1|     |eth0|               |eth0|------------+
                           +----+     +----+               +----+

% ssh ruser1@remote-server "ifconfig eth1 | awk '/inet / { print \$2 }' | sed -e s/addr://"
54.234.1.33

3rd way

If you're unable to ssh into the box that has WAN access and you're using a home router/switch such as a Linksys or Netgear box. You may be able to get the IP from that device via a HTTP status page. I've done this in the past as well, something similar to what's described in this whatismyip.com forum post.

                                                               192.168.1.2
                                                            +----------------+
  +--------+    WAN IP      +-------------+      LAN IP     | Computer that  |
  |Internet|----------------|router/switch|-----------------| wants to know  |
  +--------+  54.234.1.33   +-------------+   192.168.1.x  +----+ WAN IP     |
                              192.168.1.1                  |eth0|------------+
                                                           +----+

# something like this....

% wget -q -O - http://<username>:<password>@192.168.1.1/Status_Router.asp | grep "ipaddress" | cut -d" " -f2

NOTE: This approach is highly dependent on which router/switch you have, whether it's a Linksys, Netgear, etc. brand. Each will have their own unique page with the WAN IP on it.

4th way

Sending a query against an external internet site which will report back your WAN IP address.

NOTE: I'm aware that the original question mentioned that they were looking for alternatives to this approach but I'm putting it in here so that this answer covers all the bases.

                                                        +---------------+
  +-------------+   +--------+   +------+     LAN IP    | Computer that |
  |whatsmyip.com|---|Internet|---|router|---------------| wants to know |
  +-------------+   +--------+   +------+  192.168.1.x +----+ WAN IP    |
you're 54.234.1.33                                     |eth0|-----------+
                                                       +----+

# 1st server
% wget -qO - ipv4bot.whatismyipaddress.com
54.234.1.33

# 2nd server
% curl 'https://api.ipify.org?format=json'

{"ip":"54.234.1.33"}
% curl 'https://api.ipify.org?format=txt'
54.234.1.33

# 3rd server
% curl -s checkip.dyndns.org | sed 's#.*Address: \(.*\)</b.*#\1#'
54.234.1.33

Additional info is available here: HOWTO: Check you external IP Address from the command line


If your system has curl installed (most do), you can use

curl ifconfig.me

When you are behind a NAT Router with UPNP, you can use miniupnpc to detect wan ip address:

# debian/ubuntu setup: 
# sudo apt-get install miniupnpc

# get WAN IP address from UPNP router:
upnpc -s | grep ^ExternalIPAddress | cut -c21-

You could use it in a script e.g. for cron like:

#!/bin/bash
#
# In this example, lynx is used as http client. you could also use something else
# like wget etc.
# debian/ubuntu lynx setup: 
#
# apt-get install lynx
#

EXTIP=`upnpc -s | grep ^ExternalIPAddress | cut -c21-`
DDNSURL="http://your-ddns-service.com/update/my/ip/to/$EXTIP"
TFILE=/tmp/.WAN_IP
if [ -f $TFILE ]; then
        OXTIP=`head -1 $TFILE`
else
        touch $TFILE
        OXTIP="NULL"
fi
if [ "$EXTIP" != "$OXTIP" ]; then
        mv $TFILE "$TFILE~"
        echo "$EXTIP" > $TFILE
        lynx -source $DDNSURL > /dev/null
        echo "================================"
        date
        echo "WAN_IP = $EXTIP"
fi