How to Block the Access to Certain Websites on Linux

Distractions usually occurs when you are connected to the Internet.

The reason for this is social media sites such as Facebook.com and Twitter.com.

If you spend too much time on this, it will reduce your productivity.

How to block these websites to improve productivity.

This is a great way to prevent children from accessing adult content on the Internet.

In this article, we will show you how to block a website to prevent access.

This can be done in some ways and the details are below.

Method-1: How to Block a Website Access on Linux Using the /etc/hosts File

The “/etc/hosts” file is a simple text file used by all operating systems to map hostnames to IP addresses.

The file contains one line per IP address with the following information.

IP_address canonical_hostname [aliases...]

It is a local DNS server that is used primarily to avoid DNS resolution.

For example, a match found in the “/etc/hosts” file will be used before any DNS resolution.

In other words, the DNS resolution is not performed if the given match is found in the “/etc/hosts” file.

For example, to block “facebook.com” and “twitter.com”, it is sufficient to add the following lines to your host file.

# vi /etc/hosts

0.0.0.0 www.facebook.com
0.0.0.0 www.twitter.com
0.0.0.0 twitter.com

Most of you can already use the 127.0.0.1, loopback address. It is used to establish an IP connection with the local computer.

So, I advise you to use 0.0.0.0, which is defined as an non-routable meta-address that can be used to refer to an invalid or unknown target.

Using 0.0.0.0 is empirically quick as there is no wait for a timeout resolution.

When you block them, it is good to restart network service and flush the DNS cache on Linux.

Now, we are going to check both websites in the browser to verify this.

Facebook.com

Twitter.com

Yes, we got the expected results as we did not access them.

Method-2: How to Block a Website Access on Linux Using iptables

Iptables is used to set up, maintain, and inspect the tables of IPv4 packet filter rules in the Linux kernel.

Use the following commands to block “facebook.com” using iptables on Linux.

# iptables -A OUTPUT -p tcp -d www.facebook.com --dport 443 -j DROP
# service iptables save
# service iptables restart
# iptables -L

Use the following commands to block “twitter.com” using iptables on Linux.

# iptables -A OUTPUT -p tcp -d www.twitter.com --dport 443 -j DROP
# iptables -A OUTPUT -p tcp -d twitter.com --dport 443 -j DROP
# service iptables save
# service iptables restart
# iptables -L

Method-3: How to Block Website Access on Linux Using Chomper

Chomper is a Python command-line that allows user to create a list of webistes to be blocklisted or whitelisted for specified periods of time.

This tool is especially developed to improve the user productivity on working hours by saving them from Internet distractions.

This allows you to block particular URLs as well.

Method-4: How to Block Website Access on Linux Using CTparental

CTparental is a parental control tool used to control how others can use a Linux computer.

You can easily manage this using a web interface (https://admin.ct.local).

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...