Sometimes, we need to resolve IP addresses for a hostname or domain name. You can get the IP address of a hostname using various commands such as ping
, dig
, host
and nslookup
.
For our example, we will find the IP addresses associated with hostname ezoic.com. These commands work on both Linux and macOS.
ping
command
ping
is the easiest command for this. Press Control+C to stop.
ping ezoic.com
Output:
$ ping ezoic.com
PING ezoic.com (34.235.35.145): 56 data bytes
64 bytes from 34.235.35.145: icmp_seq=0 ttl=246 time=28.128 ms
64 bytes from 34.235.35.145: icmp_seq=1 ttl=246 time=19.554 ms
64 bytes from 34.235.35.145: icmp_seq=2 ttl=246 time=10.445 ms
64 bytes from 34.235.35.145: icmp_seq=3 ttl=246 time=18.912 ms
^C
--- ezoic.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 10.445/19.260/28.128/6.256 ms
Alternatively, you can run ping
with -c1
option to make it send only one ping packet.
$ ping -c1 ezoic.com
PING ezoic.com (34.235.35.145): 56 data bytes
64 bytes from 34.235.35.145: icmp_seq=0 ttl=246 time=18.738 ms
--- ezoic.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 18.738/18.738/18.738/0.000 ms
To capture this IP address, we use a combination of ping
options along with tr
and awk
.
$ ping -q -c1 ezoic.com | tr -d '():' | awk '/^PING/{print $3}'
34.235.35.145
host
command
host
is a simple command that performs DNS lookups. It is used to resolve IP address to hostnames and vice versa.
host ezoic.com
Output:
$ host ezoic.com
ezoic.com has address 3.216.225.198
ezoic.com has address 3.222.197.147
ezoic.com has address 18.211.109.206
ezoic.com has address 35.174.149.57
ezoic.com has address 34.235.35.145
ezoic.com mail is handled by 5 alt1.aspmx.l.google.com.
ezoic.com mail is handled by 5 alt2.aspmx.l.google.com.
ezoic.com mail is handled by 1 aspmx.l.google.com.
ezoic.com mail is handled by 10 aspmx2.googlemail.com.
ezoic.com mail is handled by 10 aspmx3.googlemail.com.
To capture this IP address, we use a combination of host
along with awk
.
$ host ezoic.com | awk '/has address /{print $4}'
3.216.225.198
18.211.109.206
3.222.197.147
34.235.35.145
35.174.149.57
From the output, we see that the hostname ezoic.com resolves to 5 IP addresses.
getent
command
getent
is a Linux command, and not installed by default in macOS. It gets entries from databases in the Name Search Switch libraries in /etc/nsswitch.conf
.
You can lookup IPv4 and IPv6 addresses for a hostname.
For IPv4:
$ getent ahostsv4 ezoic.com
34.235.35.145 STREAM ezoic.com
34.235.35.145 DGRAM
34.235.35.145 RAW
18.211.109.206 STREAM
18.211.109.206 DGRAM
18.211.109.206 RAW
3.216.225.198 STREAM
3.216.225.198 DGRAM
3.216.225.198 RAW
35.174.149.57 STREAM
35.174.149.57 DGRAM
35.174.149.57 RAW
3.222.197.147 STREAM
3.222.197.147 DGRAM
3.222.197.147 RAW
For IPv6:
$ getent ahostsv6 ezoic.com
::ffff:35.174.149.57 STREAM ezoic.com
::ffff:35.174.149.57 DGRAM
::ffff:35.174.149.57 RAW
::ffff:34.235.35.145 STREAM
::ffff:34.235.35.145 DGRAM
::ffff:34.235.35.145 RAW
::ffff:3.222.197.147 STREAM
::ffff:3.222.197.147 DGRAM
::ffff:3.222.197.147 RAW
::ffff:18.211.109.206 STREAM
::ffff:18.211.109.206 DGRAM
::ffff:18.211.109.206 RAW
::ffff:3.216.225.198 STREAM
::ffff:3.216.225.198 DGRAM
::ffff:3.216.225.198 RAW
To get only the IPv4 addresses without duplication:
$ getent ahostsv4 ezoic.com | awk '{print $1}' | sort | uniq
18.211.109.206
3.216.225.198
3.222.197.147
34.235.35.145
35.174.149.57
To get only the IPv6 addresses without duplication:
$ getent ahostsv6 ezoic.com | awk '{print $1}' | sort | uniq
::ffff:18.211.109.206
::ffff:3.216.225.198
::ffff:3.222.197.147
::ffff:34.235.35.145
::ffff:35.174.149.57
nslookup
command
nslookup
is a tool for querying the Domain Name System (DNS) to obtain the mapping between domain name and IP address, or other DNS records.
nslookup ezoic.com
Output:
$ nslookup ezoic.com
Server: 66.175.211.5
Address: 66.175.211.5#53
Non-authoritative answer:
Name: ezoic.com
Address: 3.216.225.198
Name: ezoic.com
Address: 35.174.149.57
Name: ezoic.com
Address: 18.211.109.206
Name: ezoic.com
Address: 3.222.197.147
Name: ezoic.com
Address: 34.235.35.145
To get only the IP addresses:
$ nslookup ezoic.com | grep 'Address: ' | awk '/Address: /{print $2}'
34.235.35.145
18.211.109.206
3.222.197.147
35.174.149.57
3.216.225.198
dig
command
dig
is a tool to query DNS servers and perform lookups.
dig +short ezoic.com
Output:
$ dig +short ezoic.com
18.211.109.206
3.216.225.198
35.174.149.57
3.222.197.147
34.235.35.145
Our IP address to hostname and hostname to IP address online tools
If you do not have access to these tools or a Linux or Mac server, feel free to use our online tools:
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.