Hosts file: Is it incorrect to have the same IP address on multiple lines?

I found this thread that discusses doing something along these lines. The thread is pretty adamant about not having multiple lines line the /etc/hosts file.

excerpt - Re: /etc/hosts: Two lines with the same IP address?

No, it will not. The resolvers stop at the first resolution. Having something like:

   127.0.0.1 localhost.localdomain localhost
   127.0.0.1 somenode.somedom.com somenode

Will not do what you are talking about. BUT having:

   127.0.0.1 somenode.somedom.com somenode
   127.0.0.1 localhost.localdomain localhost

Will cause all kinds of havoc. Including forwarding.

I would generally not do what you're attempting. If you need more evidence the man page even says not to do this:

excerpt man hosts

This manual page describes the format of the /etc/hosts file. This file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information:

         IP_address canonical_hostname [aliases...]

All this being said, if your hostnames are FQDN and they don't overlap then you're probably safe to do what you're doing. Just keep in mind that if there is any overlap such as what was mentioned in the thread above, then you may run into resolving issues.


I believe the third method has been working fine; is there a problem with this?

I've always done that a little bit, but there is a potential problem since according to man gethostbyaddr that system call may use /etc/hosts to associate an IP address with a name. Although the much more common case is the other way around (get address from name), be aware of this in case anything funny happens.

Tags:

Hosts