In this tutorial, we’ll discuss how to resolve or translate a domain name to an IP address. For this purpose, we’ll be using the following commands: ping, host, nslookup, dig, and nmap.
2. Using the ping Command
The ping utility is used to test for the reachability of a host via an ICMP echo request packet.
It comes preinstalled on many Linux distros, but if it doesn’t come preinstalled on our Linux distro, we can then install it. We can use apt or yum to install iputils-ping on Ubuntu/Debian or iputils-ping on RHEL.
Let’s resolve a domain name to IP address by pinging the domain name:
$ ping baeldung.com PING baeldung.com (220.127.116.11) 56(84) bytes of data. 64 bytes from 18.104.22.168 (22.214.171.124): icmp_seq=1 ttl=32 time=11.1 ms
When we ping the domain baeldung.com, it replies with the IP address of 126.96.36.199, which is an A record of the domain baeldung.com.
3. Using the host Command
The host command is a DNS lookup utility used to convert domain names to IP addresses and reverse IP lookup.
The command comes preinstalled with many Linux distros, but we can install it if it’s not available on our system. We can use apt or yum to install dnsutils on Ubuntu/Debian or bind-utils on RHEL.
Let’s use the host command and examine the output:
$ host baeldung.com baeldung.com has address 188.8.131.52 baeldung.com has address 184.108.40.206 baeldung.com has address 220.127.116.11 baeldung.com has IPv6 address 2606:4700:20::ac43:482d baeldung.com has IPv6 address 2606:4700:20::681a:c4a baeldung.com has IPv6 address 2606:4700:20::681a:d4a
This will print out all the DNS records like A, AAAA, MX, and other records. But in this case, we might want to check the IPv4 addresses.
Let’s add a type of record to print:
$ host -t a baeldung.com baeldung.com has address 18.104.22.168 baeldung.com has address 22.214.171.124 baeldung.com has address 126.96.36.199
From the command above, the -t option specifies the type of record to look for. In our case, it’s an A record of the domain name.
4. Using the nslookup Command
Another command we’ll be looking at is the nslookup command which is used to query internet domain names. This command offers similar features to the host command.
By default, the nslookup command comes preinstalled with some Linux distros, but we can install it on our system. The command is part of the DNSUtil or Bind-Utils package that we installed in the host command above. It has two types of mode called interactive mode and non-interactive mode.
Let’s open our terminal and type nslookup with the domain name. This will be a non-interactive mode:
$ nslookup baeldung.com Name: baeldung.com Address: 188.8.131.52 Name: baeldung.com Address: 184.108.40.206 Name: baeldung.com Address: 220.127.116.11
The non-interactive mode will look up for both IPv4 and IPv6 by default. The interactive mode of nslookup allows us to change the type of DNS record we are looking for, like A, AAA, MX, CNAME, etc.
Using nslookup with the interactive mode is similar:
[email protected]:~$ nslookup > set type=A > baeldung.com Server: 172.31.0.2 Address: 172.31.0.2#53 Non-authoritative answer: Name: baeldung.com Address: 18.104.22.168 Name: baeldung.com Address: 22.214.171.124 Name: baeldung.com Address: 126.96.36.199
From the command above, From type=A, We can change the DNS record we are looking for, like MX, CNAME, etc.
5. Using the Dig Command
The dig command works similarly with the nslookup command but with more features. The dig command comes preinstalled in some Linux distros, but we can install it if it’s not available on our Linux system.
It is available via the DNSUtil or Bind-Utils package that we saw above.
We can query for any type of DNS record we want with dig. But doing so, we’ll be translating a domain name to an IP. Let’s check for the A record of the domain name:
[email protected]:~$ dig baeldung.com A +short 188.8.131.52 184.108.40.206 220.127.116.11
From the command above, the A record is the IPv4 Address, while the +short means to get a short answer without displaying the Header. Looking for AAAA record of the domain name, which is an IPv6 with dig:
[email protected]:~$ dig baeldung.com AAAA +short 2606:4700:20::681a:c4a 2606:4700:20::681a:d4a 2606:4700:20::ac43:482d
As you can see, we can also check the IPv6 address of the domain name.
6. Using Nmap
Nmap, which stands for Network Mapper, is a tool used for security auditing and scanning large networks.
We can use apt or yum to install nmap on Ubuntu/Debian and RHEL.
Let’s take a look at host discovery with ping scan options:
$ nmap -sn baeldung.com Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-11 13:51 WAT Nmap scan report for baeldung.com (18.104.22.168) Host is up (0.25s latency). Other addresses for baeldung.com (not scanned): 22.214.171.124 126.96.36.199 2606:4700:20::681a:c4a 2606:4700:20::681a:d4a 2606:4700:20::ac43:482d Nmap done: 1 IP address (1 host up) scanned in 1.76 seconds
First, the -sn option instructs nmap to a ping scan and disables port scan. We’re using this option since we are only interested in the domain name translation. Finally, the output of nmap above shows the IP addresses of the domain and other IPs associated with the domain.
In this tutorial, we learned about different commands that we can use to translate a domain name to IP address. We looked at commands like ping, host, nslookup, dig, and nmap. Finally, we learned how to check for other DNS records with the commands.