How to Block Certain Website Access on Linux

Distraction is generally happens when you are connected to the Internet.

This is because of social media sites such as Facebook.com, Twitter.com, etc.,

This would reduce your productivity on work if you spend more time on this.

How to block these websites to improve the productivity.

It’s a great way to prevent children to access adult content on the internet.

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

This can be done in few 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 plain-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’s a local DNS server that is used primarly to bypass DNS resolution.

A match found in the /etc/hosts file will be used before any DNS resolution.

There is no DNS resolution will be performed if the given match is found in the /etc/hosts file.

For example, to block facebook.com and youtube.com, adding the below lines to your hosts file will do it.

# 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 might already use 127.0.0.1, the loopback address. It used to establish an IP connection to the local machine.

so we prefer to use 0.0.0.0, which is defined as a non-routable meta-address used to designate an invalid, unknown, or non applicable target.

Using 0.0.0.0 is empirically faster, possibly because there’s no wait for a timeout resolution.

Once you blocked them, it’s better to restart network services and flush DNS cache on Linux.

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

Facebook.com

Twitter.com

Method-2: How to Block a Website Access on Linux Using the 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 the 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 the 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 a Website Access on Linux Using the 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 a Website Access on Linux Using the 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...