Thursday, May 7, 2015

How to block websites and show warning page in local machine using webserver?

This post specific to Debian based linux

3 main steps:
1. Add website entries in the /etc/hosts file
2. Setup a webserver in local machine
3. Create files to serve using web server.

1. Add website entries in /etc/hosts file

    Open /etc/hosts file in an editor (as a super user, for normal users the file will open only in readonly mode.).  The typical content of the file will looks like

#Local Machine
127.0.0.1    localhost
127.0.1.1   

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters


add your new entries (websites to be blocked) at the bottom of the file like

127.0.0.1    twitter.com
127.0.0.1    yahoo.com
127.0.0.1    facebook.com
...
...
save and exit.

2. Setup a webserver in local machine.


  Any webserver can be used for the very purpose which ranges apache, Thin, twiggy, yaws, wbox, webfs, mini-httpd, lighttpd etc.,   As we are going to serve only one static webpage, It would be better if we use a very minimal, low foot print server.

I found micro-httpd a optimum one for the purpose.  Now, do the following steps in terminal.

#touch /etc/inetd.conf

#sudo apt-get install micro-httpd inetutils-inetd

By default the service directory for the micro-httpd server is "/var/www".  But, if you want to change it, you can do so in the file "/etc/inetd.conf".

3. Create files to serve using web-server


Content of a sample warning page.

<html>
<head> <title> Warning page! </title> </head>
<body bgcolor="black">
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<div align="center" style="color: red; margin-top: 25%;">

<b><h2>
Very Sorry!The website you are trying to acces is blocked </h2>
</b> </div>
</body>
</html>

copy paste the above content in a file and save it with the name "index.html" under "/var/www/" directory.  Now, reboot your machine then try to access the blocked websites via browser.  Now, you can access all the websites for blocked one your browser will show the warning page.