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

38

15

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.

user61954

Posted 2012-12-22T06:11:56.150

Reputation: 719

Related: How to get my external IP address (over NAT) from the Windows command-line?

– G-Man Says 'Reinstate Monica' – 2015-05-12T15:51:11.527

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 – 2012-12-22T14:27:46.463

@slm I am behind a router and ant to find out the ip assigned to the router by ISP – user61954 – 2012-12-22T14:57:04.780

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 – 2012-12-22T15:00:27.287

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 – 2012-12-22T15:32:03.990

related: Windows command that returns external IP

– jfs – 2014-02-27T15:03:29.860

Answers

28

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

slm

Posted 2012-12-22T06:11:56.150

Reputation: 7 449

I have something like my system connects to a router and it is connected to the intrnet – user61954 – 2012-12-22T08:45:44.163

5Since 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 – 2012-12-22T10:47:57.537

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 – 2012-12-22T14:26:11.517

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 – 2012-12-22T18:26:23.477

I upvoted. But the first and second way do not work, it only returns the localip address. Thanks! – Jared Burrows – 2012-12-28T21:20:45.553

Read the first paragraph of my answer. If you have 2 ethernet devices, and one of the 2 is connected to your WAN (cable modem and/or DSL modem) you can get the WAN IP address by simply running ifconfig against that ethernet device. – slm – 2012-12-28T21:28:45.953

12

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

curl ifconfig.me

Rory

Posted 2012-12-22T06:11:56.150

Reputation: 729

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 – 2016-04-15T05:36:38.910

ifconfig.me is a website that gives you your IP. – Rory – 2016-04-15T09:08:48.237

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. – Dan Dascalescu – 2017-12-19T08:13:32.713

9

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

daeme

Posted 2012-12-22T06:11:56.150

Reputation: 91

5

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

rhasti

Posted 2012-12-22T06:11:56.150

Reputation: 212

5

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

Percival Ulysses

Posted 2012-12-22T06:11:56.150

Reputation: 461

+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 – 2015-04-16T18:40:46.830

2

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/

Pierre Carrier

Posted 2012-12-22T06:11:56.150

Reputation: 896

1

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.

Johnny

Posted 2012-12-22T06:11:56.150

Reputation: 763

0

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.

mdpc

Posted 2012-12-22T06:11:56.150

Reputation: 4 176

ifconfig is deprecated. – BatchyX – 2012-12-22T09:52:38.050

"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. – None – 2012-12-22T15:30:34.503

@BatchyX. What OS are you running? Every system I'm running on has ifconfig. Fedora, CentOS, Ubuntu, etc. – slm – 2012-12-22T18:28:32.450

2@batchx: And where do you get that information? I disagree with you on that one. – mdpc – 2012-12-22T19:24:13.353

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 http://www.lartc.org/howto/lartc.iproute2.html#LARTC.IPROUTE2.WHY

– BatchyX – 2012-12-26T17:45:43.263

-1

curl checkip.dyndns.org

sed that if you need to

William Entriken

Posted 2012-12-22T06:11:56.150

Reputation: 2 014

2curl icanhazip.com – paradroid – 2013-01-25T21:33:54.293