2007-12-23
Multihomed hosts and /etc/hosts
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.)
Shortening hostnames for fun and profit
Once upon a time I needed to NFS export filesystems to a lot of
workstations, in a situation where I was worried about size limits
in /etc/exports
(and we didn't use YP/NIS, so we couldn't just put
everything in netgroups). In situations like this, one thing to do
is to shrink hostnames down as much as possible, and that's what we
did.
(This was back in an era where the existence of such limits were at least plausible.)
First, we named the workstations after elements.
This let us make their canonical names in DNS be the short abbreviations
for each element (although the local hostname was still the friendlier
element name), meaning that workstations had a canonical hostname
that was only one or two characters long. Then we put them all in
/etc/hosts
, using shortened names: their canonical hostname, plus only
the subdomain of their lab.
All of this gave us hostnames for /etc/exports
that were only four
to six characters long, far shorter than they normally would have been,
and I stopped worrying about the potential problem.
(In the end I don't know if the exports file actually had any size limits; possibly I did all of this work merely out of paranoia.)
Perhaps we could have done all of this without making the abbreviation
be the canonical name in the DNS, but I didn't feel like finding out the
hard way that mountd
did DNS lookups under some circumstances. And we
were clearly going to have the abbreviations in DNS, since having names
in /etc/hosts
that aren't in DNS is a recipe for future confusion and
explosions.