Get Public IP from Terminal on Ubuntu 20.04

If you’re using the internet, then you’ve heard of IP address, right? In computer networking, an IP (Internet Protocol) address is a numerical identifier to identify a certain device that uses the Internet Protocol for communication. The IP address may be assigned temporarily or permanently to the device.

When the device connects to other devices through the network, other devices see the public IP address. In this guide, check out how to get the public IP address of your device from the terminal on Ubuntu 20.04.

IP address basics

There are two versions of IP addresses currently in use: IPv4 and IPv6. Any IP address can be either private (can be seen only within a network) or public (can be seen from any machine on the internet). IPv4 is yet the most popular format but in the current era, it can’t assign enough devices with unique IP addresses. The IPv6 solves this by increasing the number of available unique IP addresses.

Let’s have a quick look at what an IP address looks like. In the case of IPv4, it’s a 32-bit (4 bytes) address divided into four 8-bit blocks separated by “.” symbols. The address is represented with decimal digits.


The possible range of IPv4 is from to

In the case of IPv6, it’s a bit complicated but similar in principle. An IPv6 address is a 128-bit (8 bytes) address divided into eight 16-bit blocks separated by “:” symbols. The address is represented with hexadecimal digits.

$ 2000:0000:3339:CFF1:0069:0000:0000:FEFD

Because of its sheer length, it can be problematic to use in various situations. As such, there are rules to shorten the address. After shortening the example address, it’ll look something like this.

$ 2000:0:3339:CFF1:69::FEFD

Get the public IP address from terminal

Now, we’re ready to grab the public IP address of our device. There are multiple ways to do so. For example, if you have access to GUI, then you can use the web browser to visit sites like WhatIsMyIPAddress, WhatIsMyIP, etc.

As the title of the guide suggests, we’ll be exploring the ways of checking the IP address through the terminal only. Thankfully, there are multiple tools available at our disposal to check the public IP address.

Get public IP address using dig

The dig (domain information groper) is a simple utility tool for probing DNS name servers. Using the dig tool along with OpenDNS as a resolver, we can get the public IP address.

$ dig +short myip.opendns.com @resolver1.opendns.com

Get public IP address using the host

The host command is another simple tool for performing DNS lookups. To get your public IP using host, run the following command.

$ host myip.opendns.com resolver1.opendns.com

The output is slightly bigger than the previous example. Want just the IP address as the output? Then pass the output to grep and awk to filter it out. Learn more about grep and awk.

$ host myip.opendns.com resolver1.opendns.com |
 grep "myip.opendns.com has" | awk '{print $4}'

Get public IP address using wget

Wget is a simple and powerful command-line downloader. How do we use wget to get the public IP address? By downloading a certain file from a certain online service.

First, make sure that the wget is installed on your system. Run the following command.

$ sudo apt update && sudo apt install wget -y

We’re ready to check the public IP address. The first one is IP Echo Service. Run the following command.

$ wget -qO- http://ipecho.net/plain | xargs echo

The next one is icanhazip.com. This website returns the public IP address in a plain text format.

$ wget -qO- icanhazip.com

Another similar service is ifconfig.co.

$ wget -qO- ifconfig.co

You can also use ifconfig.me to get the IP address.

$ wget -qO- ifconfig.me | xargs echo

Get public IP address using cURL

The curl tool is another popular command-line downloader/uploader that can operate on any of the popular protocols (HTTP, HTTPS, FTP, FTPS, and others).

Curl doesn’t come pre-installed on Ubuntu 20.04. Install curl.

$ sudo apt update && sudo apt install curl -y

The method is similar to the wget one.

$ curl ifconfig.co

$ curl ifconfig.me && echo

$ curl icanhazip.com && echo

Get public IP address using ip

The ip command is responsible for managing network devices, interfaces, and tunnels. It can also report information about the connected networks.

Run the following ip command to list the network interfaces and the IP address.

$ ip addr show

The following command is the equivalent one. It’ll give the same result.

$ ip a

Final thoughts

As we’ve demonstrated, there are tons of ways to check the public IP address of your device. Which one you should use? Depends on you. Try all of them out at least once. Then, you can easily decide which one to master.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.