Obtaining your external IP address from the command-line

Sometimes you need to know what your external IP address is, for example when you need to make use of a protected service where they need to (temporarily) add your IP address to their firewall’s allow list to allow you to connect to said service.

If you have a broadband connection at home it is possible that you have a fixed external IP address and that you already know what it is. If you are however visiting elsewhere you likely do not know what it is, a quick solution in such cases is to open your browser and use a site such as https://www.whatismyip.com/ to see what your external IP address is. But what if you need to know this on a Unix-like server that only offers a command-line interface (or if you simply prefer to use your shell)?

The simplest way I have found to see what your external IP address on the command-line is actually the same: using cURL or wget you can download a webpage and see its contents. If you however try to do this on a site that such as the earlier mention https://www.whatismyip.com/ you will get a bunch of HTML printed on your screen making it hard to get the information you are actually after.

If you look around a bit you will find that there are numerous sites where you can have your IP address echoed back at you in plain text. If you don’t know which one to use and/or prefer to setup your own you can do this by hosting the following tiny PHP script on one of your own servers:

<?php

header('Content-Type: text/plain');
header('Cache-Control: none');
echo $_SERVER['REMOTE_ADDR'] . PHP_EOL;

The above script will echo out the IP address used to connect to it, this can be a v4 or v6 address depending on your connection and the server’s setup. If the server that you are running this script on runs behind a load balancer you will have to adjust it a bit because $_SERVER['REMOTE_ADDR'] will contain the load balancer’s IP address instead of the client’s, this is however beyond the scope of this article.

I am hosting this PHP script under three URLs:

The first one only accepts IP v4 connections and thus will return you an IP v4 address, the second will only accept IP v6 connections and thus will return you an IP v6 address, and the last one will accept both IP v4 and IP v6 connections (in which case the IP address shown depends on the connection used).

When visiting any of these using your browser you will be greeted with only your IP address. Because the output does not contain any other markup you can getting your external IP address from it with cURL or wget like so:

➜  curl https://ip4.5ec.nl
135.181.44.212
➜  wget -qO- https://ip6.5ec.nl
2a01:4f9:c010:bbd2::1
➜  curl https://ip.5ec.nl
2a01:4f9:c010:bbd2::1

If you want a shortcut to the above commands you can use getmyip which is a little open source program that I wrote using Go. Once you have it installed you can get your external IP address like so:

➜  ./getmyip 
2a01:4f9:c010:bbd2::1
➜  ./getmyip -4
135.181.44.212
➜  ./getmyip -6
2a01:4f9:c010:bbd2::1

And with that we have come to the end of this article.

If you have feedback on this article or have questions based on its contents then feel free to reach out to me on Twitter or through e-mail.