If I check with google, I can see my public IP. Is there something on the Ubuntu command-line which will yield me the same answer?
If I check with google, I can see my public IP. Is there something on the Ubuntu command-line which will yield me the same answer?
If you are not behind a router, you can find it out using
ifconfig
.If you are behind a router, then your computer will not know about the public IP address as the router does a network address translation. You could ask some website what your public IP address is using
curl
orwget
and extract the information you need from it:or shorter
For finding the external ip, you can either use external web-based services, or use system based methods. The easier one is to use the external service, also the
ifconfig
based solutions will work in your system only if you're not behind aNAT
. the two methods has been discussed below in detail.Finding external IP using external services
The easiest way is to use an external service via a commandline browser or download tool. Since
wget
is available by default in Ubuntu, we can use that.To find your ip, use-
Courtesy:
You could also use
lynx
(browser) orcurl
in place ofwget
with minor variations to the above command, to find your external ip.Using
curl
to find the ip:For a better formatted output use:
A faster (arguably the fastest) method using
dig
withOpenDNS
as resolver:Copied from: https://unix.stackexchange.com/a/81699/14497
Finding external IP without relying on external services
Type the following in your terminal:
In the above, replace
<interface_name>
with the name of your actual interface, e.g:eth0
,eth1
,pp0
, etc...Example Usage:
Type the following in your terminal (this gets the name and ip address of every network interface in your system):
Example Usage:
N.B: Outputs are indicative and not real.
Courtesy: https://www.if-not-true-then-false.com/2010/linux-get-ip-address/
UPDATE
LANG=c
has been added toifconfig
based usages, so that it always gives the english output, irrespective of locale setting.My favorite has always been :
simple, easy to type.
You will have to install curl first ;)
If ifconfig.me is down try icanhazip.com and or ipecho.net
or
icanhazip.com is my favorite.
You can request IPv4 explicitly:
If you don't have
curl
you can usewget
instead:Running my own service, designed to be simple and stupid, ident.me.
Its API and implementation are documented at https://api.ident.me/
Examples from the terminal (add
https://
for security at the expense of speed):You could use a DNS request instead of HTTP request to find out your public IP:
It uses
resolver1.opendns.com
dns server to resolve the magicalmyip.opendns.com
hostname to your ip address.Amazon AWS
Sample output:
Also works on browser: http://checkip.amazonaws.com
I like it because:
The one i'm using is :
Yes, you can have ip :-)
For this, STUN was invented. As a client you can send a request to a publicly available STUN server and have it give back the IP address it sees. Sort of the low level whatismyip.com as it uses no HTTP and no smartly crafted DNS servers but the blazingly fast STUN protocol.
Using
stunclient
If you have
stunclient
installed (apt-get install stuntman-client
on debian/ubuntu) you can simply do:where
A.B.C.D
is the IP address of your machine on the local net andW.X.Y.Z
is the IP address servers like websites see from the outside (and the one you are looking for). Usingsed
you can reduce the output above to only an IP address:However, your question was how to find it using the command line, which might exclude using a STUN client. So I wonder...
Using
bash
A STUN request can be handcrafted, sent to an external STUN server using
netcat
and be post-processed usingdd
,hexdump
andsed
like so:The echo defines a binary STUN request (0x0001 indicates Binding Request) having length 8 (0x0008) with cookie 0xc00cee and some pasted stuff from wireshark. Only the four bytes representing the external IP are taken from the answer, cleaned and printed.
Working, but not recommended for production use :-)
P.S. Many STUN servers are available as it is a core technology for SIP and WebRTC. Using one from Mozilla should be safe privacy-wise but you could also use another: STUN server list
Type in this exactly, press Enter where indicated:
telnet ipecho.net 80
EnterGET /plain HTTP/1.1
EnterHOST: ipecho.net
EnterBROWSER: web-kit
EnterEnter
This manually submits a HTTP request, which will return your IP at the bottom of a
HTTP/1.1 200 OK reply
Example output: