Blocking multiple sites with /etc/hosts

8,504

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:

Share:
8,504

Related videos on Youtube

dukevin
Author by

dukevin

.-"-.__ | .' / ,-| ```'-, || . ` / /@)|_ `-, | / ( ~ \_```""--.,_', : _.' \ /```''''""`^ / | / `| : / \/ | | . / __/ | |. | | __/ / | | | "-._ | _/ _/=_// || : '. `~~`~^| /=/-_/_/`~~`~~~`~~`~^~`. `> ' . \ ~/_/_ /" ` . ` ' . | .' ,'~^~`^| |~^`^~~`~~~^~~~^~`; ' .-' | | | \ ` : ` | :| | : | |: | || ' | |/ | | : |_/ | . '

Updated on September 18, 2022

Comments

  • dukevin
    dukevin over 1 year

    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.

    • Bruno Pereira
      Bruno Pereira over 11 years
      Can you use wildcards with your hosts file? (just a thought)
    • dukevin
      dukevin over 11 years
      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?
    • Bruno Pereira
      Bruno Pereira over 11 years
      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?
    • Glutanimate
      Glutanimate over 11 years
      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.
    • Bruno Pereira
      Bruno Pereira over 11 years
      Nice solution from @Glutanimate ^^ Might work if the people we are talking about here are not that smart.
    • Glutanimate
      Glutanimate over 11 years
      Does this answer your question, OP?: askubuntu.com/questions/160235/…
    • Jonathan
      Jonathan about 7 years
      Also if you want to do pr0n blocking you can use dns.norton.com for free, and set it in your router