5

My question addresses a strange behaviour of hostname -f on my Gentoo Linux System.

The symptom:

rt01 ~ # time hostname -f
rt01.domain.net

real    0m5.007s
user    0m0.001s
sys     0m0.000s

With strace i can see that the following steps are performed:

  • Read /etc/host.conf (probably) to determine the order of how host lookups should be done
  • Read /etc/hosts (in my opinion this should be enough, instead it is ...)
  • Connecting to the DNS server specified in /etc/resolv.conf
  • Read /etc/hosts again

The iptables setup on that machine prevents it from connecting to any DNS server. So it is - by design - blocked at this point. My expectation is, that this step would not be necessary with the following configuration:

/etc/host.conf

# This keyword specifies how host lookups are to be performed. It
# should be followed by one or more lookup methods, separated by
# commas.  Valid methods are bind, hosts, and nis.
#
order hosts, bind

/etc/hosts

# IPv4 and IPv6 localhost aliases
127.0.0.1       rt01.domain.net rt01 localhost
<public ip>     rt01.domain.net rt01
::1             localhost

/etc/conf.d/hostname

# Set to the hostname of this machine
hostname="rt01"

Can somebody explain to me, why hostname -f tries to connect to a DNS Server. I thought the information provided by /etc/hosts should be enough.

1 Answers1

3

The order in which DNS queries are resolved is based on the contents of /etc/nsswitch.conf. Specifically, the order of the options on the line for hosts

For example, this line will cause your machine to query a server before checking local files:

hosts:          dns files

Man page: http://man7.org/linux/man-pages/man5/nsswitch.conf.5.html

zymhan
  • 1,351
  • 1
  • 14
  • 30
  • 2
    Deleting dns (so that "hosts: files" remain) solved my issue. Thank You very much. –  Mar 28 '16 at 17:00