Your router, that device that makes it possible to connect to the Internet, has two IP addresses by default – an internal one where it can be reached by computers behind it, that’s those in your internal network, and an external one that makes it possible for it to talk to other routers on the Internet.

In this article, you’ll learn two tricks to get your router’s public, or external IP address. For most people, finding out what their router’s IP address involves logging into the router’s management dashboard or using one of many websites that provide that service.

If you’re using a Linux computer, you have two more options.

Use Curl to query icanhazip

icanhazip (I-can-haz-IP) is an ad-free service maintained by a well-known Linux user. Visiting http://icanhazip.com will return your IP address, but you can get the same information from the command line using the curl command, a tool used to transfer data from or to a server with the aid of one of several supported protocols, like HTTP, FTP, etc. The following shows how to use curl to query icanhazip.com:

# What's your router's public IP address?
# Let curl provide the answer

curl http://icanhazip.com

# By default, the above command will return your router's IPv6 address.
# To get the IPv4 address, use this next command

curl -4 http://icanhazip.com

#

Use Ansible’s ipify_facts module

Ansible is an open source, SSH-based automation, configuration management and deployment tool from Red Hat (via acquisition). Even on Fedora Ansible is not installed by default, so you need to first install it. There’s a package for it in the official repository of every Linux distribution, so use your distribution’s package manager to install it. This article shows how to install it on your favorite Linux distribution.

The aspect of Ansible that makes it possible to retrieve your router’s public, or external IP address is a module called ipify_facts. The following shows how to use it:

# What's your router's public IP address?
# Let Ansible provide the answer
# ">" indicates output

ansible localhost -m ipify_facts

> localhost | SUCCESS => {
>    "ansible_facts": {
>        "ipify_public_ip": "12X.127.68.1"
>    }, 
>    "changed": false
> }

# To get the same result on one line, tack on the -o option, like so

ansible localhost -m ipify_facts -o

> localhost | SUCCESS => {"ansible_facts": {"ipify_public_ip": "12X.127.68.1"}, "changed": false}

#

Unlike the icanhazip service, Ansible only returns the IPv4 address, so if you need to know your router’s IPv6 address, use curl to query icanhazip.

Ansible