By now anyone using the internet is bombarded by ads everywhere online unless measures are taken to minimize them. There are plenty of ad blocking programs out there but another method that is easy is implement is the use of the hosts file. Originally this file was used to map hosts names to IP address before Domain Name Servers (DNS) was implemented in 1984 allowed for the process to be automated. For most end users today this file is unused but could provide beneficial experiences if configured. When an IP address is specified in the hosts file for a domain the system will use that address, allowing blocking of sites by directing them to 0.0.0.0 or 127.0.0.1. This can be used to stop ads from loading and potentially increase page load times. Now it might seem like a tedious task to add all the add sites to block but there are providers of hosts files that are already completed and updated regularly; I use MVPs.org to provide my hosts files.
Note that while the hosts file when configured can block ads and some malware redirects, it is only an additional layer of protection. Systems should still run other forms of protection such as anti-virus and anti-malware with the hosts file. Below are instructions to load the file from MVPs.org to the most common operating systems.
The hosts file in modern distributions is located at %SystemRoot%\system32\drivers\etc\hosts and can be edited with notepad or other text editors. Just be sure not not add any extension like .txt which the built in notepad is known for doing. Note that the file is in the windows directory so any app trying to edit it will require being run as administrator.
Thankfully however, MVPs.org has an app to update the host file easily. Just download the zip file from http://winhelp2002.mvps.org/hosts.htm and unzip to a folder. Then right-click the mvps batch file and choose Run as administrator. This will load the downloaded data into the host file. More detailed instructions and the date of last updated host file is available on their site.
While I was manually updating host files before, came across a post to make a script for Linux which when added to the chrontab allows for easily maintaining the most up to date hosts file. Visit http://www.putorius.net/2012/01/block-unwanted-advertisements-on.html for details and description of the script. I made a few small changes.
To complete these steps you will need to be running as root or using sudo su to get root permission. Your prompt should end with
First, backup your current host file:
cp /etc/hosts /etc/.etchosts
Next use vi or nano to create the script /root/update_hosts.sh and add the following code to it:
mv hosts.txt /etc/hosts
cat /etc/.etchosts >> /etc/hosts
Make the file executable by running:
chmod +x /root/update_hosts.sh
Schedule to run automatically:
crontab -e then add the line below to make it update nightly at 23:59
59 23 * * * /root/update_hosts.sh > /dev/null 2>&1
The time can be changed by adjusting the numbers. the > /dev/null 2>&1 is sending all output from the scheduled job to the bit bucket so that crontab does not email the results each night.
Instructions for updating the Mac OS host file can be found here: http://pointhope.de/tips&tricks/no_place_like_localhost.html
If you have root on your Android device the hosts file is stored at /system/etc/hosts and can be changed with a terminal app, however you will need to mount the path as R/W before changes can be made. Something like Beansoft – Mount /system (rw / ro) will do the job. Because on Android the hosts file is symbolic linked to /etc/hosts as well it seems to not want to allow overwriting the file but the contents can be changed with the cat command and achieve the same desired result.
Remember to run
su as commands require root access.
First time backup your current hosts file:
cp /system/etc/hosts /system/etc/.etchosts
Then run the following to update:
If you do not have a /sdcard/tmp directory run
mkdir /sdcard/tmp and rerun the cd command above
cat hosts.txt > /system/etc/hosts
cat /system/etc/.etchosts >> /system/etc/hosts
At this point you can exit terminal and then remount System as R/O.