Restricting website access using /etc/hosts
I've been adding websites that I don't want to have access to to my /etc/hosts file by writing
0.0.0.0 www.websiteIwanttoblock.com.
For some reason I'm still able to access this particular website. Any help is appreciated thanks.
Edited 15 Aug 2020. We only hold this version.
About this post
- Posted
- Archived
- Edited
- Score
- 1 · 100% up
- Kind
- Text
- ID
- hkccca
18 comments
This is not an appropriate way to restrict access since you're just trying to keep some DNS lookups from being sent up the "chain". I guess you are doing this for parental control reasons? I think you might have a look at your router UI. Most of them got some parental control settings.
Perhaos a silly question, but are you trying to access exactly what you set to block? E.g. FQDNs with and without "www" are technically different, etc.
Deleted before we saved it.
If you're using Firefox (perhaps other browsers too), it will cache the DNS lookup. So if you visited that site, then added it to /etc/hosts, then try to visit it again, you'll get there. If you restart Firefox that will solve the problem (if this is your cause).
Firefox (and some other browsers) are now using DoH (DNS over HTTPS), which bypasses the hosts file, entirely. This is my major complaint about DoH (and others have other valid complaints). By bypassing local security measures, DoH prevents us from configuring our network access the way we want it.
That's a perfect pain in the arse.
Happily my Firefox is using my own local nameserver which I block Facebook etc with rather than /etc/hosts. Though after checking I can see that I've got DoH disabled; whether I did that myself or it's a default I don't know.
This not the best way to block sites.
Not all programs will check hosts file.
Better to block on router or a service such as opendns.
Technically correct, but irrelevant. Programs use OS-provided mechanisms for looking up IP addresses (e.g.
getaddrinfo(3), which invokes glibc's address resolution component, which will take/etc/hostsinto account by default).In the context of simply resolving names to IP addresses, an
/etc/hostsentry can be considered a DNS record.--edit: I shall stand corrected. See the other comment about Firefox using DNS-over-HTTP, which would in fact bypass the local OS's DNS resolution mechanisms.
As /u/arch_maniac mentioned, Firefox's DNS over HTTPS would override this. Assuming you're using Firefox, check your preferences - it's under Network Settings. Presumably other browsers also have a way to turn this off?
This is nsswitch.conf(5). See
getent hosts www........ different software behaves differently, but well written software should behaves the same as getent.Maybe try to use a lying DNS server? Less overhead, more universal. Unbound(8) can scratch that itch. See https://try.popho.be/byeads.html, and https://git.sr.ht/~moviuro/moviuro.bin/tree/master/lie-to-me for the script.
You could also setup some simple iptable rules like:
iptables -A OUTPUT -d <IPv4 address> -j DROPAnd you can find a site's IPv4 address by running
ping <website>Not every website has its own IP address. In many cases, several websites share the same address (https://www.yougetsignal.com/tools/web-sites-on-web-server/).
Interesting..I didn't know that. Though I still stand by my post because I doubt many popular sites wouldn't have their own server, such as: 66.254.114.41, 104.18.155.3, 23.36.68.13 (or whatever). Btw, nsfw.
Try replacing 0.0.0.0 with 127.0.0.1 (the loopback address).
Within /etc/hosts append 127.0.0.1 www.websiteiwanttoblock.com
I'd say 0.0.0.0 is perfectly fine there.