Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

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

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

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

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

  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.e foo.myOtherSite.com        # myOtherSite testing environment

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

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
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). –  TAFKA 'goldilocks' Nov 26 '13 at 18:14
    
But the gethostbyaddr() thing they raise is significant. +1 –  TAFKA 'goldilocks' Nov 26 '13 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 '13 at 19:23
1  
@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 '13 at 20:01
    
@slm That sounds like a very good option for a production environment. Probably a valuable learning project too. –  AlexMA Nov 26 '13 at 20:22
add comment

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|improve this answer
    
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 '13 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 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. –  TAFKA 'goldilocks' Nov 26 '13 at 18:20
    
In fact I've edited this to take into account "a potential problem"... –  TAFKA 'goldilocks' Nov 26 '13 at 18:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.