/etc/hosts alias or multiple record

I want to know what is different between this two configuration :
First :       localhost my-hostname   host-a a.com

Second :       localhost my-hostname   host-a   a.com

What happened if i do not use aliases ?

Asked By: mah454


For the /etc/hosts file these two alternatives are equivalent when resolving an address for a name. In both cases the names host-a and a.com will be resolved as the name host-a with address

For a reverse lookup of name from IP address the two alternatives are slightly different. Both will return host-a as the canonical name. The first will include a.com as an alias.

You will get the first line of the file that matches, and the first text entry on that line is the canonical name.

Test framework (modify as necessary to test various scenarios):

perl -MData::Dumper -e 'print Dumper(gethostbyname("a.com"))'

perl -MData::Dumper -e 'print Dumper(gethostbyaddr(pack("C4", 192, 168, 10, 12), 0))'

Personally, I try to avoid using /etc/hosts, preferring to use DNS. However there are some cases where it can be useful and in these situations I always put the FQDN first on a line and its alias(es) afterwards. I do the same by line in the file for actual machine name and its services.

Example, where eleven is the server’s name and it provides the web and ftp services: eleven.contoso.com eleven web.contoso.com web ftp.contoso.com ftp
Answered By: Chris Davies
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.