1

I've tried adding the following line to my hosts file in Windows 7:

127.0.0.1 nytimes.com

The hosts file is located in:

C:\Windows\System32\drivers\etc\

The idea is to block access to the New York Times website (and others).

Even after re-starting the machine, this does not block access to the site. Why not?

5 Answers 5

6

The site is www.nytimes.com, it's a different host name.

3

If the browser is configured to use a proxy, then it likely doesn't matter what's in the HOSTS files.

1
  • Moreover, using a proxy is the far better way to go about achieving the actual goal at hand, in any event.
    – JdeBP
    Aug 8, 2011 at 0:31
3

(Normally a restart isn't required for changes to the local HOSTS file to take effect.)

Your blocking technique may work better if you include both hostnames (and you can place more than one on a single line, or make several entries for the same IP address across multiple lines), one with the "www." prefix and one without (just as I'm showing here, except that I'm using "example.com" instead):

127.0.0.1 example.com www.example.com

The reason that blocking doesn't work as you expected is that the HOSTS file is very literal in the way it handles things, and you're probably using the "www." prefix variation of the hostname which I'm also assuming that you didn't define [since your example shows only one hostname without the "www." prefix].

If you were using a DNS server instead, then you could set up a zone for "example.com" and "www.example.com" would be included automatically (this is due to the hierarchial nature of DNS). The HOSTS file, on the other hand, will allow you to override IP addresses only for the hostnames you specify therein.

When resolving a hostname to an IP address, your computer normally consults the local HOSTS file first, followed by any local cache, and then queries DNS after that. This is done to preserve bandwidth, and it's very effective.

For a handful of computers, the local HOSTS file can be easy to manage, but for a large number of computers it's usually easier to set up a DNS server (with the zones you want to block added to resolve to nothing, or 127.0.0.1, or even an intranet server) and configure all the computers to use it.

1

I would recommend using browser plugin like AdBlock Plus to do this. It is a cleaner solution, ie. more likely to work, because AdBlock is designed to block elements and URLs within pages, whereas hosts file is not meant for use like this. Adblock Plus is available to both Firefox and Chrome, which are very much recommended for security reasons, too.

1
-2

There should be not a space but a tab between IP and host name:

127.0.0.1->example.com
6
  • Are you sure this matters? On other systems there is no difference between space and tab in a hosts file.
    – Raffael
    Aug 7, 2011 at 12:03
  • 2
    @Raffael: It doesn't matter. Spaces and/or tabs are acceptable delimiters. Aug 12, 2011 at 3:33
  • @Randolf, Raffael: That's the issue I ran by myself. Space was not working until I switched to tab Aug 12, 2011 at 4:38
  • I just tested this with a space in 64-bit Windows 7 (with Service Pack 1 and other updates installed), and it worked just fine. It also worked equally well with a tab. Removing the entry resulted in my temporary hostname not resolving (as expected). Is your editor inserting something different for the space, such as a unicode character instead of ASCII character #32, perhaps? This is odd, as it should work with spaces and/or tabs. Aug 12, 2011 at 4:43
  • 1
    @Randolf: That's exactly how I had it in my mind too. But I am using more Linux than Windows so I was not 100% sure. Thanks for the confirmation.
    – Raffael
    Aug 12, 2011 at 7:15

Your Answer

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