Change the order of name resolution

Author: Alex Nikiforov nikiforov.al@gmail.com FreeBSD

Reviewer: Brett Mahar brett.maharATgmailDOTcom OpenBSD

Reviewer: Yannick Cadin yannick@diablotin.fr FreeBSD/OpenBSD


Concept

Be able to determine the default order of host name resolution on BSD systems and recognize which configuration file controls the order of host name resolution.

Introduction

Most programs (like ping and traceroute) that do a hostname lookup use the gethostbyname(3) or getaddrinfo(3) functions. Commonly the local file /etc/hosts is used first and then normal DNS lookups are done if needed. (The /etc/hosts file is introduced in section TODO.)

Note that ping or telnet can be used to show hostname lookups as the systems sees them.

On DragonflyBSD, NetBSD and FreeBSD, the "name-service switch dispatcher" configured in /etc/nsswitch.conf is used to select which sources for hostname lookups to use and what order to use them. The possible sources are "files" for /etc/hosts, "dns" to use DNS, and "nis" to use NIS (aka "YP"). For example:

""$ grep ^hosts /etc/nsswitch.conf ""hosts: files dns

On OpenBSD, the /etc/resolv.conf has an additional "lookup" keyword which defines the ordering of the databases to use, such as "bind" for using DNS (network-based), "file" for searching in /etc/hosts, and "yp" for retrieving from a YP server.

If the /etc/resolv.conf file doesn't exist on OpenBSD, then the default is that the /etc/hosts file is used. If the /etc/resolv.conf file does exist but does not define the "lookup", then the default "lookup" order is: "bind file".

When using normal DNS lookups, the DNS servers are defined in /etc/resolv.conf. Note that dhclient will overwrite the values in resolv.conf with the data received from the DHCP server. To avoid this custom configurations should be placed in /etc/resolv.conf.tail.

TODO: /etc/resolv.conf.tail is OpenBSD only?

This is covered in section TODO.

Note that a few programs -- like "dig" and "nslookup" -- do the DNS lookups directly because they are more specific.

Examples

Practice Exercises

More information

ping(8), telnet(1), nsswitch.conf(5), resolv.conf(5), host.conf(5)