37

I have the following /etc/hosts file on a ubuntu 12.04 machine

127.0.0.1 localhost
10.248.27.66 ec2-50-112-220-110.us-west-2.compute.amazonaws.com puppetmaster

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

However the host command does not resolve the name puppetmaster correctly, while the telnet command is does

root@ip-10-248-34-162:/home/ubuntu# host puppetmaster
Host puppetmaster not found: 3(NXDOMAIN)

root@ip-10-248-34-162:/home/ubuntu# telnet puppetmaster 8140
Trying 10.248.27.66...
Connected to ec2-50-112-220-110.us-west-2.compute.amazonaws.com.
Escape character is '^]'.

Why does the host command not resolve entries in /etc/hosts?

4 Answers 4

69

The host program uses libresolv to perform a DNS query directly, i.e., does not use gethostbyname.

Most programs, when attempting to connect to another host, invoke the gethostbyname system call or a similar function. This function obeys the configuration of /etc/nsswitch.conf. This file has a line which in Ubuntu 12.04 defaults to the following:

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

which means that it will first use /etc/hosts, then fall back to DNS queries.

If you want to perform a host lookup this way, you can do this with getent hosts. For example:

$ getent hosts serverfault.com
198.252.206.16  serverfault.com

I hope this helps.

6
  • 2
    Thanks Kvisle, but I'm still a little confused. The line in /etc/nsswitch.conf looked like this hosts: files dns and I changed it to hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 however in both cases /etc/hosts is not queried, instead it appears direct dns queries are made. How can I ensure that /etc/hosts will be queried first when using a command like dig or host?
    – user784637
    Apr 11, 2013 at 19:33
  • 6
    You can't. dig/host uses DNS directly. If you need to do a namelookup that checks the hosts-file first, you have to use getent hosts or something else that uses gethostbyname()
    – Kvisle
    Apr 11, 2013 at 19:35
  • 1
    Oh I see, my bad, I got it now =)
    – user784637
    Apr 11, 2013 at 19:44
  • 2
    I believe that nowadays one should be using getent ahosts instead of getent hosts because getent hosts uses gethostbyaddr() or gethostbyname*() which are obsolete. If I have understood correctly, getent hosts emulates how old UNIX C programs used to work and getent ahosts emulates the way moderm programs should work. Oct 2, 2017 at 6:10
  • But is there a way to make host command use also /etc/hosts?
    – Kornel
    Jan 9, 2018 at 20:25
13

Because the host utility is exclusively a DNS lookup utility.

Most applications use the library calls getaddrinfo or gethostbyname. These libraries interrogate a file called /etc/nsswitch.conf to determine the lookup priority and policy of how to perform different lookups.

Typically /etc/nsswitch.conf contains the line

hosts:        files dns

Which tells a program to first interrogate /etc/hosts and then interrogate DNS if unsuccessful.

Since hosts does exclusively DNS lookups it does not peek into /etc/hosts to do the lookup.

1
  • I suppose "Since hosts does..." is a typo for "Since host does...". Oct 21, 2020 at 14:36
4

You will find that dig and nslookup behave the same way as host.

The reason for this is that the purpose of all of these commands is to do DNS lookups, not to look in files.

Most other programs use the operating system's name resolver which consults /etc/nsswitch.conf and then (if required) /etc/resolv.conf to decide how to resolve the hostname you are requesting. (This is a simplification, there are other options.) The nsswitch.conf file usually puts precedence on local files rather than DNS.

1
-1

check the file /etc/nsswitch.conf and look for the line starting with word "hosts"? Do you see the word "files" on this line ? If yes, is it before or after the word "dns" ?

On a normal system, this line should be something like

hosts      files dns

if yours is not present or in different order, that might be your problem.

1
  • It's present and looks like this hosts: files dns
    – user784637
    Apr 11, 2013 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .