try.directtry.direct

Back to explains list

Find the public IP address from the terminal

It is important to be able to find out your server's public IP address right from the terminal while working on remote server. Check the article below, to learn how to discover your system or Server's Public IP address from the Terminal on Linux.


1. Using host Command


Нou can find your server's public IP address y using the host command:

host myip.opendns.com resolver1.opendns.com

Output:

Using domain server:
Name: resolver1.opendns.com
Address: 2620:119:35::35#53
Aliases:

myip.opendns.com has IPv6 address 2400:6180:100:d0::8e9:c001

If you want to get an IPv4 address using the host command, you can add the “-4” option to it.

host -4 myip.opendns.com resolver1.opendns.com

Output:

Using domain server:
Name: resolver1.opendns.com
Address: 208.67.222.222#53
Aliases:

myip.opendns.com has address 143.110.250.111

You can use the grep command to filter only IPv6/IPv4 by running the following command:


For IPv4

host -4 myip.opendns.com resolver1.opendns.com | grep "address" | awk 'NF>1{print $NF}'Output:
143.110.250.111

For IPv6

host -6 myip.opendns.com resolver1.opendns.com | grep "address" | awk 'NF>1{print $NF}'Output:
2400:6180:100:d0::8e9:c001

2. Using Dig Command

The dig command is one of the easiest methods to find the public IP address of your server.


You can execute any of the commands below to find your system IP address.

dig ANY +short myip.opendns.com @resolver1.opendns.comdig ANY +short @resolver2.opendns.com myip.opendns.comdig +short ANY whoami.akamai.net @ns1-1.akamaitech.netdig +short @ns1-1.akamaitech.net ANY whoami.akamai.netdig +short ANY o-o.myaddr.l.google.com @ns1.google.comYou can specify the IP version as shown in the below example:dig +short ANY o-o.myaddr.l.google.com @ns1.google.com -4

Kindly note that you can use the "-4" option with dig for IPv4 and the "-6" option for IPv6 in case you want to get a specific IP version.


3. Using the wget command

Wget is a powerful command-line downloader available on most Linux distributions. The wget command allows you to find the public IP address of your system by using it with third-party websites, as shown below:


wget -qO- checkip.amazonaws.comwget -qO- whatismyip.akamai.comwget -qO- http://ipecho.net/plain | xargs echowget -qO- ifconfig.co -4wget -qO- ifconfig.cowget -qO- https://ifconfig.me | xargs echowget -qO- icanhazip.com -4

4. Using the curl command

Just like the wget commands, you can use the curl command with a third-party service to find the public IP address of your system.


Below are some third-party services you can use with the curl command:

curl ip.mecurl -4 icanhazip.comcurl ifconfig.mecurl ipecho.net/plaincurl ifconfig.cocurl checkip.amazonaws.comcurl whatismyip.akamai.comcurl ident.me

Now you can easily discover your system's public IP address using Terminal on Linux.


Do we miss something useful? Send it to us, use the contact us form or Join our discord channel and consult with our tech experts.


Need expert's service? Get system administration help from our partners


Next article: Simple Firewall