How to use curl to get public IP address

The curl command on Linux systems is commonly used to download and upload files to or from a remote server. Another interesting thing we can use it for is to determine the public IP address of our system. To do this, we can use curl to query websites that are configured to do nothing more than return the IP address of whichever system is connecting to it. This is one of the quickest and easiest ways to get your public IP address on the Linux command line. In this tutorial, you will see how to use the curl command to get your system’s public IP address.

In this tutorial you will learn:

  • How to install curl on major Linux distros
  • How to use curl to get public IP address
How to use curl to get public IP address
How to use curl to get public IP address
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software curl
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install curl



Although curl is a common utility, it’s not included on every single Linux distro. Specifically, it may be missing from more desktop oriented distros. Just in case it’s not already installed, you can use the appropriate command below to install it through your system’s package manager.

To install curl on Ubuntu, Debian, and Linux Mint:

$ sudo apt install curl

To install curl on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install curl

To install curl on Arch Linux and Manjaro:

$ sudo pacman -S curl

Use curl to get public IP address

There are many websites that have been configured to show someone’s public IP address. Use one or more of the following examples below to determine yours.



  1. This site will also work with the -4 or -6 curl options, if you need to retrieve your IPv4 or IPv6 address specifically.
    $ curl icanhazip.com
    OR
    $ curl -4 icanhazip.com
    OR
    $ curl -6 icanhazip.com
    
  2. $ curl ifconfig.me
  3. $ curl api.ipify.org
  4. $ curl bot.whatismyipaddress.com
  5. $ curl ipinfo.io/ip
  6. $ curl ipecho.net/plain
Using curl to get public IP address on the Linux command line

Using curl to get public IP address on the Linux command line

Of course, other sites also exist that can do the same thing, but these should be enough to cover your needs. In the future, some sites may stop working and new ones will fill their place, so keep in mind that any website on the list may not be permanent.

Closing Thoughts

In this guide, we saw how to retrieve the public IP address of a system by using curl on the Linux command line. This relies on third party public websites to show us our IP address, but it proves to be one of the quickest and easiest ways to see our public IP on the command line.



Comments and Discussions
Linux Forum