Editor's Note: Tip Top Tips is a new monthly column in The PCLinuxOS Magazine. Each month, we will feature -- and possibly even expand upon -- one tip from the PCLinuxOS forum. The magazine will not accept independent tip submissions specifically intended for inclusion in the Tip Top Tips column. Rather, if you have a tip, share it in the PCLinuxOS forum's "Tips & Tricks" section. Your tip just may be selected for publication in The PCLinuxOS Magazine.
The inaugural tip for the Tip Top Tips column comes from Texstar.
Download this hosts file and copy it as root to your /etc folder, replacing the existing hosts file, and provided you have not changed your host name from localhost.
What it does ...
You can use a HOSTS file to block ads, banners, 3rd party Cookies, 3rd party page counters, web bugs, and even most hijackers. This is accomplished by blocking the connection(s) that supplies these little gems. The hosts file is loaded into memory (cache) at startup, so there is no need to turn on, adjust or change any settings. Now you don't have to track down and install adblock edge (or any other ad blocker) for all your browsers, unless you want to.
To sweeten the pot a bit, user jonjongx shared a longer, more complete hosts file that was over twice as big as the one that Texstar shared. If you're also a Windows user, jonjongx also shared a similar hosts file, specially tailored for Windows.
Did you notice that Texstar posted that you can replace your existing hosts file, provided you have not changed your host name from localhost? Yep. Several others noticed also. So, let's review what you need to do if you have changed your host name from localhost.
On my computer, my default hosts file looks like this:
# generated by drakconnect
127.0.0.1 localhost.localdomain localhost
Changing my host name in drakconnect to parnote-toshiba, it looks like this:
# generated by drakconnect
127.0.0.1 parnote-toshiba.localdomain parnote-toshiba
So, let's take a look at the beginning part of the hosts file. The file you download will look like this:
1. # This MVPS HOSTS file is a free download from: #
2. # http://winhelp2002.mvps.org/hosts.htm #
3. # #
4. # Notes: The Operating System does not read the "#" symbol #
5. # You can create your own notes, after the # symbol #
6. # This *must* be the first line: 127.0.0.1 localhost #
7. # #
9. # -------------- Updated: January-24-2015 ---------------- #
11. # #
12. # Disclaimer: this file is free to use for personal use #
13. # only. Furthermore it is NOT permitted to copy any of the #
14. # contents or host on any other site without permission or #
15. # meeting the full criteria of the below license terms. #
16. # #
17. # This work is licensed under the Creative Commons #
18. # Attribution-NonCommercial-ShareAlike License. #
19. # http://creativecommons.org/licenses/by-nc-sa/4.0/ #
20. # #
21. # Entries with comments are all searchable via Google. #
23. 127.0.0.1 localhost.localdomain localhost
25. ::1 localhost #[IPv6]
I've numbered the lines so that you can follow along much more easily. You can turn on line numbering in your favorite text editor, so that you can follow along more easily, as well. All the lines that start with # are comments. In fact, everything following a # is treated as a comment, as in line 25, where there's a comment at the end of the entry.
In line 23, you need to change the host name to match the host name you use on your computer. So, in my case, line 23 should read like this:
127.0.0.1 parnote-toshiba.localdomain parnote-toshiba
You will also need to change line 25 to read something similar to this:
::1 parnote-toshiba #[IPv6]
Again, make sure to use the host name that you used to name your computer. Attempting to edit or change the contents of a hosts file -- which would be anything beyond the first 25 lines shown here, and beyond the editing shown here -- is foolish and fits with a six letter word that starts with "s" and ends in "d" that rhymes with the word "squid." Unless you really know what you're doing, the rest of the file contents are best left alone. Also, wildcard characters, such as "*" and "?", are not allowed in a hosts file (which is why you see so many addresses that look remarkably similar). So, while you may be tempted to reduce the hosts file size with the use of wildcards, the results will disappoint you. My recommendation is to not even try.
OK ... such changes can't possibly come without some other things to be cautious about. Before downloading the hosts file and replacing your current hosts file, you will want to (as the root user) go into the /etc folder and rename your current hosts file to something like hosts.bak or hosts.orig. This is especially true if you already have a custom hosts file, as this newly downloaded hosts file will end up replacing your current hosts file. How to do this via a graphical file manager will vary, depending on which file manager and which desktop environment you're using. But, you can do this very easily from the command line, and it's the same for all desktop environments. If you downloaded your new hosts file to your Downloads directory, and called it hosts.txt, you can complete all the steps by entering the following at a command line prompt:
tr -d '\15\32' < ./hosts.txt > ./hosts.new
<enter root password>
cp hosts hosts.bak
cp /home/<your-username>/Downloads/hosts.new /etc/hosts
(Answer "y" when asked if you want to overwrite the hosts file)
In case you didn't know, Windows/DOS and *nix computers write text files differently. Windows/DOS computers place a CR+LF (carriage return + line feed) at the end of every line, while *nix computers place only a LF at the end of every line. The first line switches to the Downloads directory in your /home folder. The second line removes the CR used at the end of every line in a Windows/DOS text file, leaving just a LF character at the end of every line, as *nix text files expect. If you don't perform this step on a hosts file that is formatted for Windows/DOS (and the one at winhelp2002.mvps.org is formatted for Windows/DOS), it won't work correctly under *nix. Your *nix computer will see it as one long line, and the lines won't be properly parsed.
Removing the CR from the hosts.txt file also shaved over 15 KB from the hosts file size on my computer. This makes sense, since there are over 15,000 entries (lines) in the new hosts file. Each CR in the Windows/DOS text file counts as another byte. As you can see, these can add up, one byte per line. If you're not sure if a file is written in Windows/DOS text file format or *nix text file format, go ahead and run the command anyways. If no CR+LF is found at the end of each line, no changes will be made.
Updating your hosts file in this manner allows you to restore your previous hosts file, should you ever decide to do so. In that case, all you have to do is delete your hosts file and rename your hosts.bak file to hosts ... all as the root user, mind you. Just that quickly, your previous hosts file is restored.
Also, keep in mind that the hosts file will be parsed for every web page you visit. A longer hosts file will take longer to parse than a shorter file (although, with today's fast, modern multi-core processors, the time difference may be barely perceptible). Thus, you may notice faster browsing with a shorter hosts file, compared to a longer hosts file. But compared to not blocking all those ads and other undesired elements on a web page, even a longer hosts file will display faster browsing than loading up/downloading all the junk on a lot of web pages. The tradeoff here with a shorter hosts file vs a longer hosts file is that a shorter hosts file is (more than likely) going to block less of the undesired garbage from being downloaded. This is going to be an especially important consideration if you are on a metered (limited) bandwidth internet plan, or if you are still using a dialup connection.
If you have Ghostery, AdBlock Plus or any other ad blocker installed, you might want to disable them in your browser(s). Otherwise, you won't really know if your new hosts file is doing its job. Of course, there is nothing to prevent you from using any of the browser add ons to block garbage on websites, in addition to your new hosts file. Some users view the use of both as an extra layer of protection.
The other problem you might encounter is that things you may not want to be blocked are being blocked. If you're brave (refer to my earlier warning about editing the meat of the hosts file), and if you know from where those items originate, you will have to comment out their originating address from the hosts file. Remember, a line that starts with "#" is treated as a comment, and the rest of that line is ignored. So, placing a "#" at the beginning of the line will eliminate the address that follows from being blocked. The other (and better) alternative is to find a less restrictive hosts file. You can perform an internet search for alternatives.
Finally, you will want to keep in mind that your hosts file will need to be updated periodically. At the minimum, you will want to update your hosts file every month. New PCLinuxOS user reelcat provided scripts that he uses to update his hosts file. He also provided some very important additional considerations, as well as some alternative hosts files that can be used. All you have to do is set up a crontab task to run the scripts on a regular basis, in the background without you having to do anything else.
Does It Work?
Yes, it does. It works quite well. Below are some screenshots from some web pages that I visited after switching to the use of a custom hosts file.
The top image was displayed when I visited the Digital Inspirations tech blog. The second image was displayed on YouTube, when it couldn't load the ad. The third image was displayed on the delicast streaming audio site, when it couldn't load the ad image (barely visible at the far right of image). The ads on another site I frequent is replaced by empty yellow boxes, in the fourth image. So, as you can see, the results you get may vary for each site you visit where the intrusive ads are blocked. Sometimes, as on the Digital Inspirations tech blog, they replace the ads with a plea for mercy. Sometimes, as on YouTube, you get the typical Firefox "can't connect" screen. And other times, you simply get nothing, as on the delicast site, or empty boxes, as in the fourth image.
Using a custom hosts file to make your web browsing less "intrusive" is an excellent idea. The added benefit is that it works on a systemwide basis. So, you can have as many web browsers installed as you want, and it will block all the garbage in every one of them ... all without the installation of ad blockers in any of them. I have Firefox, Chromium, Chrome and Midori installed (although I use Firefox the vast majority of the time). I did notice one thing when I launched Chrome that no ad blocker had ever successfully accomplished: the text banner ads in Gmail were gone -- as in nowhere to be found, missing, not displayed. In Firefox, I had managed to successfully block the text banner ads in Gmail via AdBlock Plus, but I had never been able to block them in Chrome.
Using a custom hosts file will speed up your browsing. It will also save bandwidth, which is especially important when using metered (limited) internet services, or if you are unfortunate enough to still be using a dialup connection. It blocks the garbage so it is never even downloaded.