Find your public IP address easily with CLI

Hello everybody,

In this post, I present you a small and useful bash script that can be used on Linux/Unix/OSX systems. By working in IT, I often need to know what is my public IP address. Everytime, I must use Google and write something like this: “whatsmyip“… I recently had the idea to write my own bash script. This script is very simple:


PUBLIC_IP=`wget -O - -q ; echo`
echo "My Public IP address is :" $PUBLIC_IP

Save this script in a file named, for example “myip” and move it into a directory presents in the PATH. After that, don’t forget to make it executable.

$ echo $PATH
$ /usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/bin
$ sudo cp myip /usr/local/sbin
$ sudo chmod +x /usr/local/sbin/myip

After that, you can just type “myip” in a Shell and you get your public IP address 🙂

$ myip
My Public IP address is :

This script is very simple and can be customised of course. We can imagine to check more than one web site to add redundancy and use another website that is able to return the IPv6 version of the public IP address if available. Don’t hesitate to post your improvements !

Bookmark the permalink.


  1. Instead of relying to an http web service, you can use a DNS resolver which is more reliable, like:
    PUBLIC_IP=$(dig +short

    I use the next two lines in a bash application (LiveWB) to get the country:
    EXTIPADDR=$(dig +short
    COUNTRY=$(whois $EXTIPADDR | awk -F’:[ \t]+’ ‘tolower($1) ~ /^country$/ { print tolower($2) }’)

  2. Hello Vangelis and thank you for you awesome answer ! I will try to include your solution on my script 🙂 I will post the update soon

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.