Block Ads Using Host File In Ubuntu
The easy way to block ads or any other malware, pornography site and so on is by using the host file.
Whenever you open any web site the domain (or FQDN) has to be converted to IP. For that it uses DNS server. But before using DNS it checks if there is an entry in the host file. So we'll put all the domain name of the adserver with wrong IP address so that they can't function.
For eg: if we put Google.com as 127.0.0.1, our browser will try to find google.com in the IP address 127.0.0.1 which turns out to be our own computer and there isn't any Google's webserver in our computer. So it won't work.
But filling the entry for all the ad servers manually is tedious task. This is where sites like http://hosts-file.net/ comes into play. From this website we can download the host file that contents entry for ads, malware, torrent, pornography or subset of any.
So if we want to block only ads lets download the host file for blocking ads by clicking here or visiting the download page and downloading ad_servers.asp file.
After downloading open that file. Entry beginning with "#" are comments. We need to add entry for our computer in that file. You can look at the contents of your existing host file by running the following command in terminal:
Make a same entry in the file you downloaded.
Now again in terminal backup your existing host file. After that replace the original host file with the downloaded one.
If you don't like to see the error message use some ad blocking extension for browser like adblock plus for Chrome and adblock plus for Firefox.
You may ask why I didn't use ad blocking extension directly as it is enough to achieve the objective. Well, I chose to use host file to block ads instead of extensions because of following reasons:
Whenever you open any web site the domain (or FQDN) has to be converted to IP. For that it uses DNS server. But before using DNS it checks if there is an entry in the host file. So we'll put all the domain name of the adserver with wrong IP address so that they can't function.
For eg: if we put Google.com as 127.0.0.1, our browser will try to find google.com in the IP address 127.0.0.1 which turns out to be our own computer and there isn't any Google's webserver in our computer. So it won't work.
But filling the entry for all the ad servers manually is tedious task. This is where sites like http://hosts-file.net/ comes into play. From this website we can download the host file that contents entry for ads, malware, torrent, pornography or subset of any.
So if we want to block only ads lets download the host file for blocking ads by clicking here or visiting the download page and downloading ad_servers.asp file.
After downloading open that file. Entry beginning with "#" are comments. We need to add entry for our computer in that file. You can look at the contents of your existing host file by running the following command in terminal:
cat /etc/hostsThere you'll see one entry for your computer. For eg: 127.0.0.1 my-laptop
Make a same entry in the file you downloaded.
Now again in terminal backup your existing host file. After that replace the original host file with the downloaded one.
sudo cp /etc/hosts{,.orig}Now restart the browser and if you open any website containing ads you'll see a website not found error page in the ad placeholder.
sudo cp ~/Downloads/downloaded_host_file /etc/hosts
If you don't like to see the error message use some ad blocking extension for browser like adblock plus for Chrome and adblock plus for Firefox.
You may ask why I didn't use ad blocking extension directly as it is enough to achieve the objective. Well, I chose to use host file to block ads instead of extensions because of following reasons:
- Host file ensures that the ad doesn't gets loaded instead of just hiding it.
- I found that extensions like adblocking has to regex search each URL of the webpage with its huge data of blacklist and decreases the performance on my computer.
If you want to revert the changes just backup the ad blocking host file and restore the original file by running the following command in terminal. And yes you may want to disable/uninstall the ad blocking extensions.
sudo cp /etc/hosts{,.ad}
sudo cp /etc/hosts{.orig,}
Comments
Post a Comment
Comments are moderated. No spam please.