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

79,357

Solution 1

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.

Solution 2

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.

Share:
79,357

Related videos on Youtube

AlexMA
Author by

AlexMA

Updated on September 18, 2022

Comments

  • AlexMA
    AlexMA almost 2 years

    For a while I have been formatting my hosts file like this. Notice the same ip on two lines:

    e.f.g.h foo.mydevsite.com
    e.f.g.h foo.myOtherDevSite.com
    

    I read recently that aliases are supposed to be consolidated on one line:

    e.f.g.h foo.mydevsite.com foo.myOtherDevSite.com 
    

    However, I don't like this method because you can't easily comment out certain aliases or add comments to particular aliases, like this:

      a.b.c.d foo.mydevsite.com          # myDevSite on box 1
    # a.b.c.d foo.myOtherSite.com        # myOtherSite on box 1 
      a.b.c.d ubuntuBox                  
    
      e.f.g.h foo.myOtherSite.com        # myOtherSite testing environment
    

    So far this has been working fine; is there a problem with this?

  • AlexMA
    AlexMA over 10 years
    As I suspected, but wanted to be sure there doesn't seem to be great documentation on it. I was also unsure about whether there might be more issues with some extra-strict unix distributions. I will give you answer credit soon if nobody else chimes in with a different stance.
  • goldilocks
    goldilocks over 10 years
    I don't observe the effects noted by either party in that thread. E.g., multiple lines for 127.0.0.1 do not affect the output of hostname (contra the OP), and all of them resolve correctly (contra the respondent).
  • goldilocks
    goldilocks over 10 years
    Well, the point slm makes is worth considering; perhaps the system was not meant to be used this way after all. I'm going to keep doing it (n.b. I mostly just use this for 127.0.0.1 so I can transplant and test things locally without modifying a virtual host configuration), but I've added a caveat to the last paragraph.
  • goldilocks
    goldilocks over 10 years
    In fact I've edited this to take into account "a potential problem"...
  • goldilocks
    goldilocks over 10 years
    But the gethostbyaddr() thing they raise is significant. +1
  • AlexMA
    AlexMA over 10 years
    Very interesting! I did not realize a hosts file was used bidirectionally ( get host-name from ip, get ip from host-name). That definitely does bring up some issues. Maybe I should look into a hosts file build process which grabs a human readable version and then "corrects" it by concatenating host-aliases to one line. Should be a very easy script.
  • slm
    slm over 10 years
    @AlexMA - if you're finding that /etc/hosts is causing you grief you can always setup DNS. I run BIND for this exact reason, (1) b/c it's easier to centrally manage, and (2) I get the resolving the way I want from there without a lot of headaches. There are lighter options such as DNSMasq for doing this too. These systems can be used on a single box too!
  • AlexMA
    AlexMA over 10 years
    @slm That sounds like a very good option for a production environment. Probably a valuable learning project too.
  • AlexMA
    AlexMA over 6 years
    This is useful info, but should probably be a comment rather than an answer according to this site's guidelines.
  • paradroid
    paradroid almost 3 years
    These days Deban uses 127.0.1.1 as an address for other hostnames in /etc/hosts.