1. Introduction
We often have to resolve a given hostname to its IP address. For this, we could use popular commands such as host, nslookup, dig, and nmap, but these commands may ignore the local /etc/hosts file and resolve names directly with the DNS server.
The /etc/hosts file is a special file in Linux where we can specify mappings for different IP addresses to their domain names. Any application that we run or test locally will first use this file to resolve the IP address. DNS servers are used only if the name isn’t found in this file. Therefore, for testing or inspection purposes, we need to be able to use this file to resolve hostnames to IP addresses.
In this tutorial, we’ll look at different command-line tools to resolve the IP address using the /etc/hosts file first.
2. Preparation
As an example, we’ll map example.com to 127.0.0.1 in the /etc/hosts file. For this, we need to add a line in the file using nano, vim, or any text editor of our choice. A simple syntax is used to map a name to an IP address inside of the file:
$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 r2d2
# The line we added is below
127.0.0.1 example.com
... more lines
Once we see the line starting with 127.0.0.1 pointing to example.com, we’ll proceed further. We must also remember to remove this line once we finish the tutorial, to avoid any surprises later on.
3. Using getent
The name getent is short for “get entries” and, as the name suggests, the command can get us entries from administrative databases. The specific database we’re interested in is called ahosts. We use the getent command and ahosts option, then specify that we want to look up the IP address of example.com:
$ getent ahosts example.com
127.0.0.1 STREAM example.com
127.0.0.1 DGRAM
127.0.0.1 RAW
From the above output, we see that in the “STREAM” line, we have example.com, and the line starts with the IP address we had configured earlier – 127.0.0.1. Specifying the ahosts database gives us both IPv4 and IPv6 addresses, if they’re present.
We could also use the hosts database, which also reads the /etc/hosts file, just like ahosts does. However, the hosts database has a limitation when we have both IPv4 and IPv6 entries for the same name. In that case, this would return either the IPv4 or the IPv6 address, but not both. Let’s try this out by adding the following line in our /etc/hosts file:
::1 example.com
Now, let’s see the output with both hosts and ahosts databases:
$ getent hosts example.com
::1 example.com
$ getent ahosts example.com
::1 STREAM example.com
::1 DGRAM
::1 RAW
127.0.0.1 STREAM
127.0.0.1 DGRAM
127.0.0.1 RAW
Above, we see the output using hosts and ahosts databases. The hosts database gives us only the IPv6 address. Next, the ahosts database gives us both the IPv4 and IPv6 addresses.
4. Using ping
In Linux, we commonly use the ping utility to test the reachability of a specified host. The output of the ping command prints the IP address via which it is trying to reach the host. Therefore, we can use this fact to look up the IP address of a given hostname:
$ ping example.com
PING example.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms
^C
--- example.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.022/0.022/0.022/0.000 ms
In the above output, once we see that our IP address, 127.0.0.1, is printed, we press ^C to stop the command.
5. Using Python
With python3 installed on our system, we can execute a short program to resolve the IP address for a given hostname. Let’s use Python to resolve example.com to an IP address in our terminal:
$ python3 -c 'import socket;print(socket.getaddrinfo("example.com","http")[0][4][0])'
127.0.0.1
In the above command, we used the -c option of the python3 command to supply a Python program as a string. In the program, we imported the socket library to connect to example.com and print the IP address used for the connection. If we’ve got both IPv4 and IPv6 addresses associated with our given hostname, this command shows only one of them.
6. Using Perl
If we have Perl installed on our system, we can run a Perl script from the terminal to look up the IP address, just as we did with Python above:
$ perl -e 'use Socket; print inet_ntoa(inet_aton("example.com")) . "\n";'
127.0.0.1
Above, we used the -e option to supply the script to the perl command. We imported Perl’s Socket module and used it to get the IP address of example.com by connecting to it. We must note that this command shows only the IPv4 address, even if both IPv4 and IPv6 addresses are present for the given hostname.
7. Conclusion
In this article, we looked at different ways of looking up the IP address of a given hostname, using the /etc/hosts file first, and only then using the DNS. The commands getent and ping were easy to use but provided slightly verbose output. We would need to process these outputs further to extract just the IP address. We can also execute short Python or Perl scripts to accomplish the task and give us just the IP address.