25

I added some domains to block (127.0.0.1 mydomain.com) to my /etc/hosts file and for some reason, Safari seems to be ignoring them.

I have tried:

  • disabling Safari extensions,
  • clearing the cache (sudo dscacheutil -flushcache),
  • restarting Safari and the Mac altogether.

Chrome respects the /etc/hosts changes, but Safari does not. Attached is a sample of my /etc/hosts file.

I am running Safari 6.0.4 with Mountain Lion 10.8.3

Any ideas?

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
127.0.0.1       facebook.com
  • 3
    If there is no response, Safari will try to add www. To the beginning of the URL. Facebook can then redirect the browser to Facebook.com. Try adding www.facebook.com to your file and see if that works. – JoshRagem May 30 '13 at 14:43
  • I thought this might be the case. I tried both www.facebook.com and *.facebook.com. Neither worked. – Nicholas Tulach May 30 '13 at 15:35
  • When I saved the text in the code block as /etc/hosts, it did block Facebook in Safari. Does your /etc/hosts use CR line endings, or have you tried moving other entries above the IPv6 entries? – Lri May 30 '13 at 18:37
  • What's interesting is that the line i use for redirecting search.yahoo.com to DuckDuckGo works, even if i put it after the redirect for facebook.com, but the Facebook line doesn't do anything. Totally stumped on this one. – Nicholas Tulach Jun 3 '13 at 12:55
  • did you flush the dns cache and clear the safary history and cache- if you don't know how to flush the dns cache you can also restart your mac. – konqui Jun 25 '14 at 21:53

13 Answers 13

13

I had a slightly different version of the same problem, and thought I'd mention what worked for me.

I develop websites. To do that, I have a complete copy of each site running on my local network. When I wish to work locally, I have always just added an entry for "example.com" to my local /etc/hosts file, and that has always overridden DNS, making it possible for me to access the local copy of the website in any browser, including Safari and Firefox. I can work without affecting the actual live site, then upload changes when they're complete.

Recently, however, this technique stopped working for both Safari and Firefox, but not for Chrome, so for a while I was limited to using Chrome for local development. I searched google and tried all the suggested fixes -- CR at the end of the hosts file, only one entry per line, various Firefox configuration changes using "about:config", etc. Nothing worked.

Then I tried this simple thing: in addition to putting the IPV4 version of the address in the /etc/hosts file, I also put the IPV6 version.

Before:

10.0.1.23       example.com

After:

10.0.1.23       example.com
0:0:0:0:0:FFFF:0A00:0117        example.com

As soon as I added the IPV6 entry, both Firefox and Safari started hitting the correct (local) server.

There are various websites that will translate IPV4 addresses to IPV6; just do a google search on "ipv4 to ipv6".

  • Could you check the last character of your /etc/hosts is a newline? For example open it with vi. – dan Nov 2 '16 at 18:27
  • The last character of my /etc/hosts file is "\n", or hex 0A. – Fred Nov 4 '16 at 20:22
  • 1
    This is the correct answer. To prevent Safari, you'll need both IPv4 and IPv6 entries. Tested in MacOS 10.12. – alttag Feb 8 '17 at 23:49
  • ipaddressguide.com/ipv4-to-ipv6 - Both add 2 lines + remove very long lines - separate them - its works! – Artemiy StagnantIce Alexeew Jul 25 at 10:09
8

I had this same problem and it was caused by having really long lines in my hosts file (multiple hosts mapped to the same IP address listed on the same line). I fixed it by splitting this into several lines.

7

I spent quite some time on OS X El Captain 10.11.4 to block sites using /etc/hosts file instead of using Parental Control in System preferences. At the end it simply worked like this:

127.0.0.1 www.website.com
127.0.0.1 website.com

I tried a lot of variants, but I was always mapping to 0.0.0.0 (0.0.0.0 website.com). That never worked in any variation.

Unlike some suggestions on the web, these were useless:

  • There was no need for IPv6 blocking like fe80::1%lo0 www.website.com or ::1 website.com
  • There was no need for dscacheutil -flushcache, but just do it in case.
  • It does not matter if you paste it at the beginning or end of the hosts file
  • Browsers seem to have been responded differently: Chrome and Safari blocked it immediately once the correct hosts file was saved, Firefox kept ignoring it for a while (not clear if restarting it was enough).
    • Try to use New Private Window or restart the browser for testing it.
  • Do not add http:// or https:// at the beginning of the address
  • No other commands were necessary.
  • Do not disable (= comment out by adding a # at the beginning) lines that are in the hosts file already.

Useful checks

  • Check if the system recognizes your redirection dscacheutil -q host -a name website.com - it should display:

    name: website.com
    ip_address: 127.0.0.1
    
  • Check if the syntax of the hosts file is correct by: cat -vet /etc/hosts. This shows invisible characters:

    • Lines should end with $
    • Between 127.0.0.1 and website.com should be only a space or a tab, which is displayed as ^I.
  • Note that for some sites, like facebook for instance, you might need to block a lot of addresses.
  • I think that the hosts file should end with a newline.
6

I had a similar issue. Every piece of software on my Mac honored my /etc/hosts file entry when my hosts file was symlinked (aliased) to another file, except Safari. When your hosts file is a symlink, Safari ignores it.

My solution was to make /etc/hosts a hard link. Luckily the hosts file I keep up to date is in the same file system. If it isn't, you're borked.

Bad Safari... bad.

  • 1
    I have been considering a career change - until I found this. Thought I’d be really smart and store all of my dotfiles (and other configuration files) in a git repository, and then create symbolic links. Have been pulling what‘s left of my hair for several hours today. – localheinz Feb 8 '18 at 18:42
  • Thank you, you've saved me hours of frustration! Unbelievable Safari! – DrMeers Jul 19 '18 at 4:33
4

The only thing that works for me (10.12.1 "Sierra"):

127.0.0.1   example.com
127.0.0.1   www.example.com
fe80::1%lo0 example.com
fe80::1%lo0 www.example.com
  • 2
    This is the correct answer. You must add domain.com and www.domain.com in host file. Safari can add www. in your request and will not match with domain.com. – Vagner Nov 9 '16 at 11:01
3

Worked for me on Yosemite:

  1. Go to System Preferences > Network > Advanced > Proxies
  2. Tick Auto Proxy Discovery, hit OK and Apply (I haven't got anything else ticked)
  3. Enter in Terminal: dscacheutil -flushcache

This flushes the dnscache in Yosemite. Now Safari and Chrome should respect your hosts file.

  • I wanted to try this one, but I'm tethering with my iPhone and the Advanced > Proxies option is not available. – Roger Dueck Feb 22 '17 at 4:34
2

Try putting one or two carriage returns after the last entry.

...
127.0.0.1       facebook.com

^ Carriage Return
  • 1
    unfortunately this didn't help. – Nicholas Tulach Jun 3 '13 at 13:03
  • You have to have a newline (not a carriage return) at the end of every line, including the last one. – ganbustein Dec 23 '14 at 4:25
  • Unless he's running Mac OS 9. :o – William T Froggard Oct 14 '15 at 4:40
1

In some cases, loopback addresses need to be added for both IPv4 and IPv6 in the /etc/hosts file.

Say we already added a blocking entry for the IPv4 address of website.com:

127.0.0.1 website.com

If dscacheutil -q host -a name website.com returns addresses for both protocol versions:

name: website.com
ipv6_address: rand:omin:vali:dipv:6addr:ess5

name: website.com
ip_address: 127.0.0.1

then we need to add another line into the hosts file:

::1 website.com
0

I had wrong line ending. It has to have LF, I had CR.

0

I had this problem too but the solution is really easy Let's suppose you create an Alias in the host file to your machine with the name localhost2

That entry in the host file should look like this: 127.0.0.1 localhost2

When you type "localhost2" in the safari url bar you will notice in the dropdown that the default option is to search that in google, you should select the option saying "Go to Site localhost2"

0

Make sure your /etc/hosts has exactly these attributes. I was copying, modifying and replacing the file using my own userid, every other program kept working except safari...

$ ls -l /etc/hosts
-rw-r--r--  1 root  wheel  1004 Sep 24 16:03 /etc/hosts
0

For an https address example.com, I had to include both IPv4 and IPv6 addresses for both example.com and www.example.com before it worked. Like this:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       example.com
::1     example.com
127.0.0.1       www.example.com
::1     www.example.com
255.255.255.255 broadcasthost
::1             localhost
-2

According to this blog http://thecoredump.org/2011/09/editing-the-hosts-file-in-mac-os-x-lion/

You have to put the entry at the start of the host file. Very unorthodox. Haven't any personal experience of this though.

  • They are wrong. Only syntax matters, not location. And all additional entries should always follow the defaults. – user10355 Jul 17 '13 at 14:56
  • They probably say to put it at the beginning to avoid a common pitfall of people editing the hosts file: they forget to put a newline at the end of the last line. Without that newline, the last line will be ignored. If you add the entry to the front, it's hard to forget the newline. – ganbustein Dec 23 '14 at 4:23
  • Not sure why, but this works for me. I'm using yosemite. – Vicary Jan 9 '15 at 15:23

You must log in to answer this question.

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