Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I just came across a server that does not respond to things like ping localhost or mysql -h localhost - I believe this is because localhost is not defined in the hosts file.

On just about every machine I've used localhost has been an entry in the hosts file. Is there any good reasons not to define it - like possible security concerns? Or is this something that probably should be configured by default and perhaps is just missing?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Yes it's good. As you've already found out, there are certain programs (like mysql) that expect to be able to connect to localhost. Without that entry in your hosts file, programs cannot resolve that name to the proper IP address (in this case, the loopback address of 127.0.0.1).

There may be different ways of resolving localhost sometimes, but really, why would you not want to put it in there? There is no good reason not to.

share|improve this answer
    
Yes, that's what I'm asking. Why would you not want to put it in there? I just want to know if there is any good reason before I ask the sysadmin to do it. So no? –  cwd Sep 1 '11 at 19:55
1  
No. There is no good reason not to put it in there. In fact, I can't even think of a bad reason to leave it out. Leaving it out does nothing except break the system. –  Jason Berg Sep 1 '11 at 19:57

I believe it absolutely should be defined in the hosts file.

share|improve this answer
    
perhaps absolutely should is what you mean? Can you provide a link to some supporting documentation? –  cwd Sep 1 '11 at 18:54
1  
@Alex Depends on your OS. Some handle this resolution elsewhere. –  Shane Madden Sep 1 '11 at 18:56
    
@Shane Madden Good point, it depends on nsswitch.conf too, it's possible to define localhost not in the hosts file but it should be defined somewhere and I believe default approach is to define it in /etc/hosts –  Alex Sep 1 '11 at 18:59
    
@cwd Yeah, absolutely should would be better since there are many OSes in the world besides Linux, I will edit my answer. –  Alex Sep 1 '11 at 19:05
1  
@cwd Regarding the supporting documentation, here is a quotation from /etc/hosts on CentOS: # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost –  Alex Sep 1 '11 at 19:15

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.