Block youtube on safari in hosts file
Edit your hosts file:


sudo vi /etc/hosts



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



Reset the Directory Service cache


dscacheutil -flushcache



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