Multihomed hosts and /etc/hosts

December 23, 2007

As a side note to looking up hostnames from IP addresses for people who use /etc/hosts, note that /etc/hosts lookups work badly in the presence of hosts with multiple IP address, since most gethostbyname() implementations will only return the first IP address that they find in /etc/hosts. These days you really want a minimal /etc/hosts and a reliable DNS server, unless you have special concerns.

(The gethostbyname() behavior is sensible, since otherwise it would always have to scan the entire /etc/hosts file just to make sure that it had found all IP addresses for a host, even when most hosts only have one IP address.)

While there are workarounds for this issue, I think that the best way out is just to not have any entries for your multihomed hosts in /etc/hosts, even on the hosts themselves. This appears to work fine on at least modern Linuxes, and I can't imagine that the *BSDs do any worse here.

(You can have similar behavior with gethostbyaddr(), depending on how you give an IP address multiple names in /etc/hosts. Putting all the names on one line works out, but having multiple lines for one IP address has the same problem.)

Written on 23 December 2007.
« Shortening hostnames for fun and profit
The difference between shells that do job control and shells that don't »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sun Dec 23 23:41:18 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.