Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux command for public ip address

I want command to get Linux machine(amazon) external/public IP Address.

I tried hostname -I and other commands from blogs and stackoverflow like

ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

and many more. But they all are giving me internal IP Address.

Then I found some sites which provides API for this.

Example : curl http://ipecho.net/plain; echo

But I don't want to rely on third party website service. So, is there any command line tool available to get external IP Address?

like image 589
Darshan Patel Avatar asked Sep 20 '25 02:09

Darshan Patel


1 Answers

You could use this script

# !/bin/bash 
#
echo 'Your external IP is: '
curl -4 icanhazip.com

But that is relying on a third party albeit a reliable one. I don't know if you can get your external IP without asking someone/somesite i.e. some third party for it, but what do I know.

you can also just run:

curl -4 icanhazip.com

This is doing the same thing as a command the -4 is to get the output in Ipv4

like image 51
disco Avatar answered Sep 22 '25 21:09

disco