Simple command to block distracting websites [Productivity]

twitter logo ・1 min read

For Linux based systems

Step 1: Open terminal
Step 2: Open the hosts file sudo vi /etc/hosts
Step 3: Add websites you want to block and save

For windows

The host file location(step 2) is> C:\Windows\System32\drivers\etc
everything else is the same

I'm tweeting similar things on twitter. Follow me and feel free to message me.

twitter logo DISCUSS (12)
markdown guide

Someone made this into a single simple command, here:

Tested and approved, i have been using it for a while now.


I expected this to be more like "[A] simple command" and not "Did you know there is a hosts file which exists and you can edit?"


Here you go:

sudo su
echo "" >> /etc/hosts
echo "" >> /etc/hosts

Will work very well to block a website. Unblocking the website will need more sophisticated version of this by having conditional statements and patterns to remove the matching entries. I prefer editing the file, it's much faster and neat.


I was thinking of having two /etc/hosts files:

a. one that blocks distracting websites
b. a "normal" one

Also, a cron job (?) that is running twice:

a. in the morning to activate the "blocking" one (renaming?)
b. in the afternoon (after work anyway) to activate the "normal" one

Was thinking about the same but dunno how...
How? 😀


If your on MacOS, it's even easier to just use something like the app "Self Control" you can either blacklist or whitelist a website for a set period of time, with no way to turn it off, even at restart.


We may need to extend that list with entries for and :D


make sure you clear your browser cache after doing it.


and you can also block a bunch of tracking websites with that technic.


I tried this couple of months ago! Figured out social logins don't work anymore. I settled with custom userscripts (tempermonkey/greasemonkey) that removes body from html upon visiting website.

Classic DEV Post from Mar 24

Learning React, GraphQL, and Apollo?

How to build a URL shortener with React, GraphQL and Apollo

Pradeep Sharma profile image

Sore eyes? now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️