/etc/hosts 文件无法正确阻止 ubuntu 中的网站

/etc/hosts 文件无法正确阻止 ubuntu 中的网站

为了完成一些工作,我试图从我的电脑上屏蔽某些网站(即 reddit)。但是,修改 /etc/hosts 文件似乎没有效果。我在 Ubuntu 18.04 下工作

这是我的 /etc/hosts 的内容

127.0.0.1       localhost
127.0.1.1       MY_COMPUTER_NAME
127.0.0.1 reddit.com 
127.0.0.1 www.reddit.com
127.0.0.1 np.reddit.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我希望这个文件能阻止我所有的连接尝试www.reddit.com 但即使重新启动计算机/浏览器后,当我输入以下内容时,我的浏览器仍然可以正常访问 reddit www.reddit.com 在地址栏中。

看来我无法让 /etc/hosts 文件正常工作。我还没有成功屏蔽任何网站(这不仅仅是 reddit 的问题)

如何使用 /etc/hosts 阻止从我的计算机访问网站?如何确认我的 /etc/hosts 文件是否正常工作?

当我尝试 ping 该网站时,发生了以下情况:

ping -c5 www.reddit.com
PING www.reddit.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.054 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.043 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.054 ms

我正在使用 Firefox,结果发现问题在于:Firefox 默认不使用 /etc/hosts。

答案1

感谢 Karnil 的评论,我能够在 [此处] 找到相关评论(https://unix.stackexchange.com/questions/499792/how-do-etc-hosts-and-dns-work-together-to-resolve-hostnames-to-ip-addresses

即以下评论:

Beware that many Web browsers implement their own DNS servers and DNS cache and do not consult any name-lookup mechanisms that have been configured on the system. In other words, some Web browsers completely ignore /etc/hosts and the locally-defined name servers. It's quite confusing to witness the first time around. (Looking at you, Chromium-based browsers!)

这是 Firefox 的情况。我使用了提供的响应这里,即在 about:config 中将 network.dns.offline-localhost 更改为 false

此外,所描述的过程这里 (禁用“DNS over https”)对于我的 Firefox 遵守 /etc/hosts 文件是必要的。它似乎是 Firefox 的一个相当新的演变

相关内容