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

share|improve this question
    
Hard to know the problem. When accessing the site via hostname (dev.julianfernand.es) fails, can you access it via ip address (192.168.1.100) ? – Panther Sep 18 '13 at 16:09
    
@bodhi.zazen Yes, I can. However since our company doesn't uses the default "access IP to access website" idea from shared hosts, this doesn't work. – Julian Fernandes Sep 18 '13 at 16:17
    
I was not suggesting you access websites via ip as a long term solution. The information helps debugging though as it excludes other problems from firewall to routing to server side issues. – Panther Sep 18 '13 at 17:31
    
@bodhi.zazen It makes sense :) – Julian Fernandes Sep 19 '13 at 10:29
    
What about if you type the ip address in the address bar of your browser ? – Sergiy Kolodyazhnyy Aug 12 '15 at 2:04

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

share|improve this answer
1  
Thanks for the reply! Sadly this did not work :/ – Julian Fernandes Sep 18 '13 at 17:04
    
Please also check your Nginx configuration and error log file. There must be some clue in there when you access the site. – Arnold Sep 18 '13 at 17:25

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!

share|improve this answer
1  
You probably want files as the first entry if you want /etc/hosts to take precedence over results from DNS servers. – muru Apr 23 '15 at 21:30
    
That is strange but it is not a mistake, I have to have exactly this order to make it work. – jmarceli Apr 23 '15 at 21:32

The following worked for me: add

addn-hosts=/etc/hosts

in

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

kill dnsmasq and

service NetworkManager restart
share|improve this answer

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.

share|improve this answer
1  
i do not have any dnsmasq file in specified path. I am on linux 15.04 – Hiru Aug 1 '16 at 16:20

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|improve this answer
    
Tried this on 13.10 and elementaryOS, still nothing. But thanks for the help! :) – Julian Fernandes Sep 19 '13 at 10:30
    
@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. – Mark E. Haase Feb 18 '14 at 21:48
    

Simple and Updated

  1. Create /etc/NetworkManager/dnsmasq.d/hosts.conf.
  2. Put lines like address=/whatever/1.2.3.4 in it. See the docs (look for --address). Wildcards are possible: address/.whatever./1.2.3.4.
  3. Kill dnsmasq (bug).
  4. Restart it: $ service network-manager restart.
share|improve this answer

It sounds like your setup is a lot like mine. I have a box running Ubuntu as my office server and development host. On that box, I run Nginx, Apache, Tomcat, Rails apps and whatever else I need.

From my Mac, I can simply add a hosts entry and load the server with whatever name I need, but from a ElementaryOS client, that doesn't work.

I tried the fixes above, with no success.

What I did was to run Squid Proxy Server and add the hostnames to /etc/hosts on the server. (Editing requires a restart of squid.)

After that, make the appropriate proxy settings in your browser or OS's control panel.

share|improve this answer

Check the permissions on /etc/hosts file.

On my cloud service after cloning a server the permissions on hosts file changed from 644 to 600 so the file could not be read by apache (www-data) I guess. I ran sudo chmod 644 hosts from /etc and that fixed it.

The problem started out as:

MongoConnectionException Failed to connect to: localhost:27017: Previous connection attempts failed, server blacklisted. 

I tracked it down to the server variable in MongoClient pointing at localhost. I was unable to ping localhost or the hostname.

share|improve this answer

Your Answer

 
discard

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.