How To Find Out Public IP Address From Command Line On Linux

Find Out Public IP Address

How do you find out the public IP from command line? Easy! There are numerous methods/apps available to do this in graphical user mode. Open your web browser and go to WhatIsMyIP.com website. There you can see your public IP. It is one of the most popular method to find out your public IP address, regardless of any Operating system. All you need is an Internet connection and a web browser. But, how do you find out public IP address from command line? That’s what we are going to see in this guide.

Find Out Public IP Address From Command Line On Linux

There many commands to find the public IP address. Here is few of them. I prefer to use dig and host commands.

If dig and host commands are not available in your system, install dnsutils package.

On Arch Linux and derivatives:

sudo pacman -S dnsutils

On RHEL, CentOS, Fedora:

sudo yum install dnsutils

Or,

sudo dnf install dnsutils

On Debian, Ubuntu and its derivatives:

sudo apt-get install dnsutils

Now, let us find the public IP.

Method 1:

Let us find out public IP with Google resolvers using dig command:

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's/"//g'

Alternatively, use this command to find out the public IP:

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com

Can I get the same info with opendns resolvers? Yes, you can!

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

Method 2: 

We can get the same info using host command.

host myip.opendns.com resolver1.opendns.com

Sample output:

Using domain server:
Name: resolver1.opendns.com
Address: 208.67.222.222#53
Aliases:

myip.opendns.com has address 122.178.32.233
Host myip.opendns.com not found: 3(NXDOMAIN)
Host myip.opendns.com not found: 3(NXDOMAIN)

Method 3:

You can use curl command to find the public IP of your system from a third-party websites. I do not recommend finding IP address using third-party sites due to security reasons. Be cautious!

Here is few examples.

curl ipinfo.io

Sample output:

{
 "ip": "122.178.32.233",
 "hostname": "abts-tn-dynamic-233.32.178.122.airtelbroadband.in",
 "city": "Coimbatore",
 "region": "Tamil Nadu",
 "country": "IN",
 "loc": "10.9925,76.9614",
 "org": "AS24560 Bharti Airtel Ltd., Telemedia Services",
 "postal": "641001"

curl is installed by default. If it is not installed by any chance, run the following command to install it.

On Arch Linux and derivatives:

sudo pacman -S curl

On RHEL, CentOS, Fedora:

sudo yum install curl

Or,

sudo dnf install curl

On Debian, Ubuntu and its derivatives:

sudo apt-get install curl

Here it is another method to find the public IP.

curl https://wtfismyip.com/text

Sample output:

122.178.32.233

The other useful methods to find out the public IP address are given below.

curl ifconfig.co
curl ident.me
curl icanhazip.com
curl ipecho.net/plain
curl ip.appspot.com

Working in command line is fun. You can do anything from command line in a matter of seconds. For those who are striving to learn GNU/Linux, I strongly suggest you to start from the command line.

That’s all for now. Let us meet again with an interesting article shortly. Until then, check our archives to learn more about Linux, Unix, Open Source, tips and tricks etc.

Happy weekend!

Cheers!!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • zen

    ‘curl icanhazip.com’ is the easiest one for me – response is just the ip – no mess, no fuss.

  • Really great tips here. Only knew about typing “what’s my ip” on google search or using iplocation.net or using opendns resolvers.