5.6 Resolving Hostnames

5.6 Resolving Hostnames

The last bit of configuration for a simple network connection is hostname resolution. Simply put, your computer needs to know how to translate a name such as www.example.com to an IP address. Without this capability, your Internet connection is practically worthless — no one in their right mind advertises IP addresses for Web sites and email addresses, partly because a host's IP address is subject to change, but also because it's not easy to remember a bunch of numbers.

The most basic way to do this mapping is with the /etc/hosts file, which looks like this:      localhost       atlantic.aem7.net       atlantic       pacific.aem7.net        pacific

In the bad old days, there was one central hosts file that everyone repeatedly copied to their own machine to stay up to date. This might have worked for a handful of machines, but as the ARPANET/Internet grew, this quickly got out of hand. The Domain Name System (DNS) decentralized hostname resolution. The idea is that your machine's resolver (in the C library) asks a nameserver for the IP address corresponding to a hostname. If the nameserver does not know, it can find out who is responsible for that name and ask for the IP address from the responsible party's nameserver. If you do not already know the DNS server addresses that your ISP uses, ask your provider for them.

Let's say that your ISP's nameserver addresses are and Put the servers in your system's /etc/resolv.conf file like this:

search mydomain.example.com example.com

The search line in the preceding example asks the resolver to look for the IP address of host.mydomain.example.com, and for host.example.com if you ask for an incomplete domain name host that does not resolve on the first attempt.

For a simple network configuration where you do not have control of your own nameserver, there is little else to know about DNS except how to make sure that your machine's hostname resolution works. To verify this, type host known_name where known_name is some Internet name that you know exists (such as www.example.com). The output should be something like this:

known_name has address

You should also be able to look up a name based on its IP address with host address. If the address owner is doing their part, you should get the hostname corresponding to address, but DNS administrators often do not configure their servers to return these hostnames. Furthermore, the mapping of hostname to address is not one-to-one; you can have many hostnames for a single address.

Before moving on, take a look at /etc/nsswitch.conf. It should have a line like this:

hosts:          files dns

Putting files ahead of dns here ensures that your system checks the /etc/hosts file for the hostname of your requested IP address before asking the DNS server. This is usually a good idea (especially for looking up localhost), but your /etc/hosts file should be as short as possible. Don't put anything in there because you want to boost performance; it will burn you later. You can put all the hosts within a small private LAN in /etc/hosts, but the general rule of thumb is that if a particular host has a DNS entry, it has no place in /etc/hosts.


The /etc/hosts file is also useful for resolving hostnames in the early stages of booting, when the network may not be available.

DNS is a fairly broad topic, and if you have any responsibility for domain names, have a look at DNS and BIND [Albitz].