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?

3 Answers 3


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: localhost.localdomain localhost somenode.somedom.com somenode

Will not do what you are talking about. BUT having: somenode.somedom.com somenode 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.

  • 2
    I don't observe the effects noted by either party in that thread. E.g., multiple lines for do not affect the output of hostname (contra the OP), and all of them resolve correctly (contra the respondent).
    – goldilocks
    Nov 26, 2013 at 18:14
  • 1
    But the gethostbyaddr() thing they raise is significant. +1
    – goldilocks
    Nov 26, 2013 at 18:45
  • 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.
    – AlexMA
    Nov 26, 2013 at 19:23
  • 2
    @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!
    – slm
    Nov 26, 2013 at 20:01
  • @slm That sounds like a very good option for a production environment. Probably a valuable learning project too.
    – AlexMA
    Nov 26, 2013 at 20:22

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.

  • 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.
    – AlexMA
    Nov 26, 2013 at 17:59
  • 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 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
    Nov 26, 2013 at 18:20
  • In fact I've edited this to take into account "a potential problem"...
    – goldilocks
    Nov 26, 2013 at 18:44

Apache 2.4 refused to start on my Unix system. The root cause was duplicate lines in /etc/hosts. After I removed the duplicate line I was able to start the web server.

  • 8
    This is useful info, but should probably be a comment rather than an answer according to this site's guidelines.
    – AlexMA
    Feb 14, 2018 at 21:25
  • They don't have enough reputation to comment. Apr 22, 2023 at 20:27
  • Then they should abstain from posting this; using the "Post your answer" button to circumvent the reputation requirement is an attempt to work around the privilege system, and as such (strictly speaking) a mod-flaggable offense.
    – tripleee
    Nov 25, 2023 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .