Hereafter we take only ipv4 into account. While looking for a way to block a certain website without any browser plugin, I found this question: Blocking Websites with /etc/hosts. According to the accepted answer, just adding
0.0.0.0 <domain>
to /etc/hosts
can block an access to the domain <domain>
. And this worked like a charm. But why does this work that way?
Experiments
Assume a website X.com
loads some scripts from Y.com
, and I added 0.0.0.0 Y.com
to /etc/hosts
to block the scripts to be loaded.
When I directly type
Y.com
or0.0.0.0
in the browser's address bar, it is routable and leads me to my local websitelocalhost:8080
.However, when I access
X.com
,Y.com
is totally blocked. By "totally" I meanY.com
cannot be accessed not due to Timeout nor Not Found nor Forbidden; as far as I inspect Network Monitor in Firefox, the browser doesn't even try to accessY.com
in the first place†1, which implies the meaning of0.0.0.0
here is different from the first result above.
†1: Or, perhaps, it actually tries to access Y.com
but returns instantly with no result. If so, I don't understand why it returns instantly without waiting for (dozens of) seconds for timeout.
Questions
In this case, who interprets
0.0.0.0
? A browser?Why does a indirect reference to
0.0.0.0
(Experiment 2) mean "this should be blocked" while a direct access to0.0.0.0
(Experiment 1) means "this should be connected to the local website"?
0.0.0.0 - Wikipedia gives me a hint, but it doesn't explain in which context a certain meaning is chosen.
Environments:
Firefox 77.0.1 on Arch Linux
My Guess
After posting this question, I did some tests to find one fact:
Although many blog posts and answers on this website say 0.0.0.0 <domain>
can be used to block <domain>
, actually it does not block the domain. Strictly speaking, it depends. Like a normal entry in /etc/hosts
, 0.0.0.0 <domain>
just converts an access to <domain>
to an access to 0.0.0.0
.
Because
0.0.0.0
is same aslocalhost
in this context†2and an access to
0.0.0.0
is instantaneous†3
, as far as you are not running a webserver on the host, 0.0.0.0 <domain>
effectively blocks an access to <domain>
.
When you are running a webserver,
An access to
<domain>/<file>
is effectively blocked iflocalhost/<file>
doesn't exist. Note, however, since the webserver is accessed and returns 404, numerous accesses to<domain>/<file>
may slow down your computer.An unexpected result is observed if
localhost/<file>
does exist. If you are lucky, it just break the layout of a website. But generally it can be very dangerous.
So, in my guess, 0.0.0.0 <domain>
is nothing more than a workaround; it works under limited environments.
†2: I don't yet understand why. Suspected reason: What does Chrome/server do when I use 0.0.0.0 instead of localhost in browser?
†3: For example, ping -c 1 0.0.0.0
returns in a moment. I don't know why. (Perhaps just because an access to a local interface is very fast?)