33

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

  • 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
  • What about if you type the ip address in the address bar of your browser ? – Sergiy Kolodyazhnyy Aug 12 '15 at 2:04

10 Answers 10

14

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

| improve this answer | |
  • 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
6

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!

| 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
6

The following worked for me: add

addn-hosts=/etc/hosts

in

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

kill dnsmasq and

service NetworkManager restart
| improve this answer | |
  • makes no difference, the Name Server switch is the one that controls the order in which host name resolution is determined. – Aurovrata Feb 20 at 6:38
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.

| improve this answer | |
  • 2
    i do not have any dnsmasq file in specified path. I am on linux 15.04 – Hiren Aug 1 '16 at 16:20
  • There is no accepted answer anymore. Which answer are you talking about? – toon81 Sep 8 '18 at 23:57
3

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

| improve this answer | |
1

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.
| improve this answer | |
0

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.

| improve this answer | |
0

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.

| improve this answer | |
  • I've sometimes had success changing 'localhost' to '127.0.0.1' in these sorts of cases – pbr May 16 '18 at 3:26
0

Edit /etc/nsswitch.conf, comment out below line by adding # in front of the line

hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname

and add

hosts:          files mdns4_minimal [NOTFOUND=return] dns

Basically, configuration is reordered. Now domain lookup process will consult file first which is /etc/hosts and then it will consult DNS. With default configuration it consults DNS first before any other appropriate services or files.

For quick test whether its working or not you can use

sudo python -m SimpleHTTPServer 80

to create a simple HTTP Server to serve files from directory and then comment below line in /etc/hosts file

127.0.0.1      localhost
127.0.1.1      01hw730983

and add

127.0.0.1       content

then go to a browser and type content/, if you able to see Directory structure its working else its not.

| improve this answer | |
0

It's silly but the browser cache is the 'guilty' in my case = should be obvious for a developer :(

Another silly (obvious) thing what I missed, Browsers (Chrome, Mozilla) perform a google (or search engine) search directly from the address bar, so instead of resolving the address what you gave they search that word on the internet!

Solution:

  • Once the /etc/hosts file is edited ping the chosen address! If the ping command resolves the IP what you have given the 'hosts' file works!

  • Clear your browser's cache (browsers cache DNS resolutions for some time (TTL))

  • Disable the default search engine of the browser you are using

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.