6

The easiest/simplest understanding of the web is

a. When you connect to your ISP, the ISP gives a dyanmic address (like a temporary telephone number) only for the duration of that connection, the next time you connect, you will again have a different dynamic IP Address.

b. You use the browser to to different sites which have static IP Address (like permanent numbers or/and permanent address of an establishment).

Now is there a way to get self's IP address instead of going to a web-service like whatismyipaddress.com.

The connection is as follows :-

ISP - Modem/Router - System

Edit - The Modem/Router is a D-Link DSL-2750U ADSL router/modem. http://www.dlink.co.in/products/?pid=452

I did see How to track my public IP address in a log file? but that also uses an external web-service, it would be better/nicer if we could do without going to an exernal URL/IP address for the same.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
shirish
  • 12,356
  • Querying the router won't work if your ISP is using large scale NAT for IPv4, as many ISPs have begun to do (and many more will in future). The only reliable way to get this information is via an external service. – Michael Hampton May 07 '16 at 03:56
  • @MichaelHampton Even that might not be too reliable - your outgoing connections might use another IPv4 every time, and inbound connections might be disabled altogether. – glglgl May 07 '16 at 09:26
  • @shirish That's not the understanding of the web, but of the Internet. The web kind of lies on top of it. – glglgl May 07 '16 at 09:27

4 Answers4

9

In addition to Tony´s answer, of querying OpenDNS, which I use in my scripts upon logging on to my servers to display both the local machine and remote public IP address:

echo `hostname` `hostname -i` `dig +short +time=1 myip.opendns.com @resolver1.opendns.com`

Google also offers a similar service.

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'

If you have a private IP address, behind a home or corporate router/infra-structure, or even if you are your own router, these services in the Internet will reveal the public IP address you are using to reach them, as it is what arrives to them doing the request.

Please do note that the above methods only work if the Linux machine in question has direct access to the Internet.

If your Linux server is your router, besides you being able to have a look at your current interfaces, you might also do:

hostname -i

As normally the public IP address is often the main/first interface. If not the first interface, you might also do:

$hostname -I
95.xx.xx.xxx 192.168.202.1 192.168.201.1 

Which shows you all the IP addresses of the machine interfaces.

Please read too:

How To Find My Public IP Address From Command Line On a Linux

Again, if the Linux server is the router, it might be interesting to place a script in /etc/dhcp/dhclient-exit-hooks.d to track and act on your IP changes, as I documented in this question:

Better method for acting on IP address change from the ISP?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
5

You can only do this if your modem or router provide a way to query that information. The IP address is assigned to the WAN / Internet facing interface on the router or modem, so you need to be able to query that, or connect to an Internet service which then tells you what it was.

You can use DNS to get a response, using this,

dig +short @resolver1.opendns.com myip.opendns.com

but it still goes to an external service to achieve it.

So, either query your router (if there's an interface you can use) or use an external service.

Of course, if the Linux machine is your router, then you just use ip or ipconfig to query the WAN interface.

EightBitTony
  • 21,373
4

1) You can use dig (domain information groper) command:

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'

or : dig TXT +short o-o.myaddr.l.google.com @ns1.google.com

2) You can try host command :

host myip.opendns.com resolver1.opendns.com

3) using 3rd party web-sites through the command curl or wget:

curl ipecho.net/plain ; echo

or

wget -qO- http://ipecho.net/plain ; echo

or

curl ipv4.icanhazip.com

4) By telnet:

telnet myip.gelma.net
GAD3R
  • 66,769
1

Your system doesn't know that ip, so you will to ask something external to the system. As your router does know, that could offer such a service, I don't know routers that do (but I've never investigated), but if routers with that functionality exists we would probably need to know the model of your router to tell you how to.

Various services to tell you your public ip exists and not all are web-based, naturally your router could implement this web-based as well as non web-based.

  • In fact you could curl and scrape any number of sites. Or run your own site on the cloud to echo this back, as per most of the above answers. – mckenzm May 07 '16 at 11:29