Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 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 '12 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 '12 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. –  David Schwartz Dec 22 '12 at 15:32
    
add comment

8 Answers

up vote 14 down vote accepted

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|grep inet|awk '{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 - http://cfaj.freeshell.org/ipaddr.cgi
54.234.1.33

# 2nd server
% wget -qO - icanhazip.com
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

share|improve this answer
    
I have something like my system connects to a router and it is connected to the intrnet –  user61954 Dec 22 '12 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 '12 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 '12 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 '12 at 18:26
    
I upvoted. But the first and second way do not work, it only returns the localip address. Thanks! –  Jared Burrows Dec 28 '12 at 21:20
show 1 more comment

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

curl ifconfig.me
share|improve this answer
add comment

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,'

share|improve this answer
add comment

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
share|improve this answer
add comment

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/

share|improve this answer
add comment
curl checkip.dyndns.org

sed that if you need to

share|improve this answer
    
curl icanhazip.com –  paradroid Jan 25 '13 at 21:33
add comment

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.

share|improve this answer
    
ifconfig is deprecated. –  BatchyX Dec 22 '12 at 9:52
    
"traceroute command, but it gives the local ip of the lan , 192.168.1.1". 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. –  befree42 Dec 22 '12 at 15:30
    
@BatchyX. What OS are you running? Every system I'm running on has ifconfig. Fedora, CentOS, Ubuntu, etc. –  slm Dec 22 '12 at 18:28
2  
@batchx: And where do you get that information? I disagree with you on that one. –  mdpc Dec 22 '12 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 '12 at 17:45
add comment

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.

share|improve this answer
add comment

protected by Community Jan 7 at 4:23

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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