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

Find exact installation date of Linux

Ran into an interesting question today while trying to debug a problem with a monitoring …

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

Browse anonymously in Kali Linux with Anonsurf

anonsurf allows you push your whole system via TOR network. It’s very easy to configure …

One comment

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

Use, Twitter, Facebook, or Google+ accounts to comment (anonymous comments allowed). Leave your solution to help others.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy Policy on Cookies Usage

Some services used in this site uses cookies to tailor user experience or to show ads.