Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

By editing the hosts file and adding: 0.0.0.0 sitetoblock.com I can block that particular website. Using the hosts file, is it possible to block every website except a few sites I specify? Wikipedia and dictionary.com would be whitelisted.

If this isn't possible with /etc/hosts, would you recommend another method or app that would do something similar? I do not want a browser plugin, but rather an OS wide block.

share|improve this question
    
Can you use wildcards with your hosts file? (just a thought) –  Bruno Pereira Aug 27 '12 at 11:35
    
I'm not sure but that would be nice. Then after the wild card statements, I can put the sites I want to whitelist and their actual IP? –  Kevin Duke Aug 27 '12 at 11:36
    
Think you need to setup your own DNS server (allows wildcards) or use iptables to filter all traffic. hosts does not allow wildcards. Do you have the ability to setup and change the network we are are talking about or this is just a computer? –  Bruno Pereira Aug 27 '12 at 11:37
2  
If you are trying to increase your producitivity, a browser extension might be the better option. Have you tried out Stayfocusd (chrome) or Leechblock (Firefox) ? Both offer the options you are searching for. –  Glutanimate Aug 27 '12 at 11:38
    
Nice solution from @Glutanimate ^^ Might work if the people we are talking about here are not that smart. –  Bruno Pereira Aug 27 '12 at 11:39

1 Answer 1

up vote 1 down vote accepted

You can set up a transparent proxy. As Wikipedia says,

Also known as an intercepting proxy or forced proxy, a transparent proxy intercepts normal communication at the network layer without requiring any special client configuration. Clients need not be aware of the existence of the proxy. A transparent proxy is normally located between the client and the Internet, with the proxy performing some of the functions of a gateway or router

Doing it with /etc/hosts is fine, as long as the user doesn't know about the file or doesn't have the permissions to edit the file. But I guess what you want to achieve would be easier if you use proxy, although it will take some time to set it up.

To set up the transparent proxy, you can use squid. Here are some quick steps to set it up:

  • Install squid

    sudo apt-get install squid squid-common

  • Edit the /etc/squid3/squid file:

    Look for the line http_port 3128 and make sure it is uncommented. squid by default listens to port 3128.

    acl bad dstdom_regex .*abcde.* http_access deny bad

    This will block every URL containing abcde.

  • Restart squid

    service squid3 restart

A very good guide is given here.

Here are some other links:

share|improve this answer

Your Answer

 
discard

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

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