How do routers get their external IP address? Is it assigned by a central service? Is it decided by the ISP? I have googled this problem, but have only found how to change my IP address, which is not what I am after.
How do routers get their external IP address
ipnetworkingrouter
Related Solutions
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
Best Answer
Very much like how you have a local DHCP server running on your home router to serve IPv4 addresses on your LAN network (typically in the 192.168.1.x subnet), your ISP also has a DHCP server running somewhere that your router gets its WAN address from. The IP address range your ISP's DHCP server hands out are public IP addresses that it has purchased or been assigned.