Getting your IP from anywhere

Getting your IP from anywhere


2 min read

There are several circumstances where you need to know the IP of your current machine. It may also be programmatically through a script or an Ansible playbook.

To do that you can curl a website like,, or even (you need to parse the JSON though). For example:


On an AWS instance, you can reliably curl to retrieve IPs and other info on your current instance. More details there

While these APIs are free, they are 3rd parties and may have API rate limits. One day they could also disappear or not function, right? To avoid any incident, we want to find a reliable solution that lasts. And if it can also work in an internal network without internet it would be perfect.

In that case, I recommend you create a new endpoint on any domain you like. And deploy the open-source software behind

I deployed it myself, and it is available here:

Here is how I did. First, I pulled the Docker image:

docker pull mpolden/echoip

Then to activate the Geolocation, I went to MaxMind website here: Signed up for a free account and downloaded the GeoLite 2 Databases for ASN, Cities, and Countries. I also made a script to automate the update of my databases daily using my license key. Here is the script I use:

LICENSE_KEY= # Fill your key
wget "${LICENSE_KEY}&suffix=tar.gz" -O ASN.tar.gz
wget "${LICENSE_KEY}&suffix=tar.gz" -O City.tar.gz
wget "${LICENSE_KEY}&suffix=tar.gz" -O Country.tar.gz

for file in *.tar.gz; do tar xzvf "${file}" --strip-components 1 && rm "${file}"; done

And then here is how I start the container:

docker run --name ip -p 8080:8080 -d -v ./GeoLite2-ASN.mmdb:/GeoLite2-ASN.mmdb -v ./GeoLite2-City.mmdb:/GeoLite2-City.mmdb -v ./GeoLite2-Country.mmdb:/GeoLite2-Country.mmdb --restart=unless-stopped mpolden/echoip -H "X-Real-IP" -a /GeoLite2-ASN.mmdb -c /GeoLite2-City.mmdb -f /GeoLite2-Country.mmdb

I use -H "X-Real-IP" because I host it behind Nginx. FYI here is my Nginx configuration:

server {

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

server {
    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    return 404; # managed by Certbot

You can notice I use Certbot, if you don't know about Certbot, check my article:

Voilร , if you did the same, you now have your own reliable IP service as well. Feel free to reuse it in all your scripts and Ansible playbook.

Photo by Fares Hamouche on Unsplash

Did you find this article valuable?

Support Sonny Alves Dias by becoming a sponsor. Any amount is appreciated!