Linux Public IP address – Console

Published on December 9, 2021 at 7:25 am by LEW

Introduction

This post will cover several ways to get your public IP address form the Linux console.

There are a few reasons you might want to do this. But for me the number one reason is updating my Dynamic Domain Name Service (DDNS) to point to the correct IP address. Part of my home network is my personal cloud server. Since my server sits behind my router and firewall, and it does not have a GUI, I needed to be able to get my public IP address to update my DDNS profile.

It was easy to set up a CRON job that periodically checks my public IP address against the DDNS address. But first it was necessary to get my public IP address.

Basics of Obtaining Public IP Address

This is straight forward. There are many sites out on the internet that will tell you your public IP address. You can do a search in your favorite search engine, but here are a few to get you started; icanhazip.com, ipecho.net, ifconfig.co, ifconfig.me.

All these sites will return your public IP address. Some of these sites also provide additional information. The icanhazip.com site is the easiest to use, as it returns only the public IP address. Some of the other sites require filtering of results.

Try them in a regular browser to see how they work.

Pulling Public IP from Terminal

Acquiring your public IP address can be done from the terminal using one of several programs. The wget, curl, host, or dig programs are a few of these. In this example I will be using wget.

The following command works using wget and the icanhazip.com site.

wget -qO – icanhazip.com

In wget we use the -q option for quiet mode. This suppresses the internally generated text that wget produces when run. We also use the -O option to redirects output from a file. The “-” directs the output to the terminal for display.

Conclusion

In this post we have seen that it is fairly easy to get your public IP address via the command line. While there are many different ways to do this, we covered what is probably the simplest.

Being able to find your public IP address from the command line is useful for tasks like managing a remote server without a GUI.