Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Similar to a lookup. It would obviously need to query a computer out there. Just wondering if anyone had a clever way to do it?

share|improve this question
See also – Krinkle Jul 4 '13 at 2:57

10 Answers 10

up vote 27 down vote accepted
share|improve this answer
That's a lot less work than my example :) – Alex Jul 17 '09 at 1:18
Wonderful simplicity. – Drew Stephens Jul 17 '09 at 6:06
dig +short

This only works if you are using OpenDNS as your dns server.

If you aren't, one of these should work:

dig +short @
dig +short @
dig +short @ @
share|improve this answer
Doesn't work on my MacOS 10.5, Ubuntu 8.04, or Ubuntu 9.04. – Drew Stephens Jul 17 '09 at 6:05
Interesting that it does on my Ubuntu 8.10. – Brad Gilbert Jul 17 '09 at 17:44
It is probably because I use opendns. – Brad Gilbert Jul 17 '09 at 17:45
+1 for using DNS to find out an IP (instead of http). You could also specify the dns server using its name e.g., (for readability) – J.F. Sebastian Feb 27 '14 at 14:38
lynx -dump | awk '/REMOTE_ADDR/{print $2}'
share|improve this answer

One must use OpenDNS' servers to use this... You can query a certain DNS server with dig like that:

dig +short @
share|improve this answer

STUN is the proper solution.

% stun -v
MappedAddress =
share|improve this answer

You can use curl to get the page from something like whatismyip and then get the pieces out. I used in this example...obviously the fields will differ with different services.

curl -s | grep LOOKUPADDRESS | awk '{ print $4 }'
share|improve this answer

i just run a traceroute to somewhere on the internet and look for the hop out of our local network.

perhaps there's a better way?

share|improve this answer


share|improve this answer

you can use the ifconfig command to list all interfaces and their associated IP address(es).

so, if you know your internet interface is ppp0, you can run

$ ifconfig ppp0
ppp0      Link encap:Point-to-Point Protocol
          inet addr:X.X.XX.X  P-t-P:Y.Y.Y.Y  Mask:
          RX packets:198986 errors:0 dropped:0 overruns:0 frame:0
          TX packets:122929 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:134195571 (127.9 MiB)  TX bytes:17101701 (16.3 MiB)

X.X.X.X will be your IP address. Y.Y.Y.Y is the IP address of the next hop.

you can then postprocess the output of ifconfig with grep/awk/sed/cut/perl/whatever to extract just the IP.

another alternative, if you have the iproute tools installed, is to use the ip command. e.g.

$ ip addr list ppp0
21842: ppp0:  mtu 1444 qdisc htb state UNKNOWN qlen 3
    inet X.X.X.X peer Y.Y.Y.Y/32 scope global ppp0

that's probably easier to read and certainly easier to parse:

$ ip addr list ppp0 | awk '/inet/ {print $2}'
share|improve this answer
- There is chance that any IP in ifconfig will not match real Internet IP (in case of biNAT) - In FreeBSD there is no iproute2 package, so ip command is not applicable – SaveTheRbtz Jul 18 '09 at 3:57
true, i don't use NAT so it never occurred to me just seemed "wrong" to me to use an external service for information you can query your own system about. even with NAT, i'd still prefer to figure out how to query the router doing the NAT (perhaps via an SNMP query) than to rely on an external service which may or may not be available when i need it. – cas Jul 18 '09 at 4:44
@Craig, Your approach is fine. However, its not an external IP unless you can reach it on that value from outside. You do have to rely on an external point for this answer. The idea is to find a reliable (at least more than your perimeter point) external point for the query. OpenDNS is a good point. – nik Jul 19 '09 at 17:17
@nik: the only likely case where your border router's IP isn't the external IP is when you're behind multiple layers of which case, the correct solution is to switch to a non-brain-damaged service ASAP. also, the problem with fetching a URL to find out your IP is that it only tells you the IP of the host that actually fetches it - which may be a proxy that strips Via headers. – cas Jul 20 '09 at 9:51

the simplest way is: curl

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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