Why is the /etc/hosts file not working?

139,074

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.

Solution 5

From: http://blog.calebthorne.com/2012/08/broken-etchosts-in-ubuntu-1204.html

A new "feature" in Ubuntu 12.04 desktop edition is to use dnsmasq as a plugin to NetworkManager for local DNS. Dnsmasq is intended to speed up DNS and DHCP services but comes with one unfortunate side effect: dnsmasq caches local DNS and ignores changes to /etc/hosts. I make frequent changes to the hosts file while working on websites so this "feature" was quite annoying.

The solution is to disable dnsmasq in the Networkmanager configuration file. Open /etc/NetworkManager/NetworkManager.conf and comment out the line:

dns=dnsmasq

My NetworkManager.conf file contains the following:

[main]
plugins=ifupdown,keyfile
# dns=dnsmasq

[ifupdown]
managed=false

See also https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/993298

Share:
139,074
Julian Fernandes
Author by

Julian Fernandes

Updated on September 18, 2022

Comments

  • Julian Fernandes
    Julian Fernandes over 1 year

    I have been having problems with this for a while and have tried everything I know, so I figured it was finally time to ask for some help.

    Any edit I make to /etc/hosts just doesn't work.

    Example:

    julian@ifrit:~$ cat /etc/hosts
    127.0.0.1   localhost
    127.0.1.1   ifrit
    192.168.1.100   dev.julianfernand.es
    

    In the example above, when I access dev.julianfernand.es (this doesn't exist), it should load from 192.168.1.100.

    If I ping, it works just fine. However when I access dev.julianfernand.es using Google Chrome or Firefox, it doesn't.

    Now, after I restart a couple times, it works. But since I work at a managed WordPress hosting company, I deal with many situations where I have to edit my file to see the customer's website on our server.

    I just can't keep restarting my computer. It isn't productive at all. Restarting the networking service doesn't work, same for clearing cache (even internal Chrome DNS cache).

    Does anyone have an idea here? This happens with elementaryOS (based on Ubuntu 12.04) and Ubuntu 13.10 (daily). Haven't tried with any other version yet.

    PS: if this matter, I have a NGINX server running on this machine with PHP-FPM and MySQL.

    Thanks in advance :)

  • Arnold
    Arnold over 10 years
    Please also check your Nginx configuration and error log file. There must be some clue in there when you access the site.
  • Julian Fernandes
    Julian Fernandes over 10 years
    Tried this on 13.10 and elementaryOS, still nothing. But thanks for the help! :)
  • Mark E. Haase
    Mark E. Haase about 10 years
    @JulianFernandes It appears this doesn't work on 13.10. I followed this advice and it worked back on 12.04, but after upgrading to 13.10, dnsmasq is back and I can't get rid of it.
  • Panther
    Panther about 10 years
  • muru
    muru about 9 years
    You probably want files as the first entry if you want /etc/hosts to take precedence over results from DNS servers.
  • jmarceli
    jmarceli about 9 years
    That is strange but it is not a mistake, I have to have exactly this order to make it work.
  • Hiren
    Hiren almost 8 years
    i do not have any dnsmasq file in specified path. I am on linux 15.04
  • Mircea
    Mircea about 6 years
    I've sometimes had success changing 'localhost' to '127.0.0.1' in these sorts of cases
  • toon81
    toon81 over 5 years
    There is no accepted answer anymore. Which answer are you talking about?
  • Aurovrata
    Aurovrata about 4 years
    makes no difference, the Name Server switch is the one that controls the order in which host name resolution is determined.
  • B. Shea
    B. Shea over 3 years
    Yep - I think that's what we call a 'pro-tip'. haha - Always test /etc/hosts with ping.
  • schworak
    schworak over 2 years
    THANK YOU SO MUCH! I had one server picking up the host name from my router rather than from /etc/hosts and couldn't figure out why. After seeing your suggestion, I checked all the servers and they all had the same /etc/nsswitch.conf except for the one giving me trouble. I updated that one to match the others (almost identical to your suggestion) and my problem went away.
  • Pablo Bianchi
    Pablo Bianchi over 2 years
    Since 15.04 with systemd: sudo systemd-resolve --flush-caches