27

For some reason, two days ago my hosts file started being ignored.
I'm using Mac with Sierra OSx . This is the current content of the /etc/hosts file

##
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhostage
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost

127.0.0.1 localkur.com

Pinging localkur.com returns

ping: cannot resolve localkur.com: Unknown host

I have restart the system, clean the cache using these commands

sudo killall -HUP mDNSResponder;
sudo discoveryutil mdnsflushcache;
sudo discoveryutil udnsflushcaches;

Reopen the file in different text editing programs, vm, sublime etc.
Double checked if space or tab (or double space or double tab) - tested with all possibilities.
Also monitor the file with this command

 sudo fs_usage | grep "/etc/hosts"

And got that result when refreshing the browser or saving it with sublime

12:23:35  open              private/etc/hosts 0.000019   Google Chrom
12:23:36  open              private/etc/hosts 0.000019   Google Chrom
12:23:41  stat64            /private/etc/hosts 0.000022   Sublime Text
12:23:43  stat64            /private/etc/hosts 0.000019   Sublime Text
12:23:45  stat64            /private/etc/hosts 0.000019   Sublime Text
12:23:45  open              /private/etc/hosts 0.000019   Sublime Text

What am I missing ?
Why is this file ignored ?
How to enable it again ?

6
  • What are checked lines in System Preferences > Network > Advanced ... > Proxies? Anything special?
    – user599107
    Mar 22, 2017 at 11:07
  • the file might be corrupted/damaged, it has to be an ascii file - why don't you recreate it?
    – 13dimitar
    Mar 22, 2017 at 11:15
  • @FarazX there is "Bypass proxy settings for these Hosts & Domains:" and the value is "*.local, 169.254/16"
    – T1000
    Mar 22, 2017 at 12:16
  • @13nilux how to recreate it properly ?
    – T1000
    Mar 22, 2017 at 12:16
  • with nano - move do old file to /etc/oldhosts, create a new file with nano, populate it with the values of the old file and save it as /etc/hosts
    – 13dimitar
    Mar 22, 2017 at 12:29

8 Answers 8

13

The file might be corrupted/damaged, it has to be an ASCII file. You should recreate it:

  • move the old file with mv -f /etc/hosts /etc/oldhosts
  • create a new file with nano
  • populate it with the values of the old file
  • save it as /etc/hosts

That should do it.

3
  • 1
    Thanks, that was the problem in my case! The file was looking totaly normal but it was apparently with changed encoding or something else. It was visible by 'cat /etc/hosts' because cat wasn't listing the whole content of the file, just last line....
    – T1000
    Mar 22, 2017 at 15:42
  • 27
    It'd be wise to check file -I /etc/hosts output first. If it outputs charset=us-ascii, this answer won't work for you.
    – MCCCS
    Jul 20, 2018 at 9:32
  • This is not exactly what I need, but maybe I'm doing some other formatting wrong or something. Will keep looking....
    – wogsland
    Sep 15, 2020 at 8:45
19

Look at System Preferences -> Network

If the currently active "Location" has one or more DNS servers listed then the system will create the file "/etc/resolv.conf" which will list those nameservers as the place to go for resolution, bypassing /etc/hosts entirely

resolv.conf is created dynamically by "Network" prefs, and overwritten based on the "DNS Server" field of the currently active "Location" or removed if that field is blank

6
  • This resolved the issue for me. Thanks for posting this solution.
    – PhilR
    Apr 10, 2018 at 19:06
  • 1
    My system uses DHCP to receive the DNS resolver, and has an /etc/resolv.conf file. How can I override normal DNS query to block popunders? My intent was to add lines to /etc/hosts.
    – jetset
    Jul 30, 2018 at 21:31
  • How do you plan to distinguish between valid DNS queries and popunders? Sounds like it might be a browser level problem. Might it be better served by a browser solution like "Privacy Badger" or "uBlock Origin"?
    – BenL
    Aug 1, 2018 at 12:22
  • 7
    So what's the solution? Removing the DNS listed on the DNS tab. Nov 30, 2018 at 20:59
  • 2
    What is the solution? What are the explicit steps?
    – mathtick
    Jan 21, 2021 at 22:16
3

This is an old thread for sure but I was facing the same problem today and I found the solution. Leaving this here in case someone else needs it.

sudo dscacheutil -flushcache

Flushing the DNS cache did the trick in my case.

2

Also, make sure your list of hostnames doesn't exceed 20 names. I frequent a site which likes to put advertisements in your face, so I assign the FQDN from the popup as an alias of localhost. Recently I found my hosts file wasn't working. Seems the limit is 20 hosts... Example:

127.0.0.4 www.slutroulette.com static.exoclick.com www.gameskwala.com www.thefreecamsecret.com www.slutroulette.com cdn1.adexpert.com cdn2.adexpert.com cdn3.adexpert.com cdn3.adexperts.com abine.com www.abine.com e1.static.hoptopboy.com.rncdn1.com gameinvite.24trk.com main.exoclick.com 0427d7.se syndication.exoclick.com 0427d7.se gamingruff.com www.gamingruff.com mackeeperapp.zeobit.com 
127.0.0.5 ads.adplxmd.com mackeeperapp3.mackeeper.com onclickads.net onclick.net fundownloads108.com pussl10.com rumorsleague.com kitsfilesdll.com pussl8.com nowsetup.enterworldofupgrading.review playgr8.com autoupdate.thebigandgoodfree4upgrading.trade om.elvenar.com www.hitcpm.com www.ibtimes.co.uk mysagagame.com
1

It could be use of TAB instead of SPACE. Was in my case. Fixed it nicely!

2
1

/etc/hosts must have unix-style line endings.

In my case I've used Sublime and saved /etc/hosts with default Mac OS style. It was the reason of problem.

Solution taken from here: https://stackoverflow.com/a/42265844/1331420

1

I had the same issue and tried everything like flushing cache or re-creating the file but nothing worked. Eventually I found out that read permissions were not correct.

The correct permissions should be:

$ ls -ld /etc/hosts        
-rw-r--r--  1 root  wheel  623 Aug  6 11:27 /etc/hosts

For some reason group and other did not have read access in my case. Adding this worked:

$ sudo chmod g+r /etc/hosts
$ sudo chmod o+r /etc/hosts

So, if you need to use sudo to read the file via cat /etc/hosts this most likely is your problem.

3
  • -rw-r--r-- looks like other and group already have read permission, no?
    – oarfish
    Dec 4, 2020 at 9:44
  • @oarfish The first grey box shows what we want, NOT the state prior to the chmod commands. That's why I put "The correct permissions SHOULD be" above it.
    – Timo Ernst
    Dec 30, 2020 at 13:10
  • This is what ended up working for me. What's strange is that this was on a brand new MacBook (running 12.1) so I'd assume the stock configuration would have a functioning hosts file.
    – Mattia
    Feb 2, 2022 at 12:49
0

For me the issue was pasting the details in. Flushing the cache, etc didn't work.

Removing the original entry and manually typing everything in then worked 1st time!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .