By editing the hosts file and adding: 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.

  • Can you use wildcards with your hosts file? (just a thought) Aug 27, 2012 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?
    – dukevin
    Aug 27, 2012 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? Aug 27, 2012 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. Aug 27, 2012 at 11:38
  • Nice solution from @Glutanimate ^^ Might work if the people we are talking about here are not that smart. Aug 27, 2012 at 11:39

1 Answer 1


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

    sudo service squid3 restart

A very good guide is given here.

Here are some other links:


Your Answer

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

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