Blocking websites with /etc/hosts
You can use /etc/hosts
to set up local domains but you can also block access to websites using the same syntax.
You only need to redirect the website domain to your own computer (127.0.0.1
). For example, to block facebook.com, edit /etc/hosts/
file (with root permissions) and add the following line:
# /etc/hosts
127.0.0.1 facebook.com
Save the file and check you cannot access facebook.com
with curl
or a web browser.
$ curl -I facebook.com
curl: (7) Failed to connect to facebook.com port 80 after 0 ms: Connection refused
This rule blocks facebook.com
(including http
and https
) but doesn’t block www.facebook.com
unless you add another rule.
# /etc/hosts
127.0.0.1 facebook.com
127.0.0.1 www.facebook.com
You can add as many rules as you want.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: