How to Find the Public IP Address on a Linux System

IP addresses form the backbone of how the internet works. There are two basic types of IP addresses: Public and Private. Public IP addresses are used to connect to external networks, while private IPs are used to identify devices connected to your local network.

You can get your public IP address in Linux using several methods. In this article, we will be discussing in brief how you can use the host command, the dig command, and some external services to find your system’s public IP address.

Get Public IP Using the host Command

The host utility provides access to DNS-related information, including the public IP address of your Linux machine. When you run this command without any options, it will show the DNS server’s name, address, and the public IP of your system.

host myip.opendns.com resolver1.opendns.com

You can grep for the output line that contains the public IP and display it using the awk command.

host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'

Using the dig Command

The dig utility in Linux is another useful tool for retrieving DNS data associated with your computer. You can get the public IP address in Linux using the following dig command.

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

Related: What is a Static IP Address? Here’s Why You Don’t Need One

Using External Services

Many third-party services can show your public IP address. You can access most of these services using the curl or wget utility. The below commands leverage some such services.

curl https://ipinfo.io/ip ; echo
wget -qO- https://ipecho.net/plain ; echo

The aforementioned commands retrieve the public IP from the ipinfo.ip website.

curl https://ipecho.net/plain ; echo
wget -qO- https://ipecho.net/plain ; echo

These examples fetch your public IP address from the ipecho.net web service.

curl https://ifconfig.me ; echo
wget -qO- https://ifconfig.me ; echo

The ifconfig.me website supplies the IP information for the above commands. Many other websites on the internet are available that offer similar services.

Finding Your Public IP Address in Linux

You can easily find out your public IP address using one of the above methods. The key benefit of these approaches is that they display the correct IP address even if you’re behind a NAT router. So, anyone can use them for determining their public internet address.

The network packets transferred through the internet follow a specific model that forms the base for digital communication. The OSI model is responsible for data transfer and communication between two or more devices connected to either the same or different networks.

Source: makeuseof.com

Related posts

Connections #326: Today’s Answer and Clues (Thursday, May 2, 2024)

Why I Regret Buying an Apple Magic Keyboard for My Mac

How to Change the Font of a Paragraph Style in Google Docs