How to get Public IP from Linux Terminal?

Public addresses are assigned by InterNIC and consist of class-based network IDs or blocks of CIDR-based addresses (called CIDR blocks) that are guaranteed to be globally unique to the Internet. How to get Public IP from Linux Terminal - blackMORE OpsWhen the public addresses are assigned, routes are programmed into the routers of the Internet so that traffic to the assigned public addresses can reach their locations. Traffic to destination public addresses are reachable on the Internet. For example, when an organization is assigned a CIDR block in the form of a network ID and subnet mask, that [network ID, subnet mask] pair also exists as a route in the routers of the Internet. IP packets destined to an address within the CIDR block are routed to the proper destination. In this post I will show several ways to find your public IP address from Linux terminal. This though seems like a waste for normal users, but when you are in a terminal of a headless Linux server(i.e. no GUI or you’re connected as a user with minimal tools). Either way, being able to getHow to get Public IP from Linux Terminal public IP from Linux terminal can be useful in many cases or it could be one of those things that might just come in handy someday.

There’s two main commands we use, curl and wget. You can use them interchangeably.

Curl output in plain text format:


curl output in JSON format:

curl (bit ugly)

curl output in XML format:


curl all IP details – The motherload


Using DYNDNS (Useful when you’re using DYNDNS service)

curl -s '' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g' 
curl -s | grep -o "[[:digit:].]\+"

Using wget instead of curl

wget -O - -q ; echo
wget -O - -q ; echo

Using host and dig command (cause we can)

You can also use host and dig command assuming they are available or installed

host -t a | sed 's/.*has address //'
dig +short

Sample bash script:


PUBLIC_IP=`wget -O - -q ; echo`

Quite a few to pick from.

I was actually writing a small script to track all the IP changes of my router each day and save those into a file. I found these nifty commands and sites to use while doing some online research. Hope they help someone else someday too. Thanks for reading, please Share and RT.

Check Also

Browse anonymously with Anonsurf in Kali Linux - blackMORE Ops -5

Browse anonymously in Kali Linux with Anonsurf

IP spoofing, also known as IP address forgery or a host file hijack, is a …

Setup DHCP or static IP address from command Line in Linux - blackMORE Ops - 4

Setup DHCP or static IP address from command line in Linux

Did you ever had trouble with Network Manager and felt that you need to try …

One comment

  1. Curl output in plain text format:
    as well. :)

Leave a Reply

Your email address will not be published. Required fields are marked *