In this tutorial, we will show you a quick and easy way to use curl on Linux to get your public IP address.
There are many reasons why you might want to get your device’s public IP address using curl. One of the biggest is that it is easy to integrate into your bash scripts.
For example, you can write a script that automates setting up a service that needs to know your machine’s IP address. Instead of requiring a user to provide the public IP of your machine, you can simply fetch it using curl.
A public IP address differs from a local one in that it is the IP that a server sees when you attempt to connect to it and is the same IP an outside user can use to connect to your machine.
We can use curl to get your public IP address by connecting it to a service that outputs the IP you connected with. Plenty of services offer this functionality completely free, and we will show you how to use one in particular.
Best of all, many Linux distributions come with curl pre-installed or, at worst, easily available through their package repository.
Fetching your Public IP Address on Linux using Curl
In this section, we will show you how to use curl on Linux to fetch a public IP address.
The service that we are using is “icanhazip.com“. This service is completely free and has been around for years. It only provides the IP address you require and supports both IPv4 and IPv6 IP addresses.
Installing Curl
1. Before you can use curl to fetch your public IP address on Linux, you will need to have it installed on your system.
The easiest way to verify that curl is installed is to get it to output its version.
curl --version
If it is installed, you should see a message output stating the version of curl installed.
curl 7.88.1
Alternatively, if you get an error message like the following, you will be required to install curl. Don’t worry this process is really simple.
-bash: curl: command not found
2. If you get an error message about a package not found, you will need to install curl to Linux before using it to get your public IP address. As mentioned earlier, this package is typically available on every major repository.
Below, we will show you how to easily install curl. These methods differ slightly depending on your operating system.
APT Package Manager: Debian, Ubuntu, Raspberry Pi OS, Linux Mint
a. If you are running a Debian-based operating system such as Ubuntu or Raspberry Pi OS, you will need to update your package list cache before you can install curl.
You can update this cache by using the following command.
sudo apt update
b. Once the cache has been updated, you can install curl by running the following within the terminal.
sudo apt install curl
DNF/YUM Package Manager: Red Hat, Fedora, CentOS
Those running an operating system like Fedora or RHEL can install curl using the following command. If you happen to be using an old version of these operating systems, replace “dnf
” with “yum
“.
sudo dnf install curl
Pacman Package Manager: Arch Linux, Manjaro
If you are using an operating system such as Arch Linux, you can install curl by running the command below within the terminal.
sudo pacman -S curl
Alpine Package Keeper: Alpine Linux
Finally, if you are using Alpine Linux, you can install curl on your system using the following command.
apk add curl
Using Curl on Linux to get your Public IP Address
3. Once you are certain you have curl installed on your Linux machine, we can use it to get your device’s public IP address.
As mentioned earlier in this guide, we will use curl to fetch your public IP address by sending a get request to the website icanhazip.com.
With the modern web, there is a chance that your machine has both an IPv4 and IPv6 address. Luckily, icanhazip supports both of these network protocols, and curl allows you to select whether to use your IPv4 or IPv6 address when connecting.
Fetching your Public IP address using Curl
If you only want to get the public IP address of your machine that it is using and aren’t worried whether it’s an IPv4 or IPv6 address, then you can simply use the command below.
With this command, curl will make a request to icanhazip.com that will respond with the public IP address you connected to its service with.
curl icanhazip.com
In our case, curl returned the public IPv6 address of our Linux machine.
XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX
Using Curl to get the Public IPv4 Address
One advantage of using curl on Linux to get your public IP address is that we can tell it only to attempt to connect using IPv4 or IPv6.
If you are only interested in your Linux machine’s public IPv4 address, you can use the command below. The “-4
” option tells curl to try to use only an IPv4 address.
curl -4 icanhazip.com
Since we enforced an IPv4 connection, the curl tool returned us an IPv4 address from icanhazip.
XXX.XX.XXX.XXX
Getting your Public IPv6 Address using Curl
Like being able to force curl to only connect using IPv4, you can also force curl on Linux to get the IPv6 address assigned to your machine. Please note that not all machines will have an IPv6 address, which is purely dependent on your ISP or hosting provider.
To use curl on Linux to get your machine’s public IPv6 address, run the following command. By using the “-6
” option, we tell curl to use the machine’s IPv6 address if it has one available.
curl -6 icanhazip.com
Below you can see that this time that curl returned the IPv6
XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX
Conclusion
Hopefully, by this stage, you will have a good idea of how to use curl to get the public IP address of your Linux device.
You will quickly find using curl to get your IP address super useful, especially when you are stuck using the terminal or trying to automate tasks using a bash script.
Please feel free to comment below if you have had any issues using these services to get your public IP easily.
If you found this quick guide helpful, we highly recommend exploring our several other Linux guides.