Why is the /etc/hosts file not working?

Solution 1:

In Ubuntu if you want to flush DNS cache, you need to restart nscd daemon.

Install nscd using the following command:

sudo apt-get install nscd

Flush DNS Cache in Ubuntu Using the following command:

sudo service nscd restart

OR

sudo service dns-clean start

Reference: http://www.upubuntu.com/2012/05/how-to-flush-clear-dns-cache-under.html

Solution 2:

For me the solution was to edit /etc/nsswitch.conf file (you may use command sudo vim /etc/nsswitch.conf). I've changed line:

hosts:          files mdns4_minimal [NOTFOUND=return] dns

to:

hosts:          dns files mdns4_minimal [NOTFOUND=return]

and now it is working as expected!

Solution 3:

The following worked for me: add

addn-hosts=/etc/hosts

in

/etc/NetworkManager/dnsmasq.d/hosts.conf

kill dnsmasq and

service NetworkManager restart

Solution 4:

The accepted answer works in 12.04 through 13.04 by disabling dnsmasq, but it stopped working for me in 13.10. I found the following new solution for 13.10.

Edit your /etc/default/dnsmasq and change ENABLED=1 to ENABLED=0 and restart.