Linux Commands

How to Find the Public IP Address from Command Line in Linux

If you are a Linux user, you may need to find your public IP address from the command line for various reasons, such as troubleshooting network issues or setting up a web server. In this article, we will guide you through the process of finding your public IP address using different commands in Linux.

Content of this article includes:

Finding Public IP Address Using dig Command

Finding Public IP Address Using Host Command

How to Find Public IP Address Using Curl Command

Difference Between Public and Private IP Address

The Internet Protocol address is an identifier address given to devices inside the network. There are two types of IP addresses:

Public: The Internet Service Provider assigns the public IP to the device that is directly connected to the internet. It can identify the device on the internet. It can be checked from anywhere over the internet.

Private: A private IP address is given by the network administrator to devices on a local network. The Private IP address is given to networks such as a home or offices. It is used to identify devices within the network. It cannot be accessed over the internet as it is Private.

Among both addresses the accessibility is the main difference which describes whether they can be accessed or not over the internet.

Finding Public IP Address Using dig Command

The dig command gives us a Public IP address using DNS servers. To find public IP using dig follow below steps:

Open the terminal in your Linux machine. Type the following command:

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

 
You will see a public IP address displayed on the terminal:

Finding Public IP Address Using host Command

The host command is another useful tool for getting information such as IP addresses and domain names from a DNS server. You can also use the host command to get public IP.

Run below host command to get IP:

host myip.opendns.com resolver1.opendns.com

 
Your public IP address will display on the terminal.

How to Find Public IP Address Using curl Command

    • IP Address from Ifconfig Using curl Command
    • IP Address from ipinfo Using curl Command
    • IP Address from ipify Using curl Command
    • IP Address from checkip.dyndns.org Using curl Command
    • IP Address from ident.me Using curl Command
    • IP Address from ipecho.net Using curl Command

1: IP Address from ifconfig Using curl Command

The curl command can exchange data from a server. One of its many uses is finding your public IP address.

To find public IP type the following command on shell:

curl ifconfig.me

 
This command will display Public IP on the terminal.

2: IP Address from ipinfo Using curl Command

The command curl ipinfo.io/ip also uses the curl utility to retrieve the public IP address of the system it is being executed on.

When the curl command is executed, it makes a GET request to the ipinfo.io API, which returns the public IP address.

curl ipinfo.io/ip

 
Here’s how this command works:

3: IP Address from ipify Using curl Command

The command curl api.ipify.org can also give us our public IP address using the curl utility and ipify service API. Here’s how this command works:

curl api.ipify.org

 

4: IP Address from checkip.dyndns.org Using curl Command

This command sends a request to the website checkip.dyndns.org using the curl tool. The website returns the public IP.

curl checkip.dyndns.org

 

5: IP Address from ident.me Using curl Command

This command sends a request to the website ident.me using the curl tool. The website gives Public IP of the device.

curl ident.me

 

6: IP Address from ipecho.net Using curl Command

Last on the list is the ipecho.net command. This command sends a request to the website ipecho.net/plain using the curl tool. The website returns the public IP address of the device.

curl ipecho.net/plain

 

Conclusion

Internet service providers assign the public IP to the device that is directly connected to the internet. To find the public IP address from the command line in Linux, one can use the curl command followed by a website that returns the user’s public IP address. The dig and host command can also find the public IP address using the DNS server.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.