Easy way to block advertisements and domains

Nowadays many websites have advertisements inside them in order to make money for maintaining the wesite, but on the on the hand there are users that feel advertisements annoying.

A simple way to block advertisements is to block the domains of their addresses.

 

The steps to block a domain in Windows are:

1 – Open the notepad (if your OS’s version support administrative mode run with it)

2 – Open the file C:\Windows/System32/drivers/etc/hosts

At the end of the opened file you’ll find something like that:

#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1       localhost
# ::1             localhost

 

3 – the file hosts (opened above) is the first place your OS will look for the ip address of a domain, if a domain isn’t listed there, it will use a DNS server configured in your network settings. The IP 127.0.0.1 is the IP address that identifies locally your computer, all what you have to do is write in the bottom of the document the IP 127.0.0.1 followed by the domain to be blocked.

 

Example:

...
...
...
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1       localhost
# ::1             localhost

127.0.0.1    blocked.domain.com
127.0.0.1 another.blocked.domain.com

The example above shows two domains being blocked, to block advertisements like those provided by google you must put in the file the domains that serve ads, to block some of google use the lines bellow.

127.0.0.1	googlesyndication.com
127.0.0.1       google-analytics.com
127.0.0.1 www-google-analytics.l.google
127.0.0.1 googleadservices.com
127.0.0.1 doubleclick.net
127.0.0.1 doubleclick.com
127.0.0.1 googletagservices.com

 

In order to start blocking the domains, you need to restart your PC or  DNS cache.

 

Notes:

To stop blocking domains you need to the same process and remove the lines placed in the hosts file.

Some lines written in hosts file (Windows) may be flagged as malware by Windows Defender (it happened to me while blocking the  www.google-analytics.com)

If you are using linux you can block domains in the same way as windows, you need to open the file /etc/hosts with a text editor (must be superuser)

In Linux the domains to be blocked must be in the end of the file

Usually some versions of linux don’t use DNS cache and restart isn’t needful, but if you use browsers based on Google Chrome you must clear history and restart it (it has an internal DNS cache)