Get the Public IP address from the Linux Command Line. Easy to Remember Services. Get your external IP address using the curl command.
Active11 months ago
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?
tshepang1,27722 gold badges1515 silver badges3535 bronze badges
kfmfe04kfmfe043,41444 gold badges1313 silver badges2020 bronze badges
22 Answers
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
or wget
and extract the information you need from it:or shorter
Michael KMichael K11.3k11 gold badge1515 silver badges2121 bronze badges
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 a NAT
. the two methods has been discussed below in detail.The easiest way is to use an external service via a commandline browser or download tool. Since
To find your ip, use-
wget
is available by default in Ubuntu, we can use that.To find your ip, use-
Courtesy:
You could also use
lynx
(browser) or curl
in place of wget
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
with OpenDNS
as resolver:
The other answers here all go over HTTP to a remote server. Some of them require parsing of the output, or rely on the User-Agent header to make the server respond in plain text. They also change quite frequently (go down, change their name, put up ads, might change output format etc.).
- The DNS response protocol is standardised (the format will stay compatible).
- Historically DNS services (OpenDNS, Google Public DNS, ..) tend to survive much longer and are more stable, scalable and generally looked after than whatever new hip whatismyip.com HTTP service is hot today.
- (for those geeks that care about micro-optimisation), this method should be inherently faster (be it only by a few micro seconds).
Using dig with OpenDNS as resolver:
Copied from:https://unix.stackexchange.com/a/81699/14497
- If you know your network interface name
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:
- If you don't know your network interface name
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.
10.3k55 gold badges3838 silver badges6161 bronze badges
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
PantherPanther83.4k1414 gold badges164164 silver badges265265 bronze badges
icanhazip.com is my favorite.
You can request IPv4 explicitly:
If you don't have
curl
you can use wget
instead:yprezyprez
I've found everything to be annoying and slow, so I wrote my own. It's simple and fast.
Its API is on http://api.ident.me/
Examples:
Pierre CarrierPierre Carrier
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 magical myip.opendns.com
hostname to your ip address.jfsjfs
Amazon AWS
Sample output:
I like it because:
- it returns just the plaintext IP, nothing else
- it is from a well known provider which is unlikely to go offline anytime soon
12.3k55 gold badges5555 silver badges5959 bronze badges
jflaflammejflaflamme
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:
lgarzo14.5k55 gold badges3434 silver badges3535 bronze badges
LiamLiam
Another fast one (might well be the fastest, relatively)
MohnishMohnish
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 and W.X.Y.Z
is the IP address servers like websites see from the outside (and the one you are looking for). Using sed
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 using dd
, hexdump
and sed
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
Victor KlosVictor Klos
I have a stupid service for this by telnet. Something like this:
Feel free to use it.
Dan7,46055 gold badges4747 silver badges7575 bronze badges
GelmaGelma
You can read a web page using only bash, without
curl
, wget
:jfsjfs
For those of us with login access to our routers, using a script to ask the router what its' WAN IP address is is the most efficient way to determine the external IP address. For instance the following python script prints out the external IP for my Medialink MWN-WAPR300N router:
Note that this is not very secure (as is the case with plaintext credentials & logging in to most routers), and is certainly not portable (needs to be changed for each router). It is however very fast and a perfectly reasonable solution on a physically secure home network.
To customize the script for another router, I recommend using the tamperdata addon in firefox to determine what HTTP requests to make.
cronburgcronburg
or for shorter output:
also
and probably:
N.B. If you don't mind to installing
WilfWilfcurl
, this as well:23k1111 gold badges7575 silver badges137137 bronze badges
Many home routers can be queried by UPnP:
Then, grep the ip address from the answer.
user372194
If you are using DD-WRT then this works for me:
or
- Where 192.168.1.1 is the Gateway/Router LAN IP Address of the DD-WRT router.
- The -s component means silent (i.e. don't show the curl progress information).
- Oh, I should mention that I use the above with 'DD-WRT v24-sp2 (01/04/15) std'.
CMPCMP
BinnyBinny
use
ip
!then look for the relevant adapter (not
lo
, and usually eth0
), and locate the ip address near inet
.Eliran MalkaEliran Malka
Maybe I am a little late, but inxi can do it fairly easy.
Install
inxi
Then run the following command
Example with my information blocked out using the
z
option for copy and paste to sites like this:Where it says
TerranceTerrance<filter>
is where your WAN IP, IPv4, MAC address etc will appear21.8k33 gold badges5353 silver badges105105 bronze badges
Simply issue a traceroute for any website or service..
Line 2 always seems to be my public IP address after it gets past my router gateway.
So, make a bash command.
And the output...
I don't think relying on PHP scripts and the sort is good practice.
woahguywoahguy
A command with no dependencies except 8.8.8.8 being a GOogle DNS:
RolfRolf
protected by Seth♦Feb 28 '14 at 4:30
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?