Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Aug 7 '11 at 11:54

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

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

share|improve this answer
add comment

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

share|improve this answer
    
Moreover, using a proxy is the far better way to go about achieving the actual goal at hand, in any event. –  JdeBP Aug 8 '11 at 0:31
add comment

(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.

share|improve this answer
add comment

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.

share|improve this answer
    
+1 for saying that the hosts file is not the way to go about achieving the end goal. You could also mention the Proxy Auto-Configuration script way of achieving the intended goal. –  JdeBP Aug 8 '11 at 0:33
add comment

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

127.0.0.1->example.com
share|improve this answer
    
Are you sure this matters? On other systems there is no difference between space and tab in a hosts file. –  Raffael Aug 7 '11 at 12:03
2  
@Raffael: It doesn't matter. Spaces and/or tabs are acceptable delimiters. –  Randolf Richardson Aug 12 '11 at 3:33
    
@Randolf, Raffael: That's the issue I ran by myself. Space was not working until I switched to tab –  abatishchev Aug 12 '11 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. –  Randolf Richardson Aug 12 '11 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 '11 at 7:15
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.