在你的网络中使用树莓派和 Pi-hole 拦截广告
树莓派 广告

> 
> 痛恨上网时看到广告？学习这篇教程来设置 Pi-hole。
> 
> 
> 


![](/data/attachment/album/201803/25/224601p4880iu8j88sg808.png)


有一个闲置的树莓派？在浏览网页时讨厌广告？[Pi-hole](https://pi-hole.net/) 是一个拦截广告的开源软件项目，它可以将你的家庭网络上的所有广告路由到一个不存在的地方，从而实现在你的设备上拦截广告的目的。这么好的方法只需要花几钟的时间来设置，你就可以使用它了。


Pi-hole 拦截了超过 100,000 个提供广告的域名，它可以拦截任何设备（包括移动设备、平板电脑、以及个人电脑）上的广告，并且它是完整的拦截了广告，而不是仅将它们隐藏起来，这样做可以提升总体的网络性能（因为广告不需要下载）。你可以在一个 web 界面上、或者也可以使用一个 API 来监视性能和统计数据。


### 你需要：


* 树莓派 + SD 卡
* USB 电源线
* 以太网线


![](/data/attachment/album/201803/25/224601p7797ql779lrb46l.png) ![](/data/attachment/album/201803/25/224602d32enc2kd769cibk.png) ![](/data/attachment/album/201803/25/224602q5s73n7rcb7b7irz.png) ![](/data/attachment/album/201803/25/224603aevc6kc9kvapze65.png)


你不需要使用一个最新型号的树莓派 — 一个老款足够完成这项工作，只要它的内存不小于 512MB 就可以 — 因此一个一代树莓派 Model B（rev 2）就足够，一个 Model B+、或者二代的或者三代的树莓派都可以。你可以使用 Pi Zero，但需要一个 USB micro 以太网适配器。你可以使用一个带 WiFi 的 Pi Zero W 而不是以太网。但是，作为你的网络基础设施的一部分，我建议你使用一个性能良好、稳定的有线连接来代替 WiFi 连接。


### 准备 SD 卡


开始的第一步，你可能需要将 Raspbian Stretch Lite 安装到一个 SD 卡上。SD 卡至少需要 4GB 大小（完整的桌面版 Raspbian 镜像至少要 8GB，但是 Lite 版镜像更小更轻量化，足够完成这项工作）。如果你喜欢，也可以使用完整的 Raspbian 桌面版镜像，但是作为一个去运行简单应用程序的树莓派，你没必要做更多的事情。


![](/data/attachment/album/201803/25/224604qbzdbo3j1aegc9dz.png)


使用你的个人电脑，从树莓派的网站上下载 Raspbian Stretch Lite 镜像。解压它并提取出里面的 `.img` 文件，然后将这个 `.img` 文件写入到你的 SD 卡。不论你的 SD 卡是否是空白的，这一步都不会有什么麻烦，因为在写入前它会清空上面的数据。


如果你使用的是 Linux，写入镜像文件更简单的办法是使用命令行工具 `dd`。或者，你也可以使用跨平台的软件 [Etcher](https://etcher.io/) （可以去参考 Les Pounder 写的指南 “[如何为树莓派准备 SD 卡](https://opensource.com/article/17/3/how-write-sd-cards-raspberry-pi)“）。


![](/data/attachment/album/201803/25/224605nlbtf2i8fviib6f1.png)


SD 卡准备完成之后，你可以将它插入到你的树莓派，连接上键盘、显示器和以太网，然后为树莓派接上电源。在初始化设置之后，这个树莓派就不需要键盘或显示器了。如果你有使用“<ruby> 无末端 <rt>  headless </rt></ruby>”树莓派工作的经验，你可以 [启用 SSH](https://www.raspberrypi.org/blog/a-security-update-for-raspbian-pixel/) 然后去设置它 [启用远程连接](https://www.raspberrypi.org/documentation/remote-access/ssh/README.md)。


### 安装 Pi-hole


在你的树莓派引导完成之后，用缺省用户名（`pi`）和密码（`raspberry`）登入。现在你就可以运行命令行了，可以去安装 Pi-hole 了。简单地输入下列命令并回车：



```
curl -sSL https://install.pi-hole.net | bash

```

这个命令下载了 Pi-hole 安装脚本然后去运行它。你可以在你的电脑浏览器中输入 `https://install.pi-hole.net` 来查看它的内容，你将会看到这个脚本做了些什么。它为你生成了一个**管理员密码**，并和其它安装信息一起显示在你的屏幕上。


就是这么简单，几分钟之后，你的树莓派将准备好为你拦截广告。


在你断开树莓派连接之前，你需要知道它的 IP 地址和你的路由器的 IP 地址。（如果你不知道），在你的终端中输入 `hostname -I` 来查看你的树莓派的 IP 地址，输入 `ip route | grep default` 来找到你的路由器的 IP 地址。你看到的将是像 `192.168.1.1` 这样的地址。


### 配置你的路由器


你的树莓派现在运行着一个 DNS 服务器，接下来你需要告诉你的路由器去使用 Pi-hole 作为它的 DNS 服务器而不是你的 ISP 提供给你的缺省 DNS。进入路由器的管理控制台 web 界面。这个界面一般是输入你的路由器的 IP 地址来进入的。


找到 LAN 设置下面的 DHCP/DNS 设置，然后将你的主 DNS 服务器的 IP 地址设置为你的 Pi-hole 的 IP 地址。设置完成之后，它应该你下图的样子：


![](/data/attachment/album/201803/25/224605bjtz8k18c9jkck71.png)


关于这一步的更多信息，可以查看 [Pi-hole discourse](https://discourse.pi-hole.net/t/how-do-i-configure-my-devices-to-use-pi-hole-as-their-dns-server/245)。


你还需要确保你的 Pi-hole 始终保持相同的 IP 地址，因此，你需要去查看 DHCP 设置，将你的树莓派的 IP 地址条目添加到保留地址中。


### 外部测试


现在，在命令行下输入 `sudo halt` 关闭运行的树莓派，并断开它的电源。你可以拔掉显示器连接线和键盘，然后将你的树莓派放置到一个合适的固定的地方 — 或许应该将它放在你的路由器附近。确保连接着以太网线，然后重新连接电源以启动它。


在你的个人电脑上导航到一个网站（强烈建议访问 [Opensource.com](https://opensource.com/) 网站），或者用你的 WiFi 中的一个设备去检查你的因特网访问是否正常（如果不能正常访问，可能是你的 DNS 配置错误）。如果在浏览器中看到了预期的结果，说明它的工作正常。现在，你浏览网站时，应该再也看不到广告了！甚至在你的 app 中提供的广告也无法出现在你的移动设备中！祝你“冲浪”愉快！


如果你想去测试一下你的广告拦截的新功能，你可以去这个 [测试页面](https://pi-hole.net/pages-to-test-ad-blocking-performance/) 尝试浏览一些内置广告的网站。


现在你可以在你的电脑浏览器上输入 Pi-hole 的 IP 地址来访问它的 web 界面（比如，`http://192.168.1.4/admin` 或者 `http://pi.hole/admin` 也可能会工作）。你将看到 Pi-hole 管理面板和一些统计数据（在这时可能数字比较小）。在你输入（在安装时显示在屏幕上的）密码后，你将看到更漂亮的图形界面：


![](/data/attachment/album/201803/25/224605tbppluppg65lgq24.png)


你也可以微调你的 Pi-hole 的设置，像域名的白名单和黑名单、永久和临时禁止、访问拦截统计信息等等。


个别情况下，你可能需要去升级你的 Pi-hole 安装。当软件需要更新时，这个 web 界面会出现一个更新提示。如果你启用了 SSH，你可以远程登入，否则，那你只能再次连接键盘和显示器。远程登入之后，输入 `pihole -up`命令来更新它。


![](/data/attachment/album/201803/25/224606hr2qrptlm1gqprsx.png)


如果你使用过 Pi-hole 或者其它的开源广告拦截器，请在下面的评论区把你的经验共享出来。




---


via: <https://opensource.com/article/18/2/block-ads-raspberry-pi>


作者：[Ben Nuttall](https://opensource.com/users/bennuttall) 译者：[qhwdw](https://github.com/qhwdw) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Got an old Raspberry Pi lying around? Hate seeing ads while browsing the web? [Pi-hole](https://pi-hole.net/) is an open source software project that blocks ads for all devices on your home network by routing all advertising servers into nowhere. What's best is it takes just a few minutes to set up.

Pi-hole blocks over 100,000 ad-serving domains, blocks advertisements on any device (including mobiles, tablets, and PCs), and because it completely blocks ads rather than just hiding them, this improves overall network performance (because ads are never downloaded). You can monitor performance and statistics in a web interface, and there's even an API you can use.

## What you will need

- Raspberry Pi + SD card
- USB power cable
- Ethernet cable

![]() opensource.com |
![]() opensource.com |
![]() opensource.com |
![]() opensource.com |


You don't need a recent Raspberry Pi model—an older one will do the job, as long as it's got at least 512MB RAM—so a Pi 1 Model B (rev 2), a Model B+, or a Pi 2 or 3 would do. You could use a Pi Zero, but you'll need a USB micro Ethernet adapter too. You could use a Pi Zero W with WiFi rather than Ethernet, but as it's part of your network infrastructure, I'd recommend a good, solid, wired connection instead.

## Prepare the SD card

First of all, you'll probably want to install Raspbian Stretch Lite onto an SD card. The card should be at least 4GB (the full desktop Raspbian image requires at least 8GB but the Lite image is, well, lighter). You can use the full Raspbian desktop image if you prefer, but as this is going to be running as a headless application, you don't need anything more.

![Raspbian downloads](https://opensource.com/sites/default/files/u128651/raspbian-downloads.png)

opensource.com

Using your main PC, download the Raspbian Stretch Lite image from the Raspberry Pi website. Now unzip it to extract the `.img`

file within, and you'll write this image to your SD card. It doesn't matter if the SD card is blank or not, as everything on it will be wiped.

If you're using Linux, the easiest way to write the image is using the command-line tool `dd`

. Alternatively, you can use cross-platform software [Etcher](https://etcher.io/) (follow the guide "[How to write SD cards for the Raspberry Pi](https://opensource.com/article/17/3/how-write-sd-cards-raspberry-pi)" by Les Pounder).

![An image showing the Etcher application running on Windows 10 An image showing the Etcher application running on Windows 10](https://opensource.com/sites/default/files/etcher-win-520px.png)

Les Pounder, CC-BY-SA 4.0

opensource.com

Once your SD card image has burned, you can insert it into your Raspberry Pi, connect a keyboard, monitor, and Ethernet cable, and then plug in the power. After the initial setup, the Pi won't need the keyboard or monitor. If you're experienced in working with the Pi headless, you can [enable SSH](https://www.raspberrypi.org/blog/a-security-update-for-raspbian-pixel/) and set it up [remotely](https://www.raspberrypi.org/documentation/remote-access/ssh/README.md).

## Install Pi-hole

Once your Raspberry Pi boots up, log in with the default username (`pi`

) and password (`raspberry`

). Now you're at the command line, and you're ready to install Pi-hole. Simply type the following command and press Enter:

```
``````
curl -sSL https://install.pi-hole.net | bash
```


This command downloads the Pi-hole installer script and executes it. You can take a look at the contents by browsing `https://install.pi-hole.net`

in your browser and see what it's doing. It will generate an admin password for you, and print it to the screen along with other installer information.

That's it! In just a few minutes, your Pi will be ready to start blocking ads.

Before you disconnect the Pi, you need to know its IP address and your router's IP address (if you don't already know it). Just type `hostname -I`

in the terminal for the Pi's IP address, and `ip route | grep default`

to locate your router. It will look something like `192.168.1.1`

.

## Configure your router

Your Raspberry Pi is now running a DNS server, and you can tell your router to use Pi-hole as its DNS server instead of your ISP's default. Log into your router's management console web interface. This can usually be found by typing your router's IP address into your web browser's address bar.

Look for DHCP/DNS settings under LAN settings and set your primary DNS server to the IP address of the Pi-hole. It should look something like this:

![pi-hole DNS settings](https://opensource.com/sites/default/files/u128651/pi-hole-dns.png)

opensource.com

See the [Pi-hole discourse](https://discourse.pi-hole.net/t/how-do-i-configure-my-devices-to-use-pi-hole-as-their-dns-server/245) for more information on this step.

You'll also want to make sure your Pi-hole maintains the same IP address, so also look for DHCP IP reservations and add an entry for your Pi.

## Test it out

Now shut down the Pi by running the command `sudo halt`

and disconnecting the power. You can remove the monitor cable and the keyboard and place the Pi-hole in its permanent location—probably plugged in near your router. Ensure the Ethernet is connected and boot it back up by reconnecting the power.

Navigate to a website on your PC or a device on your WiFi (I highly recommend [Opensource.com](https://opensource.com/)) to check that your internet access is working correctly (if not, you may have misconfigured your DNS settings). If web browsing seems to work as expected, it's set up correctly. Now, when you browse the web, it should be ad-free! Even ads served within apps on your mobile devices won't get through! Happy surfing!

If you really want to flex your new ad-blocking muscles, you can try browsing some of the ad-ridden websites listed on this [test page](https://pi-hole.net/pages-to-test-ad-blocking-performance/).

Now access the Pi-hole's web interface by entering its IP address into your web browser's address bar (e.g. [http://192.168.1.4/admin](http://192.168.1.4/admin)

should work too). You should see the Pi-hole dashboard and some statistics (they will be very low at this stage). Once you login using the password you were given during installation, you'll see some pretty graphs too:[http://pi.hole/admin](http://pi.hole/admin)

![Pretty graphs in pi-hole](https://opensource.com/sites/default/files/u128651/pi-hole-web.png)

opensource.com

You can also tweak your Pi-hole's settings, like whitelist and blacklist domains, disable permanently or temporarily, access the stats for blocked queries, and more.

Occasionally, you'll want to upgrade your Pi-hole installation. The web interface includes an update notification when one is available. If you have enabled SSH, you can log in remotely, otherwise you'll have to reconnect the monitor and keyboard. When logged in, simply run the command `pihole -up`

.

![Updating pi-hole](https://opensource.com/sites/default/files/u128651/pi-hole-update.png)

opensource.com

Have you used Pi-hole or another open source ad blocker? Please share your experience in the comments.

## 8 Comments