6 Linux commands to find Public IP addresses for your machine

Do you want to find the public IP address of your cloud server or any machine running with Linux distros such as Ubuntu, RHEL, AlmaLinux, Oracle, Debian, etc? Then the commands given in this article will surely help you a lot.

Finding the public IP address of your machine becomes necessary if you want to access some web application, set up remote access, do some troubleshooting work, or just for your knowledge want to know your machine’s virtual footprint. Here, we discuss how to use simple tools like curl and wget to network utilities like dig and nslookup to look for public IP addresses, each command offers a unique approach to revealing this essential detail.

Well, we can check our Private addresses simply by using the “ip a” command but for public addresses, here are the commands to follow:

1. curl

Curl is the default command utility available in almost all Linux out of the box and even if not then installation of cURL is quite easy. But do you know we can use it to fetch the details of our Public IP address attached to our Server? For example, we can use the CURL to get the details from “ifconfig.me“, here is the way to use it.

For Ipv4 public address:

curl -4 ifconfig.me -w "\n"

To get an Ipv6 public IP address:

curl -6 ifconfig.me -w "\n"

Note: “-w “\n” in the command is just to get the output in the next line, followed by the command prompt. If you want, you can use the command without it. Whereas -4 represents IPv4 and -6 for IPv6 addresses.

In the given screenshot, first, we identified our AWS Cloud server running Ubuntu Linux’s private IP address and then used the “cURL” command to get Public IP addresses – Ipv4 and Ipv6.

use curl to check Linux public IP address

2. wget

If you don’t want to use the cURL and have “wget” on your system already then that can be used similarly to retrieve our public IP address with the help of ipecho.net service and print it to the terminal.

wget -qO- http://ipecho.net/plain ; echo
use Wget to get public Ipaddress

3. dig

The dig is another popular command to find the public address of a particular domain name or simply your own. In the given command the “dig” will ask for the IP address of the domain myip.opendns.com from an OpenDNS resolver i.e “resolver1.opendns.com” which in return sends back the public IP address of the user who requested instead of myip.opendns.com because it is programmed in such as way.

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

4. nslookup

Instead of the “dig” command, we can use nslookup which works similarly to fetch the public IP address from the OpenDNS resolver. nslookup performs a DNS lookup for the myip.opendns.com domain using the OpenDNS resolver.

To get IPV4 using nslookup:

nslookup -type=A myip.opendns.com resolver1.opendns.com

For IPV6:

nslookup -type=AAA myip.opendns.com resolver1.opendns.com
nslookup command to find public address

5. host

In this list of commands to find the Public IP address of your server or desktop another name is “host” like nslookup you can use it as well. Let’s see how to use it.

Ipv4 will be provided:

host -4 myip.opendns.com resolver1.opendns.com

IPv6 will be in the output:

host -6 myip.opendns.com resolver1.opendns.com
host command to get IP address public sever

6. ipconfig.io or ipinfo.io

Again if the first command given in this list that uses cURL is not working for you then you can go for the given ones:

curl ipconfig.io
curl ipinfo.io/ip

You can add the -4 option in the above-given commands to get the IPv4 address.

So, these were a few commands we can use on our Linux server or desktop to quickly identify and note the Public IP address to perform some networking tasks or troubleshooting.

Other Articles:

Leave a Comment

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