How To Get Public IP From Command Line

In this tutorial we’ll learn how to get Public IP address from Terminal or Command Line.

This will be useful to find public IP address of a cloud instance like EC2 instance, Lightsail instance, or DigitalOcean Droplets.

We can also use this method to find Public IP of a VPS or any bare metal server that have Public IP Address.

Get Public IP From DNS

If you’re familiar with dig, you might be wondering why using dig since we usually use dig to interrogate a DNS server. But we can get our public IP using dig.

Get Public IP Using dig to OpenDNS

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

As alternative, we can use nslookup to get public IP from OpenDNS DNS server

nslookup myip.opendns.com resolver1.opendns.com

Get Public IP Using dig to Google DNS

We can also get our Public IP by using dig to Google DNS

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

As alternative, we can use nslookup to get public IP from Google DNS

nslookup -type=txt o-o.myaddr.l.google.com ns1.google.com

You can replace ns1.google.com above with ns2.google.com, ns3.google.com or ns4.google.com.

Get Public IP Using dig to Akamai DNS

The next method is to get Public IP using dig to Akamai DNS

dig whoami.akamai.net. @ns1-1.akamaitech.net. +short

As alternative, we can use nslookup to get public IP from Akamai DNS.

nslookup whoami.akamai.net. ns1-1.akamaitech.net.

Using Telnet

You read it right, telnet, we can get our public IP using telnet

telnet telnetmyip.com

or we can also use nc to the same server. Please note that telnetmyip.com will give response / output in json so you have to parse the output to the the ip.
As alternative, you can also use nc to get the Public IP information from telnetmyip.com

nc telnetmyip.com

Using SSH

To get our public IP using SSH, we can use the command below

ssh sshmyip.com

You will be asked to verify server signature

ssh -o StrictHostKeyChecking=no sshmyip.com

Using Curl

There are multiple sites which can be used to check our Public IP Address.

icanhazip.com

To Check your public IP using icanhazip.com you can use the command below.

curl https://icanhazip.com

ifconfig.me

To Check your public IP using ifconfig.me you can use the command below.

curl https://ifconfig.me

ifconfig.co

To check your public IP using ifconfig.co you can use the command below.

curl https://ifconfig.co

checkip.amazonaws.com

To check your public IP using checkip.amazonaws.com you can use the command below.

curl https://checkip.amazonaws.com/

wtfismyip.com

To check your public IP using wtfismyip you can use the command below.

curl https://wtfismyip.com/text

Summary

In this tutorial we learn various methods on how to get public IP address from command line.