Putorius
Linux Quick Tips

How to Find Your Public IP Address on Linux Command Line

Conceptual Design of Public IP address

This Linux quick tip will show you many different way to get your public IP address from the command line using different tools. Since not all Linux distributions have the same set of packages (programs) installed, some of these example may or may not work on your system. For example, default Red Hat and CentOS installations do not have the dig tool installed.

All of these options will depend on external sources. We will try to use as many different sources as possible in the examples to ensure reliability.

Using the curl Command

Curl is a tool used to transfer data to and from a server using many different supported protocols. Here we will use the HTTPS protocol to pull a webpage and grep to extract our public IP address. Here are some examples of how to get your public IP address from the command line using curl.

ipaddr.pub

curl ipaddr.pub

The ipaddr.pub service can also provide additional information from the command line. You can find a complete list of options on the ipaddr.pub website.

ifconfig.io

curl https://ifconfig.io

WhatismyIP.com

curl https://whatsmyip.com/ -s | grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" -m1

Google.com

curl https://www.google.com/search?q=what+is+my+ip+address -s | grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" -m1

ipecho.net

curl -s http://ipecho.net/plain

akamai.com

curl -s http://whatismyip.akamai.com

Using the wget Command

The wget command is a command line utility for non-interactive download of files from the web. It supports most HTTP, HTTPS, and FTP as well as connecting through a HTTP Proxy server. Here are some examples of how to get your public IP address from the command line using wget.

ipaddr.pub

wget -qO- ipaddr.pub/cli

ipecho.net

wget -qO- http://ipecho.net/plain

icanhazip.com

wget -qO- icanhazip.com

Using the dig Command

The dig command is a command line tool for querying DNS servers. This utility is not always available. If you want to install dig, it is usually packaged in bind-utils on Red Hat based distros and dnsutils on Debian based distros. Here are some examples of how to get your public IP address from the command line using dig.

google.com

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

opendns.com

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

Using the host Command

The host command is a simple command line utility for performing DNS queries. Here are some examples of how to get your public IP address from the command line using the host command.

opendns.com

host myip.opendns.com resolver1.opendns.com | grep -m2 -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" | tail -n1

Using the nslookup Command

The nslookup command is tool that queries DNS Servers, much like dig. This command is available on many operating systems including Linux, UNIX and Windows. Here are some examples of how to get your public IP address from the command line using nslookup.

google.com

nslookup -query=TXT o-o.myaddr.l.google.com ns1.google.com | grep -m2 -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" | tail -n1

opendns.com

nslookup myip.opendns.com resolver1.opendns.com | grep -m2 -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" | tail -n1

Conclusion

There are many different ways to get your public IP address from the command line. Which you use will mostly depend on what is installed on your system. Our preferred method would be from a DNS server using the dig command, but as we stated, dig isn't always available.

References

The Curl Project Home Page

The Wget Project Home Page on Gnu.org

Exit mobile version