My question concerns the blocking websites through hosts..

Jedidja87

New Member
Joined
Jan 15, 2023
Messages
2
Reaction score
0
Credits
20

Steps to block websites in Linux​

  • Open terminal shortcut keys Ctrl + Alt + T.
  • Type sudo gedit /etc/hosts and type Ubuntu password.
  • Then at the bottom of file. Type each website’s name you want to block like this:
127.0.0.1 www.facebook.com
127.0.0.1 www.youtube.com

So this was model how to block websites in linux. but somewebsites doesn't use www.. but instead of it starts with.. https://examplewebsite.zn
So can i block through hosts also when i put it https://examplewebsite.zn or is it always www.website....
 


In /etc/hosts, the variables it takes are host names, not URLs, so you would omit the http://.

If you are really keen to block a lot of "nuisance" sites, you could have a look at the hosts file from Dan Pollock who's been onto this for a long time. You can visit his website at: https://someonewhocares.org/ and then select "hosts" and read the commented text in his hosts file. To download it run in a terminal:
Code:
wget https://someonewhocares.org/hosts/hosts
You can use the file as your /etc/hosts file or amend it as you wish.

MAKE SURE that his "localhost" section corresponds to what you want, or whether you would omit his 13 or so lines and replace them with your distribution's default for that section of the hosts file. That is how I work with it and it's a treat.
 
Last edited:
SO if i omit https:// then so hosts can block just when i write chat27.co.za right?? So hosts linux would be block without that too right?
 
@osprey already pointed out that you must use hostnames. Delete the "https://" or "http://" that comes before the hostname. You must also delete anything that follows the top level domain (TLD). The top level domain is the ".com" or ".org" or ".zn" in the OP's original post. Anything that follows the top level domain in a web address is not part of the hostname and should be deleted. They often start with "/" or "?".

Look at the address bar for this webpage. If you want to add linux.org to your hosts file, you would delete "https://" and also delete "/threads/my-question..." to get the hostname for this website. The hostname is "www.linux.org". The hostname includes the "www."

Hostnames are unique. "www.linux.org" is different than "linux.org". A DNS lookup takes a hostname and returns an IP address for it. The IP address for "www.linux.org" could be different than the IP address for "linux.org", or it could be the same. If a website uses web addresses with and without "www.", then you may need two entries in your host file to fully block it.

The hosts file is a list of replacements for DNS IP address lookups. Your entries in the hosts file override all other DNS lookups. If there is an entry in your hosts file for a given hostname, it "wins" over the DNS server on the internet that your system normally uses.

The way that this hosts file trick works is to point the hostnames for blocked websites to your localhost (127.0.0.1). It is guaranteed to be the "local IP address" for your computer, so the website is blocked.
 


Latest posts

Top