6

I'm using Debian 8. How do I get my external IP address from a command line? I thought the below command would do the job ...

myuser@myserver:~ $ /sbin/ifconfig $1 | grep "inet\|inet6" | awk -F' ' '{print $2}' | awk '{print $1}'
addr:192.168.0.114
addr:
addr:127.0.0.1
addr:

but as you can see, it is only revealing the IP address of the machine in the LAN. I'm interested in knowing its IP for the whole world.

8

You mean whatever routable IP your dsl/cable modem/etc. router has?

You need to either query that device OR ask an outside server what IP it sees when you connect to it. The easiest way of doing that is to search google for "what is my ip" and like the calculation searches, it will tell you in the first search result. If you want to do it from the command line, you'll need to check the output of some script out there that will echo out the information. The dynamic dns service dyndns.org has one that you can use - try this command

wget http://checkip.dyndns.org -O -

You should get something like

HTTP request sent, awaiting response... 200 OK
Length: 105 [text/html]
Saving to: ‘STDOUT’

-                     0%[                    ]       0  --.-KB/s               <html><head><title>Current IP Check</title></head><body>Current IP Address: 192.168.1.199</body></html>
-                   100%[===================>]     105  --.-KB/s    in 0s      

2017-09-20 14:16:00 (15.4 MB/s) - written to stdout [105/105]

I've changed the IP in mine to a generic non-routable and bolded it for you.

If you want just the IP, you'll need to parse it out of there - quick and dirty, but it works for me. And I'm 100% sure there is a better safer way of doing it...

wget http://checkip.dyndns.org -O - | grep IP | cut -f 2- -d : | cut -f 1 -d \<

Which will give you just

192.168.1.199

12

This would return to you your public IP

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
  • 1
    Why the downvote? It's correct, and more efficient than any of the http suggestions, so +1 from me – roaima Sep 20 '17 at 21:17
  • upvote from me, and it was my answer that was accepted – ivanivan Sep 20 '17 at 21:19
  • dig is more faster than curl and wget – GAD3R Sep 20 '17 at 21:59
5

Alternatives (avoid parsing):

To get the IPv4

curl -4 icanhazip.com

To get the IPv6

curl -6 icanhazip.com

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