Why does dns resolution always resolve the first IP address when there are multiple entries configured for the same domain/host in /etc/hosts?

Solution Verified - Updated -

Environment

Red Hat Enterprise Linux(all versions)

Issue

  • When multiple entries are present in /etc/hosts for the same domain/hostname, always local DNS resolution resolves the hostname to the first IP address.
  • Is there a way to configure round-robin fashion DNS local resolution when there are multiple entries for the same host?

Resolution

  • Ordering of lookup results is determined by the rules of RFC3484 along with any customisation provided by the /etc/gai.conf configuration file.
  • by default, any matching entries from /etc/hosts will be used in the above procedure
  • if the ordering rules do not prioritise any specific entry then the first will be used
  • if a specific match from /etc/hosts is required then an additional, unique name should be used.

Root Cause

Ordering of lookup results,whether from /etc/hosts or getaddrinfo() calls, is affected by multiple factors including

  • settings in /etc/nsswitch.conf
  • settings in /etc/host.conf
  • ordering in the initial results
  • rules specified by RFC3484
  • customisations specified in /etc/gai.conf
  • network topology and relation to local addresses

Depending on how the above play out it may be the first, the last or some other entry that is returned first.

Diagnostic Steps

Ping/nslookup the hostname.

# ping rhel7
PING rhel7 (10.0.0.10) 56(84) bytes of data.
64 bytes from rhel7 (10.0.0.10): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from rhel7 (10.0.0.10): icmp_seq=2 ttl=64 time=0.034 ms
^C
--- rhel7 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.023/0.028/0.034/0.007 ms
[root@rhel7 network-scripts]# ping rhel7
PING rhel7 (10.0.0.10) 56(84) bytes of data.
64 bytes from rhel7 (10.0.0.10): icmp_seq=1 ttl=64 time=0.018 ms
^C
--- rhel7 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.018/0.018/0.018/0.000 ms

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments