Is there a way for me to get my public (WAN) IP address on the command line? I am behind a router (LAN network), with a dynamic IP address assigned by my ISP.

I have seen solutions using an external webservice (such as ifconfig.me), but I want to know if I can do it without an external service.

  • Can you please add more details as to your network setup? Are you behind a router/switch which DOES have access to the internet and you're trying to ascertain the WAN IP address that the router/switch is getting from your ISP?
    – slm
    Dec 22, 2012 at 14:27
  • @slm I am behind a router and ant to find out the ip assigned to the router by ISP
    – user61954
    Dec 22, 2012 at 14:57
  • See the 3rd way that I just added to my answer. You're going to want to see if there's a way to get your WAN IP from your router. What make/model is the router?
    – slm
    Dec 22, 2012 at 15:00
  • You have to query some kind of external service. Generally speaking, a computer has no idea what IP address other computers will see its connections as coming from. Dec 22, 2012 at 15:32
  • related: Windows command that returns external IP
    – jfs
    Feb 27, 2014 at 15:03

9 Answers 9


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     |------------
  +--------+ | +------+      +------+ |
                          +-| eth1 |------| eth0 |-+
                            +------+      +------+

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

You can also use the ip command.

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

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  |
  +--------+  +----+-----+----+  192.168.1.x  +----+ WAN IP     |
                           |eth1|     |eth0|               |eth0|------------+
                           +----+     +----+               +----+

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

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.

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

# something like this....

% wget -q -O - http://<username>:<password>@ | 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                                     |eth0|-----------+

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

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

% curl 'https://api.ipify.org?format=txt'

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

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

  • I have something like my system connects to a router and it is connected to the intrnet
    – user61954
    Dec 22, 2012 at 8:45
  • 5
    Since he stated he is not directly connected, then none of the eth* devices will have the WAN IP. Unless his router supports ssh, he has access, and ifconfig, this is not going to work. Not downvoting, but just so anyone trying to do this is aware.
    – AaronLS
    Dec 22, 2012 at 10:47
  • Re-read his question. He says he has LAN access, just not internet access, item #1. Item #2 says he's getting a dynamic IP from his ISP, I'm assuming that he's either got another system in b/w his LAN only connection and the internet OR he's got a home router/switch b/w him and the internet.
    – slm
    Dec 22, 2012 at 14:26
  • Downvoters, try leaving a comment, this answer is by far the most complete and yet people are down voting. @user61954 even said in his comments that he's behind a router so one of the methods I've described will work.
    – slm
    Dec 22, 2012 at 18:26
  • I upvoted. But the first and second way do not work, it only returns the localip address. Thanks! Dec 28, 2012 at 21:20

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:

# 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-`
if [ -f $TFILE ]; then
        OXTIP=`head -1 $TFILE`
        touch $TFILE
if [ "$EXTIP" != "$OXTIP" ]; then
        mv $TFILE "$TFILE~"
        echo "$EXTIP" > $TFILE
        lynx -source $DDNSURL > /dev/null
        echo "================================"
        echo "WAN_IP = $EXTIP"

There is a web service which will give your IP address when you curl it:

curl ifconfig.me
  • Hey, thanks, this help to me, just one question, What is the "ifconfig.me" I know the ifconfig function, but I'm not usually with this. Please can you give me a link to read about it or explain it. --Update-- didn't read the question. ups
    – Aby W
    Apr 15, 2016 at 5:36
  • ifconfig.me is a website that gives you your IP.
    – Ivy
    Apr 15, 2016 at 9:08
  • ifconfig.me doesn't support https, so it might be spoofed. Not a good idea to use it to check if your VPN is up. Dec 19, 2017 at 8:13

Most likely, the router must provide a way to get the external IP if no external service should or can be used (as some other answers already indicated). For external services, in most of the other answers, a web service is used. This answer over at Unix SE suggests to use DNS instead.

For example, using dig with OpenDNS as resolver:

dig +short myip.opendns.com @resolver1.opendns.com
  • +1, but at the end o fthe day its the same as using curl ident.me as we are relying os a specific service (in myip.opendns,com is special-cased by theat DNS server)
    – nhed
    Apr 16, 2015 at 18:40

Fetch info from your router via ssh or curl.

I use curl to ask dyndns for my public ip but such a command seems not applicable for you right?

curl http://checkip.dyndns.org 2> /dev/null| perl -pe 's,.*Address: (\d+\.\d+\.\d+\.\d+).*,$1,'


Unfortunately there is no way to detect mechanisms like NAT that do not involve contacting a remote service. NAT is by nature completely transparent to the user, besides broken services, and there is no standard protocol for NAT discovery.

But as far as external services are concerned, I have to suggest the fast and simple one I wrote, ident.me, which you can use for both IPv4 and IPv6; for its simplest form you can use curl ident.me and the full API is documented on http://api.ident.me/


If your router supports NAT PMP (Port Mapping Protocol), you may be able to retrieve your public IP using natpmp.

There's a command line interface to the linux natpmp library called natpmpc. On ubuntu, it's in the natpmp-utils package. My router doesn't appear to support natpmp, so I'm not certain that the natpmpc utility will return your public IP.


The linux command you are seeking is ifconfig assumming you are directly connected to the internet.

If not and accessing through proxies or special routing, the easiest way (as others indicate) is to login/access another host and check it from there.

Also, you might like to discuss this question with your friendly neighborhood systems administrator.

  • ifconfig is deprecated.
    – BatchyX
    Dec 22, 2012 at 9:52
  • "traceroute command, but it gives the local ip of the lan ,". So I guess there is a lan. Therefore there must be some kind of router/gateway/nat somewhere. Then ifconfig will not give public ip adress as op's comp is not directly connected to the internet.
    – user151547
    Dec 22, 2012 at 15:30
  • @BatchyX. What OS are you running? Every system I'm running on has ifconfig. Fedora, CentOS, Ubuntu, etc.
    – slm
    Dec 22, 2012 at 18:28
  • 2
    @batchx: And where do you get that information? I disagree with you on that one.
    – mdpc
    Dec 22, 2012 at 19:24
  • 1
    @sim: Because a command is present doesn't mean it is not deprecated. Just look at the ifconfig latest release date. Now look at the ìproute2 latest release. As for a source, see lartc.org/howto/lartc.iproute2.html#LARTC.IPROUTE2.WHY
    – BatchyX
    Dec 26, 2012 at 17:45
curl checkip.dyndns.org

sed that if you need to

  • 2
    curl icanhazip.com
    – paradroid
    Jan 25, 2013 at 21:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .