Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

I need to find my external IP address from a bash script. At the moment I use this function:

myip () { 
    lwp-request -o text checkip.dyndns.org | awk '{ print $NF }'

But it depends on perl-libwww, perl-html-format, perl-html-tree installed. What other ways can I get my external IP?

share|improve this question
What do you mean by the external IP? The two answers so far use HTTP. The response may end up being the IP of your ISP's proxy server. (Which may be what you want.) – billpg Oct 14 '11 at 17:21
@billpg: I mean the IP of the NAT router – eugene y Oct 14 '11 at 20:09
Then you'll need a what-is-my-IP web service that uses HTTPS. Alas, I don't know of any. – billpg Oct 14 '11 at 22:22
@billpg ipcheckit.com – Gilles Oct 14 '11 at 22:48
checkip.amazonaws.com use this from well known provider – arulraj.net Mar 9 at 18:29

21 Answers 21

up vote 190 down vote accepted

I'd recommend getting it directly from a DNS server.

Most of the 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.).

  1. The DNS response protocol is standardised (the format will stay compatible).
  2. Historically DNS services (OpenDNS, Google Public DNS, ..) tend to survive much longer and are more stable, more scalable, and generally more looked-after than whatever new hip whatismyip.com HTTP service is hot today.
  3. (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:

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

Perhaps alias it in your bashrc so it's easy to remember

alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'

Responds with a plain ip address:

$ wanip
share|improve this answer
you are right it's too fast.. – Rahul Patil Jul 9 '13 at 16:24
@Krinkle that's an excellent answer. Is there an equivalent for myip.opendns.com in Googles Public DNS ? – Kannan Mohan Oct 26 '14 at 5:11
Alternative is nslookup . ifcfg.me – Eun Dec 17 '14 at 14:03
I found this to be ~19x faster than curl http://canhazip.com. See askubuntu.com/a/427092/2273 – Adam Monsen Mar 10 '15 at 21:09
Please note that in some cases the routers that provide NAT will also translate DNS responses (you can take a look at wiki.nil.com/Network_address_translation_of_DNS_responses); in this case you should fallback to some answer that recommends other approach than DNS. – Razvan Stefanescu Oct 30 '15 at 16:20
 curl -s http://whatismijnip.nl |cut -d " " -f 5

Replaced site with dutch working one.

share|improve this answer
+1 I knew about whatismyip.com, but not whatismyip.org, that's awesome. – Julian Oct 14 '11 at 15:15
Never knew about that one! Great site! – IDWMaster Oct 14 '11 at 22:48
@MaciekSawicki Is the -s option really necessary in this case? I tried with/without it in my fedora 15 - bash 4.2.10(1) and it worked in both ways. – ztank1013 Oct 15 '11 at 14:05
Doesn't seem to work anymore via CLI, but going to the webpage from a web browser works. I'm using openSUSE 12.1 x64. – SaultDon Aug 16 '12 at 16:13
whatismyip.com has removed the free service to check external IP. So, I'm afraid this is no longer correct. icanhazip.com still works. – daSong Jan 17 '13 at 10:20

Since whatsmyip.org and ifconfig.me have already been mentioned:

curl -s icanhazip.com
share|improve this answer
Another one: ip.appspot.com or ip.appspot.com – Lekensteyn Nov 9 '11 at 8:31
These two support IPv6. – Josh Lee Oct 8 '12 at 17:31
@JoshLee, note that icanhazip also supports ipv6. Also, a note to the OP, all these curl solutions (though valid) still depend on an external library (libcurl). – HalosGhost Jul 29 '14 at 2:51
Another one: ifcfg.me – Eun Dec 17 '14 at 14:04

NOTE: This is about external IP address (the one that the servers on the Internet see when you connect to them) - if you want internal IP address (the one that your own computer is using for connections, which may be different) see this answer.

TL;DR - Fastest methods in 2015

The fastest method using DNS:

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

or using externalip:

externalip dns

The fastest using HTTP:

curl -s http://whatismyip.akamai.com/

or using externalip:

externalip http

The fastest using HTTPS with a valid cert:

curl -s https://4.ifcfg.me/

or using externalip:

externalip https

Using telnet:

With nc command:

nc 4.ifcfg.me 23 | grep IPv4 | cut -d' ' -f4

or using externalip:

externalip telnet

With telnet command:

telnet 4.ifcfg.me 2>&1 | grep IPv4 | cut -d' ' -f4

Using FTP:

echo close | ftp 4.ifcfg.me | awk '{print $4; exit}'

or using externalip:

externalip ftp

All of the above can be run using my externalip script as:

externalip dns
externalip http
externalip https
externalip telnet
externalip ftp

Now a long story...

There are a lot of options of different servers providing the external IP especially via HTTP posted here or elsewhere.

I made a benchmark to see if any of them are better than the others and I was surprised by the results. E.g. one of the most widely recommended ifconfig.me was almost always the slowest for me, sometimes taking many seconds to respond. Many don't work over HTTPS, or do work but have invalid certificates. Some have very inconsistent response times.



This is the source of my externalip-benchmark script that I used:

You can run it yourself to see which services mentioned here are worth using:

wget https://raw.githubusercontent.com/rsp/scripts/master/externalip-benchmark
chmod a+x externalip-benchmark

My results that I got on 2015-04-03 from Warsaw - the addresses have been changed to protect the innocent:

Best http response times:

0.086s http://ip.tyk.nu/ - answer=''
0.089s http://whatismyip.akamai.com/ - answer=''
0.091s http://tnx.nl/ip - answer=''
0.117s http://ifcfg.me/ - answer=''
0.156s http://l2.io/ip - answer=''
0.317s http://ip.appspot.com/ - answer=''
0.336s http://ident.me/ - answer=''
0.338s http://ipof.in/txt - answer=''
0.347s http://icanhazip.com/ - answer=''
0.496s http://curlmyip.com/ - answer=''
0.527s http://wgetip.com/ - answer=''
0.548s http://curlmyip.com/ - answer=''
0.665s http://bot.whatismyipaddress.com/ - answer=''
0.665s http://eth0.me/ - answer=''
1.041s http://ifconfig.me/ - answer=''
1.049s http://corz.org/ip - answer=''
1.598s http://ipecho.net/plain - answer=''

Best https response times:

0.028s https://curlmyip.com/ - answer=''
0.028s https://curlmyip.com/ - answer=''
0.029s https://l2.io/ip - answer=''
0.029s https://tnx.nl/ip - answer=''
0.072s https://whatismyip.akamai.com/ - answer=''
0.113s https://ipecho.net/plain - answer=''
0.117s https://ident.me/ - answer=''
0.207s https://ip.tyk.nu/ - answer=''
0.214s https://ipof.in/txt - answer=''
0.259s https://ifcfg.me/ - answer=''
0.289s https://corz.org/ip - answer=''
0.436s https://ip.appspot.com/ - answer=''
0.448s https://bot.whatismyipaddress.com/ - answer=''
0.454s https://eth0.me/ - answer=''
0.673s https://icanhazip.com/ - answer=''
5.255s https://ifconfig.me/ - answer=''
10.000s https://wgetip.com/ - answer=''

(Note: there are some fast responses with empty content - those are invalid.)

Best average ping times:

10.210 //whatismyip.akamai.com/
36.820 //tnx.nl/ip
37.169 //ip.tyk.nu/
39.412 //ipof.in/txt
40.967 //ident.me/
41.257 //ipecho.net/plain
43.918 //ifcfg.me/
45.720 //l2.io/ip
64.749 //ip.appspot.com/
123.412 //corz.org/ip
134.245 //wgetip.com/
157.997 //icanhazip.com/
161.613 //curlmyip.com/
162.100 //curlmyip.com/
268.734 //ifconfig.me/
999999 //bot.whatismyipaddress.com/
999999 //eth0.me/

Here are the results that I got on 2015-04-03 from Amsterdam:

Best http response times:

0.021s http://ipecho.net/plain - answer=''
0.027s http://tnx.nl/ip - answer=''
0.035s http://whatismyip.akamai.com/ - answer=''
0.039s http://ifcfg.me/ - answer=''
0.045s http://l2.io/ip - answer=''
0.142s http://ident.me/ - answer=''
0.144s http://ipof.in/txt - answer=''
0.150s http://ip.appspot.com/ - answer=''
0.150s http://ip.tyk.nu/ - answer=''
0.170s http://icanhazip.com/ - answer=''
0.190s http://eth0.me/ - answer=''
0.191s http://wgetip.com/ - answer=''
0.301s http://curlmyip.com/ - answer=''
0.330s http://bot.whatismyipaddress.com/ - answer=''
0.343s http://curlmyip.com/ - answer=''
0.485s http://corz.org/ip - answer=''
3.549s http://ifconfig.me/ - answer=''

Best https response times:

0.004s https://curlmyip.com/ - answer=''
0.012s https://curlmyip.com/ - answer=''
0.012s https://tnx.nl/ip - answer=''
0.016s https://ipecho.net/plain - answer=''
0.071s https://whatismyip.akamai.com/ - answer=''
0.096s https://ifcfg.me/ - answer=''
0.097s https://ident.me/ - answer=''
0.187s https://corz.org/ip - answer=''
0.187s https://ip.appspot.com/ - answer=''
0.189s https://ip.tyk.nu/ - answer=''
0.195s https://eth0.me/ - answer=''
0.253s https://l2.io/ip - answer=''
0.300s https://ipof.in/txt - answer=''
0.324s https://bot.whatismyipaddress.com/ - answer=''
0.512s https://icanhazip.com/ - answer=''
1.272s https://ifconfig.me/ - answer=''
10.002s https://wgetip.com/ - answer=''

Best average ping times:

1.020 //ipecho.net/plain
1.087 //whatismyip.akamai.com/
5.011 //ip.appspot.com/
6.942 //ident.me/
7.017 //ipof.in/txt
8.209 //tnx.nl/ip
11.343 //ip.tyk.nu/
12.647 //ifcfg.me/
13.828 //l2.io/ip
81.642 //icanhazip.com/
85.447 //wgetip.com/
91.473 //corz.org/ip
102.569 //curlmyip.com/
102.627 //curlmyip.com/
247.052 //ifconfig.me/
999999 //bot.whatismyipaddress.com/
999999 //eth0.me/

(The 999999 pings mean 100% packet loss.)


For a comparison here are times that other methods take - tested on 2015-06-16 from Warsaw and Amsterdam.


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

usually takes (real wall clock time) about:

  • 0.035s from Warsaw
  • 0.015s from Amsterdam

There are actually four resolvers that can be used this way:

  • resolver1.opendns.com
  • resolver2.opendns.com
  • resolver3.opendns.com
  • resolver4.opendns.com

They all give the same response times in Warsaw and Amsterdam but this may not be the case in other locations.

Using - the IP of resolver1.opendns.com instead of its domain name is faster:

  • 0.023s from Warsaw
  • 0.009s from Amsterdam

but may not work in the future if the IP ever changes (though it may be unlikely for a well known DNS resolver - maybe I should use the IP in my externalip script - please comment).


Telnet with nc or telnet command (see above) usually takes:

  • 0.103s from Warsaw
  • 0.035s from Amsterdam

(There is no noticeable difference between nc and telnet commands.)


  • 0.104s from Warsaw
  • 0.036s from Amsterdam

Domain names

All of the methods will be faster (especially when run for the first time) when IP addresses will be used instead of the domain names of the given services (except with HTTP that can use host-based virtual servers and not work with bare IP - not tested) but will stop working when the services change the IP address so it may be faster but less future-proof.


If you see some interesting results from your location, or if you think that some other hosts should be recommended instead of those that I've chosen, please post a comment. If there is any important service missing, please comment or post an issue on GitHub. I'd like to keep this post updated with a current choice of best-performing services.

share|improve this answer
Did you benchmark the myip.opendns.com DNS method as in Krinkle's answer? Currently it seems that it's winner-by-default due to being unaware of any other providers for this method, but it would still be useful to compare with the other methods. – James Haigh Apr 29 '15 at 8:16
@JamesHaigh Thanks for the suggestion. I added DNS and other methods (telnet, ftp) to the answer. The fastest method seems to be DNS using directly the IP address (instead of the domain name) of the resolver. – rsp Jun 16 '15 at 15:13
You should add/test canhazip.com / canhazip.com as well (HTTP and HTTPS). – xxdesmus Aug 22 '15 at 16:15
You can add myip.addr.space to your list. I built this myself as I wasn't particularly satisfied with any of the others I had seen at the time. – Michael Hampton May 7 at 4:07

You can use ifconfig.me as alternative to whatismyip.org.

curl -s http://ifconfig.me

Also ifconfig.me has some additional functional. To find out what else information you can receive visit the website.

share|improve this answer
It is extremely slow. I often get response times over 30 seconds, sometimes even over a minute! Sometimes it's half a second (which is still a lot), and then it's 15 seconds or so. This is tested from different locations. See my answer for more info and benchmarks. – rsp Apr 3 '15 at 4:12
wget -O - -q http://whatismyip.org/
share|improve this answer
I was not able to get my IP like this, just gives it as:<img src='ipimg.php'/> – Yuugian Apr 4 '13 at 13:38
Got it working with some decent lot of regex magic, but it wasn't easy. Should you insist on using this service, be sure to wrap that line into a script: $ curl -s http://whatismyip.org | grep -o '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}' – syntaxerror Feb 1 at 2:40

I prefer to use curlmyip.com It's as simple as:

curl curlmyip.com

It's short and simple to remember.

share|improve this answer
For use in bash —the intent of the OP—, you cannot do without the -s option mentioned in other answers. – Serge Stroobandt Jul 20 '14 at 11:53
@SergeStroobandt Yes you can. All the -s switch does is run it in silent mode, ie. error messages won't be displayed. So it comes down to how he wants his script to handle errors. The command itself will return an IP address as reliably as using the -s. – Garrett Fogerlie Jul 24 '14 at 22:48
So far, this site was the most reliable one in multiple tests. – Amos Shapira Dec 12 '14 at 9:05
curl ident.me


curl ifconfig.me


curl tnx.nl/ip


curl ipecho.net/plain


curl ip.appspot.com


curl whatismyip.akamai.com


curl icanhazip.com


curl wgetip.com


curl ip.tyk.nu


curl curlmyip.com


curl corz.org/ip


curl bot.whatismyipaddress.com


share|improve this answer
netcat icanhazip.com 80 <<< $'GET / HTTP/1.1\nHost: icanhazip.com\n\n' | tail -n1
share|improve this answer
Blank output here, although the site works. Any idea why? I am behind a proxy, if that's relevant, but wget icanhazip.com works. – l0b0 Apr 4 '13 at 11:23
@l0b0 Try to omit the | tail -n1 part and see what you get from the proxy – eugene y Apr 4 '13 at 13:18
Nothing, just exit code 1. Ditto for netcat icanhazip.com 80. Looks like it ignores $http_proxy and friends, because specifying the proxy and port with -x just resulted in a hanging process. – l0b0 Apr 4 '13 at 14:05
@l0b0: netcat proxy 3128 <<<$'GET http://icanhazip.com/ HTTP/1.0\n\n' | tail -n 1 for a basic HTTP proxy (assuming it's running on port 3128). You will obviously get the IP address of the proxy back, though. – tripleee Jul 4 '13 at 4:07
HTTP 1.0 does not have the Host: request header - name-based virtual hosting was one of the major improvements in HTTP 1.1. Either change the request to specify HTTP/1.1, or remove the Host header (I recommend the former). – Michael Kjörling Jul 4 '13 at 7:39

If you want to use HTTPS to avoid some potential pitfalls:

_result=$(wget -qO- https://ipcheckit.com/)
_result="${_result##*Your IP address is<br><b>}"
printf '%s\n' "${_result%%</b></p>*}"
share|improve this answer
Perhaps you could elaborate on what the potential pitfalls are that you are avoiding here? – Caleb Oct 17 '12 at 6:27
The domain ipcheckit.com apparently is for sale and not hosts the IP address displaying service anymore. – manatwork Jan 17 '13 at 11:59
ah, the pitfalls of writeonly scripts :) – Ярослав Рахматуллин Jul 4 '13 at 14:07
@Chris Down the technique is still valid, you just need another SSL capable site to use it with. – Caleb Nov 25 '13 at 21:09

Here is another alternative that depends on hosts who's business resolves around managing dynamic IP rather that "public service" sites that may go away or change format.

  1. Register your server at one of the many free dynamic dns services (e.g. no-ip.com) This will give you a DNS entry like xxx.no-ip.org.
  2. Install the service's dynamic update tool (reports IP changes to service).

To get the IP address in a script, just do:

external_ip=`dig +short xxx.no-ip.org`

Great for use in cron job to check if dynamic IP has changed and some configuration entries need to be changed.

share|improve this answer

This always works for me, I use it in my conky to get my IP address.

wget -q -O - checkip.dyndns.org | sed -e 's/[^[:digit:]\|.]//g'
share|improve this answer
+1 Albeit looking so clumsy, this approach has been a life-saver for me too a few times. Because Internet is not WWW. You can have internet access, but you may (commonly in server rooms) be doomed to a non-GUI console, and in this case it's important to remember by heart one of the checkIP service URLs. And as this one is pretty common, it's only seemingly compilcated, as you will be more likely to remember the DynDNS one than the one of amazonaws. That is, if you have NO way to google for it. (not even lynx). – syntaxerror Feb 1 at 2:24

Since I don't rely on the connection or on the service, I use the following code, which tries to get the IP using different services (feel free to add more):

# Get my ip address and put in a file
declare -a arr=("ipecho.net/plain" "ident.me" "tnx.nl/ip" "ip.appspot.com" "https://shtuff.it/myip/short/")
IP=$(curl -s --retry 3 --retry-delay 10 ipecho.net/plain)

while [ -z "$IP" ] # If no IP found yet, keep trying!
    sleep 30
    IP=$(curl -s --retry 3 --retry-delay 10 ${arr[$((  RANDOM % ${#arr[@]}  ))]})  

echo -n "$IP" >  /root/clientIP.txt #puts ip address in clientIP.txt
echo "Our address is $IP" 

To add more robustness (e.g. if one of the services changes their format), you could check that $IP is a valid IP using the following function:

# Verify that the parameter passed is an IP Address:
# http://zindilis.com/blog/2013/05/10/bash-check-that-string-is-ip.html
# @Author: Marios Zindilis
# @License: Creative Commons Attribution-ShareAlike 4.0 International License.
# @Date: 2013-05-10
function is_IP() {
if [ `echo $1 | grep -o '\.' | wc -l` -ne 3 ]; then
        echo "Parameter '$1' does not look like an IP Address (does not contain 3 dots).";
        exit 1;
elif [ `echo $1 | tr '.' ' ' | wc -w` -ne 4 ]; then
        echo "Parameter '$1' does not look like an IP Address (does not contain 4 octets).";
        exit 1;
        for OCTET in `echo $1 | tr '.' ' '`; do
                if ! [[ $OCTET =~ ^[0-9]+$ ]]; then
                        echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' is not numeric).";
                        exit 1;
                elif [[ $OCTET -lt 0 || $OCTET -gt 255 ]]; then
                        echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' in not in range 0-255).";
                        exit 1;

return 0;
share|improve this answer

ifcfg.me supports:

curl ifcfg.me
nslookup . ifcfg.me
telnet ifcfg.me
ftp ifcfg.me
finger @ifcfg.me

IPv4 and IPv6, even more stuff with curl: ifcfg.me/?

share|improve this answer
How does the nslookup method relate to the dig method in Krinkle's answer? They both use DNS, right? So can I pass some option to dig to make it retrieve the same DNS record that nslookup is doing here? – James Haigh Apr 29 '15 at 8:09
Yeah you could use dig +short . @ifcfg.me if you wish – Eun May 1 '15 at 18:22

I run a cloud service for my family and I made this quick script I run in a cron every morning at 5 because I am cheap an will not buy a static IP.

It grabs the public IP, and emails it to my users. Made it email in hyperlink format so my Mom does not have to type the ports or anything. Maybe someone else can use it to.

ipvariable=$(wget http://ipecho.net/plain -O - -q);

echo "Today the location is http://$ipvariable:123456/foldertheyreach" | mail -s   "From your friendly cloud service provider" user1@someemail.com, user2@ect.com
share|improve this answer

This will show the current ip address in a popup window:

zenity --info --text "$(curl -s icanhazip.com)"
share|improve this answer

I have setup a service that returns IP address as JSON / XML or plain text. You can find them here


Same URL with /json and /xml will give you other formats as well

If you want HTTPS you can use the same URLs with https prefix. The advantage being that even if you are on a Wifi you will get the public address.

So a simple alias myip="curl https://ipof.in/txt" will get your IP

share|improve this answer

Alternatively you could use STUN which was invented to answer this question in an automated way and is used extensively in internet communications e.g. by SIP and WebRTC.

Using a stunclient (on debian/ubuntu do apt-get install stuntman-client) simply do:

$ stunclient stun.services.mozilla.com
Binding test: success
Local address: A.B.C.D:42541
Mapped address: W.X.Y.Z:42541

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:

stunclient stun.services.mozilla.com |
    sed -ne "s/^Mapped address: \(.*\):.*$/\1/p"

For an alternative STUN lookup using nothing but basic command line tools see my answer on AskUbuntu (intended as a fun exercise, not for production use).

share|improve this answer
curl http://checkip.amazonaws.com


  • it returns just the plaintext IP, nothing else
  • it is from a well known provider which is unlikely to go offline anytime soon
share|improve this answer

Use curl to hit shtuff.it's ip service

curl -s http://shtuff.it/myip/short
share|improve this answer

The w3m Plaintext-Browser is great for the bash. You can use grep and tail to shorten the reply as follows:

w3m -no-cookie -dump "http://www.whatismyip.com/" | grep -A 1 -m 1 "Your IP:" | tail -n 1
share|improve this answer

protected by jasonwryan Jan 10 '15 at 19:07

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.

Would you like to answer one of these unanswered questions instead?

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