mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translated
This commit is contained in:
parent
c30d266cd0
commit
e648e75db5
@ -1,90 +0,0 @@
|
||||
translating----geekpi
|
||||
|
||||
This Script Updates Hosts Files Using a Multi-Source Unified Block List With Whitelisting
|
||||
============================================================
|
||||
|
||||
![](https://irp-cdn.multiscreensite.com/58a25abc/dms3rep/multi/desktop/host-400x300.jpg)
|
||||
|
||||
There are many helpful lists maintained online with constantly-updated hosts files that contain the domains of various Internet crap peddlers. Copying these lists into your hosts file can easily block a large number of domains that your system would be better off unable to connect to. This method works without installing a browser plug-in and will handle blocking for any browser (and any other program) on the system.
|
||||
|
||||
In this tutorial, I'll show you how to get Steven Black's [Unified Hosts script][1] up and running in Linux. The script will keep your computer's hosts file updated (from multiple sources) with the latest known ad servers, phishing sites and other web scum, all while providing a nice, clean way to manage your own black/whitelists separately from the lists the script manages.
|
||||
|
||||
There are two caveats to note before putting 30,000 domains into your hosts file. First off, these huge lists contain servers that you might need unblocked in order to make an online purchase or for some other temporary situation. You'll know you have a problem when something on the web is broken since you messed with your hosts file. To remedy this, I'm going to show you how to make a handy on/off switch so you can quickly disable your blocklist to buy that Himalayan salt lamp (it's the ions). I still consider it a feature of these lists that all manner of things get blocked (it was only annoying until I thought of making that off switch). If you have a repeat problem with a blocked server that you need often, just add it to your whitelist file and you're all set.
|
||||
|
||||
The second problem is a slight performance hit because the system has to check the entire list each time a domain is called up. Maybe there's a small hit, but not big enough for me to ditch the list and let every connection through. However, consider yourself warned.
|
||||
|
||||
Hosts files block a server by directing requests to that server to either 127.0.0.1 or 0.0.0.0 - in other words, nowhere. Some say using zero's is a faster, less problematic way to accomplish the blocking. You can configure the script to use any blocking ip you want by executing it with the --ip option like "--ip nnn.nnn.nnn.nnn", but the default is 0.0.0.0 and that's where I'm leaving it.
|
||||
|
||||
I used to do what Steven's script does by hand every once in a while, going to each site, copy/pasting their lists into my hosts file, doing a find-replace to change the one-twenty-seven's to zeros, etc. I knew the whole thing could be automated and there were tons of dupes, but I never took the time to address the problems. Since finding this script, that's now a forgotten chore.
|
||||
|
||||
Let's start by getting a fresh copy of Mr. Black's code downloaded (~150MB) so we can push some electrons around. You'll need git installed, so if it's not, go to the terminal and enter:
|
||||
|
||||
**sudo apt-get install git**
|
||||
|
||||
After it's done installing, enter:
|
||||
|
||||
**mkdir unifiedhosts**
|
||||
**cd unifiedhosts**
|
||||
**git clone** [**https://github.com/StevenBlack/hosts.git** ][2]
|
||||
**cd hosts**
|
||||
|
||||
While your cursor blinks at Steven's script, let's talk about choices. The script has a few options and extensions that I wont cover here, but I figure if you got this far and you're interested, the [readme.md][3] file can tell you all you need to know.
|
||||
|
||||
You need python installed to run the script, and which version you have matters. To find the version of Python you have, enter:
|
||||
|
||||
**python --version**
|
||||
|
||||
If you don't have python installed:
|
||||
|
||||
**sudo apt-get install python**
|
||||
|
||||
For Python 2.7, execute the script by entering "python" as I do below. For Python 3, replace "python" in the command with "python3". Upon execution, the script makes sure it has the most recent version of each list and if not, grabs a new copy. It then writes a new hosts file, including anything in your black/whitelists. Let's give it a try using the -r option to replace our active hosts file and the -a option so the script wont ask any questions. Back to the terminal, and:
|
||||
|
||||
**python updateHostsFile.py -r -a**
|
||||
|
||||
The command will ask for your password so it can write to /etc/. For the newly updated list to be active, some systems require a good flush of the DNS cache. On the same hardware, I've observed different operating systems express very different behavior regarding the length of time it takes for a server to become reachable/unreachable after updating the hosts file without a flush. I've seen everything from instantaneously (Slackware) to never/reboot (Windows). There are commands to flush the DNS cache, but they're different on each OS and even each distro, so just reboot if the changes don't take.
|
||||
|
||||
Now it's as simple as adding your personal exceptions to the black/whitelist files and running the script whenever you want to update the hosts file. The script will adjust the resultant hosts file according to your specs, appending the file with your additions automatically each time you run it.
|
||||
|
||||
Finally, let's create the on/off switches. We need a script for each function, so go back to the terminal to create the OFF switch by entering (replacing leafpad with your text editor):
|
||||
|
||||
**leafpad hosts-off.sh**
|
||||
|
||||
and enter the following into the new file:
|
||||
|
||||
**#!/bin/sh**
|
||||
**sudo mv /etc/hosts /etc/hostsDISABLED**
|
||||
|
||||
And then make it executable with:
|
||||
|
||||
**chmod +x hosts-off.sh**
|
||||
|
||||
Similarly, for the ON switch:
|
||||
|
||||
**leafpad hosts-on.sh
|
||||
**
|
||||
and enter the following into the new file:
|
||||
|
||||
**#!/bin/sh**
|
||||
**sudo mv /etc/hostsDISABLED /etc/hosts
|
||||
**
|
||||
And finally make it executable with:
|
||||
|
||||
**chmod +x hosts-on.sh
|
||||
|
||||
**All you need to do is create a shortcut somewhere for each script, labeling them HOSTS-ON and HOSTS-OFF and there you have it.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.darrentoback.com/this-script-updates-hosts-files-using-a-multi-source-unified-block-list-with-whitelisting
|
||||
|
||||
作者:[dmt][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.darrentoback.com/about-me
|
||||
[1]:https://github.com/StevenBlack/hosts
|
||||
[2]:https://github.com/StevenBlack/hosts.git
|
||||
[3]:https://github.com/StevenBlack/hosts/blob/master/readme.md
|
@ -0,0 +1,90 @@
|
||||
此脚本使用含多来源统一阻止列表的白名单来更新主机文件
|
||||
============================================================
|
||||
|
||||
![](https://irp-cdn.multiscreensite.com/58a25abc/dms3rep/multi/desktop/host-400x300.jpg)
|
||||
|
||||
网上有许多持续维护的含有不同垃圾域的有用列表。将这些列表复制到你的主机文件中可以轻松阻止大量的域,你的系统则无法连接它们。此方法在不安装浏览器插件的情况下工作,并且将处理系统上任何浏览器(和任何其他程序)的阻止操作。
|
||||
|
||||
在本教程中,我将向你展示如何在 Linux 中启动并运行 Steven Black 的[统一主机脚本][1]。该脚本将使用最新的已知广告服务器、网络钓鱼网站和其他网络垃圾邮件更新(多个来源)的计算机主机文件,同时提供一个漂亮、干净的方式来管理自己的黑名单/白名单,与脚本管理的列表分开管理。
|
||||
|
||||
在将 30,000 个域放入主机文件之前,需要注意两点。首先,这些巨大的列表包含可能需要解除封锁的服务器,以便进行在线购买或其他一些临时情况。当网上的事情出现问题时,你会意识到你遇到一个麻烦了,因为你搞砸了你的主机文件。为了解决这个问题,我将向你展示如何使用方便的打开/关闭开关,以便你可以快速禁用阻止列表购买喜马拉雅盐雾灯(它是等离子灯)。我仍然认为这些列表的一个特点是将所有的一切都封锁(这只是烦人,直到我想到了关闭开关)。如果你经常遇到你需要的服务器被阻止的问题,只需将其添加到白名单文件中即可。
|
||||
|
||||
第二个问题是性能受到了轻微的影响, 因为每次调用一个域时, 系统都必须检查整个列表。也许会有一个小的命中, 但不够大, 因此我放弃名单, 让每一个连接都通过。但是, 请你自己考虑。
|
||||
|
||||
主机文件通过将请求定向到 127.0.0.1 或 0.0.0.0(换句话说定向到空地址)来阻止请求。有人说使用 0 是更快,问题更少的方法。你可以将脚本配置为使用 “-ip nnn.nnn.nnn.nnn” 这样的 ip
|
||||
选项来作为阻止 ip, 但默认值是 0.0.0.0, 这是我使用的值。
|
||||
|
||||
|
||||
我曾经将 Steven 脚本做的事每隔一段时间就手动做一遍,进到每一个站点,将他们的列表拷贝/粘贴到我的主机文件中,做一个查找替换将其中一些变成 0 等等。我知道整件事情可以自动化,这样做有点傻,但我从来没有花时间解决这个问题。直到我找到这个脚本, 现在这已经是一个被遗忘的杂务。
|
||||
|
||||
让我们先下载一份 Black 代码拷贝(大约 150MB),以便我们可以进行下一步。你需要安装 git,因此如果还没安装,进入到终端输入:
|
||||
|
||||
**sudo apt-get install git**
|
||||
|
||||
安装完之后,输入:
|
||||
|
||||
**mkdir unifiedhosts**
|
||||
**cd unifiedhosts**
|
||||
**git clone** [**https://github.com/StevenBlack/hosts.git** ][2]
|
||||
**cd hosts**
|
||||
|
||||
当您的光标在 Steven 的脚本中闪烁时,让我们来谈谈选择。该脚本有几个选项和扩展,但扩展我不会在这里覆盖,但如果你到了这一步并且你有兴趣,[readme.md][3] 可以告诉你所有你需要知道的。
|
||||
|
||||
你需要安装 python 来运行此脚本,并且与版本有关。要找到你安装的 Python 版本,请输入:
|
||||
|
||||
**python --version**
|
||||
|
||||
如果你还没安装 Python:
|
||||
|
||||
**sudo apt-get install python**
|
||||
|
||||
对于 Python 2.7,如下所示,输入 “python” 来执行脚本。对于 Python 3,在命令中的 “python” 替换成 “python3”。执行后,该脚本会确保它具有每个列表的最新版本,如果没有,它会抓取一个新的副本。然后,它会写入一个新的 hosts 文件,包括了黑名单/白名单中的任何内容。让我们尝试使用 -r 选项来替换我们的活动主机文件以及 -a 选项,以便脚本不会发生任何问题。回到终端:
|
||||
|
||||
**python updateHostsFile.py -r -a**
|
||||
|
||||
该命令将询问你的密码,以便能够写入 /etc/。为了使新更新的列表处于活动状态,某些系统需要清除 DNS 缓存。在同一个硬件上,我观察到不同的操作系统表现出非常不同的行为,在没有刷新缓存的情况下不同的服务器变为可访问/不可访问所需的时间长度都不同。我已经看到了从即时(Slackware)到重启更新(Windows)的所有情况。有一些命令可以刷新 DNS 缓存,但是它们在每个操作系统甚至每个发行版上都不同,所以如果没有生效,只需要重新启动就行了。
|
||||
|
||||
现在,只要将你的个人例外添加到黑名单/白名单中,并且只要你想要更新主机文件,运行该脚本就好。该脚本将根据你的要求调整生成的主机文件,每次运行文件时会自动追加你额外的列表。
|
||||
|
||||
最后,我们来创建一个打开/关闭开关。我们每个功能都需要一个脚本,所以回到终端输入下面的内容创建关闭开关(用你自己的文本编辑器替换 leafpad):
|
||||
|
||||
**leafpad hosts-off.sh**
|
||||
|
||||
在新文件中输入下面的内容:
|
||||
|
||||
**#!/bin/sh**
|
||||
**sudo mv /etc/hosts /etc/hostsDISABLED**
|
||||
|
||||
接着让它可执行:
|
||||
|
||||
**chmod +x hosts-off.sh**
|
||||
|
||||
相似地,对于打开开关:
|
||||
|
||||
**leafpad hosts-on.sh
|
||||
**
|
||||
在新文件中输入下面的内容:
|
||||
|
||||
**#!/bin/sh**
|
||||
**sudo mv /etc/hostsDISABLED /etc/hosts
|
||||
**
|
||||
最后让它可执行:
|
||||
|
||||
**chmod +x hosts-on.sh
|
||||
|
||||
**你所需要做的是为每个脚本创建一个快捷方式,标记它们的 HOSTS-ON 和 HOSTS-OFF,你就能找到它们了。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.darrentoback.com/this-script-updates-hosts-files-using-a-multi-source-unified-block-list-with-whitelisting
|
||||
|
||||
作者:[dmt][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.darrentoback.com/about-me
|
||||
[1]:https://github.com/StevenBlack/hosts
|
||||
[2]:https://github.com/StevenBlack/hosts.git
|
||||
[3]:https://github.com/StevenBlack/hosts/blob/master/readme.md
|
Loading…
Reference in New Issue
Block a user