Blocking Social Media and Trackers using a hosts file

Social media has become a pervasive force on the Internet. I think back to the times when I first used the Internet when the only social media was IRC, bulletin boards and MSN messenger.

Apart from being a distraction, the amount of trackers that social media sites inject into news, entertainment and other sites is a concern.

Therefore I decided to block all popular social media domains through my hosts file.

Blocking at the hosts file on your operating system is much more effective and faster than using a browser plugin to block websites and content. Since your machine will kill any network and DNS requests at the OS level rather than waiting for the browser addon to intercept any HTTP requests and resolve them to a blocked address.

In the hosts file, setting domain resolutions to 0.0.0.0 IP address is preferable to localhost, as that is a non resolvable meta-address (a flag that is used by the OS).

0.0.0.0 www.twitter.com
0.0.0.0 www.instagram.com
0.0.0.0 www.facebook.com

The example above would block the main hostname for Twitter, Facebook etc. However many of them have CDNs and other routable addresses for trackers and media. I would recommend using this list on Github here.

You can then copy and paste it to the following hosts file depending on your OS (you will need elevated privileges sudo / gksu).

Machine File location Flush DNS Command
OSX /etc/hosts sudo killall -HUP mDNSResponder
Linux /etc/hosts sudo systemd-resolve --flush-caches
Windows c:\windows\system32\drivers\etc\hosts ipconfig /flushdns

Once you have saved the edited hosts file, make sure to run the associated command with your OS to ensure you start blocking any social domains immediately.

Note: this solution is limited to the machine you are using, if you want a more comprehensive solution would be to setup pi-hole and make all DNS queries on your LAN through there.