If you’re using a Linux machine and want to find your public IP address without visiting a website, you can do so easily from the command line. In this tutorial, we’ll go through the steps to finding your public IP address using a few different methods.

Advertisement

Finding Your Public IP Address

Method 1: Using the dig Command

The dig command is used for querying DNS servers, but it can also be used to find your public IP address. Open a terminal and type the following command:

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

You should see your public IP address printed on the screen.

Method 2: Using the curl Command

The curl command is used for transferring data from or to a server. It can also be used to find your public IP address. Open a terminal and type the following command:

curl ipecho.net/plain; echo
curl icanhazip.com
curl ifconfig.me

You should see your public IP address printed on the screen.

Method 3: Using the wget Command

The wget command is used for downloading files from the web. It can also be used to find your public IP address. Open a terminal and type the following command:

wget -qO- ifconfig.me

You should see your public IP address printed on the screen.

Getting Public IP in Shell Script

We can simply use the following commands in our shell script to get our computer’s public IP and store them in a variable to use anywhere in a shell script.

Conclusion

These are just a few of the many methods available for finding your public IP address from the Linux command line. Depending on your system’s configuration and your network setup, some of these methods may work better than others. Experiment with different methods to find the one that works best for you.

Share.

8 Comments

  1. Thank you Rahul, and everyone who added sites. I put seven sites in a bash function. The last one gives more info. The first few times I ran this, opendns reported a different IP. I have helped someone whose public IP was diverted, and her logins were hacked in realtime so it was bad to log in anywhere until the method was discoverred. https://pastebin.com/DiPUdqPJ

    myip() {
    ( for site in ipaddr.pub/cli ipecho.net/plain icanhazip.com ifconfig.me \
    ipconfig.in/ip diagnostic.opendns.com/myip
    do echo “$site ”
    wget -qO- $site
    echo
    done ) | sed -n -E ‘/^$/d;H;${g;s/^[\n]+//;s/( )\n/ /g;p;}’
    wget -qO- ipinfo.io | sed ‘1s/.*/ipinfo.io:/;$d;’
    }

  2. Thanks, great your are providing 5 nice urls like this..

    here a use in Ruby

    require ‘faraday’
    Faraday.get(‘http://icanhazip.com/’).body.chomp

Exit mobile version