Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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
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
@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
@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. localhost.localdomain localhost – Alex Sep 1 '11 at 19:15

Your Answer


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.