Finding Your External IP Address

Just like you might have several ID cards for yourself, depending on where you are and what you have to verify, so does your computer on a network. The most common ID used for computers is the IP address, and IP addresses really build up the framework for both local network and internet communication. Usually, a computer will have at least two IP addresses; one for the internal network and one for external (internet). Nowadays, you might actually have a second external IP address since everyone is slowly switching over to a new addressing system. The previous IP version 4 (IPv4) is still the more common one, since it's been around so long. But now that it's literally running out of possible addresses to assign, there's a movement towards IPv6 which solves the issue by having more bits available for addressing.

Without a network, your computer will usually self identify with the address If you have several computers or devices directly connected to each other, they will either use this 127.0.0.X network or use their MAC addresses, which are alternate addresses all network hardware carry. 

Once your computer gets on a network - either through a switch or router - your computer's default IP will be overwritten by that network. Your switch or router will handle the addressing on that network now, and that network will usually come with either a 10.0.0.X or a 192.168.0.X addressing scheme by default. 

Using Linux's ip utility, we can check your PC's loopback address (when no network present):

$ ip a show lo | grep "inet "

We can use a similar command to get your PC's IP on the the local network:

$ ip a show eth0 | grep "inet "

Once you need to get onto the internet however, things become a little more complicated. Your PC, and your router, can't assign their own public IP since there's billions of devices out there. So your public, or external, IP is assigned to you rather than by you, and very often it's not even the same one each time; it's dynamic and could change as often as you restart your PC or network. 

Finding your external IP address is actually very easy, and you may have already done it before. Sites like My External IP or Show IP will hit you with your external IP address as soon as you land on their page. However, there are more elegant solutions out there than going into a browser every time, and you'll likely be using these methods for regular online operations, like connecting through SSH or adding DNS records. The way you'll do this is using utilities that deal with transfers over HTTP, FTP, Telnet or DNS. All of the commands listed here will just return your external IP address.

Using DNS, you can get your external address using these commands:

$ dig +short


$ nslookup . | grep "Address: "

These commands will use FTP and Telnet to get the same results (each line is a separate command):

$ echo close | ftp | awk '{print $4; exit}'

$ nc 23 | grep IPv4 | cut -d' ' -f4

$ telnet 23 | grep IPv4 | cut -d' ' -f4

$ netcat 80 <<< $'GET / HTTP/1.1\nHost:\n\n' | tail -n1

Using HTTP(S):

$ curl

$ curl

$ curl

$ curl

$ curl -s | awk '{print $5}'

$ curl -s

$ curl

$ curl

$ curl

$ curl

$ curl

$ wget -q -O - | sed -e 's/[^[:digit:]\|.]//g'

You can use these commands within other scripts and in everyday operations, especially in circumstances where your external IP is dynamic. 

That about covers it. If you have any questions or comments about this article, comment below or contact us on Facebook and Twitter. Before signing off I'll leave you with this "shortcut" you can use to cut down on the length of these commands for the future, by creating an alias. It looks like this:

$ alias wanip='dig +short'
$ echo "alias wanip='dig +short'" | tee -a ~/.bashrc

After setting this up, you'll just need this command to get your IP:

$ wanip

Until next time!

March 12 2018

Add or review comments

Please leave your comment

Existing comments

Comments 1

Robert baker
Great article, thanks for taking the time!