Your hosts file is the first place your computer is going to look to resolve host names (finding the ip address associated with a given server name). Therefore you can modify this file to block ads by simply redirecting ads server names to your computer address (127.0.0.1). Instead of downloading the content of the ads server, your computer is going to try to download it from your computer, which doesn’t have it and download nothing.

As there are a tremendous amount of ads servers out-there we are not going to do this by hand. We can download an ad-blocking host file from hosts-file.net and use it.

wget http://hosts-file.net/download/hosts.zip
unzip hosts.zip
sudo back.sh /etc/hosts
sudo mv hosts.txt /etc/hosts

The back.sh script is available on my gist here.

You can check any malicious lines in the new hosts file with the following commands.

egrep -v '^127.0.0.1|^#' /etc/hosts | more

Any line not redirecting to 127.0.0.1 might be dangerous !
For example by modifying this file one can redirect your bank website URL to another site (fishing).