8

My hosts file is being ignored, apparently.

It contains:

127.0.0.1 localhost.localdomain localhost
127.0.0.1 mydomain.com
127.0.0.1 nowhere

If I ping localhost or 'nowhere', I get an 'unknown host' error. If I ping mydomain.com, ping succeeds, but it reports the internet ip address (from an outside name server) for mydomain.com.

The server is running ubuntu hardy. Is there a way disable (and reenable) the /etc/hosts file?

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
Rdbhost
  • 293
  • 1
  • 2
  • 8

5 Answers5

11

I found the problem.

The /etc/hosts file had overly restrictive permissions -rw------ (I believe that is 600 octal). Changing that to mode 644 fixed the problem.

My tests with ping were done with ordinary priv, and the hosts file was unreadable. My original problem was getting postgresql to startup properly; it starts with root privilege, but apparently sheds priv before starting its child processes for autovacuum and such, so by the time it needed a domain resolved, it could not read the hosts file.

Thanks to everybody who replied. It has been enlightening.

Rdbhost
  • 293
  • 1
  • 2
  • 8
  • 1
    This saved my day. for me postgres service was resolving `localhost` using DNS and was getting wrong answer. Nowhere it was reported anything about permissions on `/etc/hosts`. changed permissions to `chmod 644 /etc/hosts` and magic happens! – Anto Aug 14 '19 at 07:32
10

Check /etc/nsswitch.conf

You should have a line like:

hosts: files dns

if the "files" part is missing, /etc/hosts won't be checked.

James
  • 7,553
  • 2
  • 24
  • 33
  • This is the answer that pointed me in the right direction. For some reason, after I updated my OS, it changed the ordering of my hosts: line. Cheers – Savlon Jan 01 '22 at 08:20
4

the hosts file is strange sometimes on some distrib, try put all localhost alias in the same line

like this :

127.0.0.1 localhost.localdomain localhost mydomain.com nowhere
Gayell
  • 131
  • 4
3

I just wasted an hour because a copy'n'paste update to my /etc/hosts had a tab instead of a blank separating the ip and the name. So another thing worth checking if an entry is being ignored for apparently no reason.

xverges
  • 133
  • 4
1

Not sure of the specifics of ubuntu, but it seems like your local resolver isn't configured properly. You should try man resolv.conf or man nslookup to see how to configure it.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
atk
  • 217
  • 1
  • 3