What is my public IP [Linux CLI]

clifun(42)
Published in
#linux
Words
238
Reading
2 min
Listen
Play
8y

Frequently I need to find what public IP I am behind. In the past, I have frequently gone to sites like WhatIsMyIP.com. The search engine, DuckDuckGo used to display my public ip when I searched their site for, "my public ip". This no longer works, which I suspect might be because of privacy concerns.

Today, I needed to find the public ip I was at and I thought, it would be really cool if I found a CLI (command line) app that did this since I spend about 90% of my time using the command line anyway and having to leave the command line to start a browser to search is a waste of time.

I thought about looking on GitHub but decided to just search DuckDuckGo instead. What I found was much simpler so I decided to share what I did to solve this problem.

reference: https://www.cyberciti.biz/faq/how-to-find-my-public-ip-address-from-command-line-on-a-linux

Since the command line shell I use in linux is bash, I added

alias whatismyip='dig +short myip.opendns.com @resolver1.opendns.com'

to the file, ~/.bashrc

To apply this change, I ran bash from the command line.

Then to test:

kharris@kharris:~$ whatismyip 
100.99.98.97

The output is clean without any quotes or other info that would need to be stripped in order to copy and paste it where needed.

Notes/Disclaimer:
  • I am not responsible for any problems you may experience
  • Your results may differ that what is described above
  • I tested this on a MacBookPro running Ubuntu 18.04
  • GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
What is my public IP [Linux CLI] | Ecency