How to find your Public IP Address in Linux

Finding your device IP address(s) can be useful for various purpose. For instance, configuring a firewall, DNS and networking.

IP addresses can be divided into two major categories, public and private. Public IP is unique and can be accessed from the Internet, which is provided by the ISP (Internet Service Provider), whereas private IP addresses are reserved for internal use on the private network.

The public IP addresses are commonly used by home routers, websites, servers, etc,.

This article describes several ways to identify the public IP address of a domain and Linux system from the Linux terminal.

Refer the following guide to find your Linux system Private IP address.

The below five commands can be used to identify multiple domain & Linux system IP addresses:

  • dig Command: dig is a flexible cli tool for interrogating DNS name servers.
  • host Command: host is a simple utility for performing DNS lookups.
  • nslookup Command: Nslookup command is used to query Internet domain name servers.
  • fping Command: fping command is used to send ICMP ECHO_REQUEST packets to network hosts.
  • ping Command: ping command is used to send ICMP ECHO_REQUEST packets to network hosts.

To demonstrate this, we created a file called “domains-list.txt” and added the following domains.

# vi /opt/scripts/domains-list.txt

2daygeek.com
magesh.co.in
linuxtechnews.com

Method-1: Using dig command

dig command (stands for “domain information groper”‘) is a powerful and flexible command-line tool for querying DNS name servers.

It performs DNS lookup against a given host & website and displays the corresponding public IP addresses of the website and host as shown below:

# dig 2daygeek.com | awk '{print $1,$5}'

2daygeek.com. 104.27.157.177
2daygeek.com. 104.27.156.177

Use the following bash script to find the multiple Linux systems and domain’s public IP addresses:

# vi /opt/scripts/dig-command.sh

#!/bin/bash
for server in `cat /opt/scripts/domains-list.txt`
do echo $server "-"
dig $server +short
done | paste -d " " - - -

Once the above script is added to a file, set the executable permission for the “dig-command.sh” file:

# chmod +x /opt/scripts/dig-command.sh

Finally run the bash script to get the output.

# sh /opt/scripts/dig-command.sh

2daygeek.com - 104.27.156.177 104.27.157.177
magesh.co.in - 104.18.35.52 104.18.34.52
linuxtechnews.com - 104.27.144.3 104.27.145.3

If you want to run the above script in one line, use the following script:

# for server in 2daygeek.com magesh.co.in linuxtechnews.com; do echo $server "-"; dig $server +short; done | paste -d " " - - -

Alternatively, you can use the following shell script to find the IP address of the multiple Linux host and domain:

# for server in 2daygeek.com magesh.co.in linuxtechnews.com; do dig $server | awk '{print $1,$5}'; done

2daygeek.com. 104.27.157.177
2daygeek.com. 104.27.156.177
magesh.co.in. 104.18.34.52
magesh.co.in. 104.18.35.52
linuxtechnews.com. 104.27.144.3
linuxtechnews.com. 104.27.145.3

Method-2: Using host command

Host command is a simple CLI application to perform DNS lookup. It is commonly used to convert names to IP addresses and vice versa.

To view the domain or Linux system public IP address with host command, use the following customized host command:

# host 2daygeek.com | grep "has address" | sed 's/has address/-/g'

2daygeek.com - 104.27.157.177
2daygeek.com - 104.27.156.177

Use the following bash script to find the public IP addresses of multiple host and domain’s:

# vi /opt/scripts/host-command.sh

for server in `cat /opt/scripts/domains-list.txt`
do host $server | grep "has address" | sed 's/has address/-/g'
done

Once the above script is added to a file, set the executable permission for the “host-command.sh” file:

# chmod +x /opt/scripts/host-command.sh

Finally run the bash script to get the output as shown below:

# sh /opt/scripts/host-command.sh

2daygeek.com - 104.27.156.177
2daygeek.com - 104.27.157.177
magesh.co.in - 104.18.35.52
magesh.co.in - 104.18.34.52
linuxtechnews.com - 104.27.144.3
linuxtechnews.com - 104.27.145.3

Method-3: Using nslookup command

nslookup command is a program for querying Internet domain name servers (DNS). It is a network administration tool that helps diagnose and resolve DNS related issues.

We need to add a lot of options with nslookup command to find the public IP address of your Linux system and domain as shown below.

# nslookup -q=A 2daygeek.com | tail -n+4 | sed -e '/^$/d' -e 's/Address://g' | grep -v 'Name|answer' | xargs -n1

104.27.157.177
104.27.156.177

Use the following bash script to find the public IP addresses of multiple domain’s and Linux system:

# vi /opt/scripts/nslookup-command.sh

#!/bin/bash
for server in `cat /opt/scripts/domains-list.txt`
do echo $server "-"
nslookup -q=A $server | tail -n+4 | sed -e '/^$/d' -e 's/Address://g' | grep -v 'Name|answer' | xargs -n1 done | paste -d " " - - -

Once the above script is added to a file, set the executable permission for the “nslookup-command.sh” file.

# chmod +x /opt/scripts/nslookup-command.sh

Finally run the bash script to get the output as shown below:

# sh /opt/scripts/nslookup-command.sh

2daygeek.com - 104.27.156.177 104.27.157.177
magesh.co.in - 104.18.35.52 104.18.34.52
linuxtechnews.com - 104.27.144.3 104.27.145.3

Method-4: Using fping command

fping command is a program such as ping, which uses the Internet Control Message Protocol (ICMP) echo request to determine whether a target host is responding.

fping differs from ping because it allows users to ping any number of hosts in parallel.

Use the following options with fping command to find multiple domain and Linux host public IP address.

# fping -A -d 2daygeek.com magesh.co.in linuxtechnews.com

104.27.157.177 (104.27.157.177) is alive
104.18.35.52 (104.18.35.52) is alive
104.27.144.3 (104.27.144.3) is alive

Method-5: Using ping command

ping command (stands for: Packet Internet Groper) is a networking utility that is used to test the availability/connectivity of a target host on an Internet Protocol (IP) network.

It verifies the availability of a host by sending Internet Control Message Protocol (ICMP) Echo request packet to the target host and waiting for an ICMP Echo reply.

It summarizes statistical results based on the packets transmitted, packets received, packet loss, typically including the min/avg/max times as shown below:

# ping -c 2 2daygeek.com | head -2 | tail -1 | awk '{print $5}' | sed 's/[(:)]//g'

104.27.157.177

Use the following bash script to find the public IP addresses of multiple domain’s and Linux system :

# vi /opt/scripts/ping-command.sh

#!/bin/bash
for server in `cat /opt/scripts/domains-list.txt`
do echo $server "-"
ping -c 2 $server | head -2 | tail -1 | awk '{print $5}' | sed 's/[(:)]//g'
done | paste -d " " - -

Once the above script is added to a file, set the executable permission for the “dig-command.sh” file:

# chmod +x /opt/scripts/ping-command.sh

Finally run the bash script to get the output as shown below:

# sh /opt/scripts/ping-command.sh

2daygeek.com - 104.27.156.177
magesh.co.in - 104.18.35.52
linuxtechnews.com - 104.27.144.3

Method-6: Using resolveip command

The resolveip utility resolves host names to IP addresses and vice versa as shown below:

# resolveip 2daygeek.com magesh.co.in linuxtechnews.com

IP address of 2daygeek.com is 104.27.157.177
IP address of 2daygeek.com is 104.27.156.177
IP address of magesh.co.in is 104.18.35.52
IP address of magesh.co.in is 104.18.34.52
IP address of linuxtechnews.com is 104.27.144.3
IP address of linuxtechnews.com is 104.27.145.3

Conclusion

We have shown you several commands to find out the public IP address of a website or domain and Linux system.

We hope you find this article useful. If you do, please support us by sharing this article with your friends.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

4 Comments on “How to find your Public IP Address in Linux”

  1. – off topic –

    Are you aware that no articles more recent than March 22 are shown in Latest Posts?

    Best regards

Leave a Reply to 2daygeek Cancel reply

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