3

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?

cwd
  • 2,693
  • 9
  • 32
  • 47

2 Answers2

5

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.

Jason Berg
  • 18,954
  • 6
  • 38
  • 55
  • 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 01 '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 01 '11 at 19:57
3

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

Alex
  • 7,789
  • 4
  • 36
  • 51
  • perhaps **absolutely should** is what you mean? Can you provide a link to some supporting documentation? – cwd Sep 01 '11 at 18:54
  • 1
    @Alex Depends on your OS. Some handle this resolution elsewhere. – Shane Madden Sep 01 '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 01 '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 01 '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 01 '11 at 19:15