Block youtube on safari in hosts file

When I try to block websites on terminal (etc hosts..) it works. But when I try to do it with Youtube, it doesn't.

Typing youtube.com as a url won't open it, but if I search youtube on google for example and then click on it, it will work (redirections.)

I tried to type all forms of "youtube.com" , with https/www, without, I cleared cache, restarted my mac. Nothing worked.

Is there a way i can fully block Youtube?

Thanks


Solution 1:

  1. Edit your hosts file:
sudo vi /etc/hosts
  1. Add URLs you want to block. This should be sufficient:
# Block YouTube
127.0.0.1 youtube.com
127.0.0.1 www.youtube.com
127.0.0.1 youtu.be
127.0.0.1 www.youtu.be
127.0.0.1 youtube-nocookie.com
127.0.0.1 www.youtube-nocookie.com
  1. Reset the Directory Service cache
dscacheutil -flushcache
  1. Ping the the hosts for verification:
$ ping youtube.com    
PING youtube.com (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.088 ms

$ ping youtu.be
PING youtu.be (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.151 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.082 ms

If you still can access the site, a reboot should help.

sudo reboot