安装Pi-hole,让树莓派为你过滤家中广告

最近在使用DietPi这个魔改版Raspbian系统时意外的发现了这个神奇的软件(软件主页:https://pi-hole.net/,正如标题所说,它可以帮你过滤整个家中的广告,但不需要繁琐地针对每台设备进行设置,算是对非智能路由及低性能路由无法过滤广告的一种解决方案。

下面就讲解一下具体的安装方法咯。

注:同样适用于其他Linux设备

0.开始之前

首先你需要为你的树莓派安装好系统,然后你可能需要针对自家的网络环境为你的树莓派设置好静态IP,不同的路由器设置方法有一定差别,具体方法自行百度(关键词:路由品牌 静态IP)。

例如我刷了LEDE的路由器的设置在这里:

不过近几年的路由器大多会针对不同的设备设置固定的IP地址,所以如果你确定自己树莓派的IP地址不会变动,不设置也是OK的。

1.安装Pi-hole

Pi-hole的安装过程比较简单,只需在终端中输入一条命令即可:

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

安装过程中你可以喝杯咖啡,看会儿书,刷会儿微博,直到你进入这个界面:

一路Enter,直到这里:

选择其中一个DNS提供商,或者选择最后一项自定义:

选好之后接着一路Enter,然后你继续可以喝杯咖啡,看会儿书,刷会儿微博,直到这里:

这时就已经安装完成了,访问上面给出的网址,输入密码即可进入后台。

如果需要修改密码,在终端中输入:

pihole -a -p newpassword

 

2.配置路由

光设置好树莓派是没卵用的,毕竟我们要过滤的是整个局域网的广告,所以我们要针对路由器进行配置。

Pi-hole采用的是使用本地DNS服务器的方式在局域网中进行过滤,所以我们需要设置Pi-hole为当前网络的DNS。

方法1
这种方法适用于大部分路由器,直接将DNS设置在路由器上。但需要注意的是,应当将DNS设置在LAN接口上(区别于WAN),在我这里是这样的:

 

方法2(推荐)
这种方法适用于一些刷了OpenWRT(LEDE),DD-WRT,Tomato等自定义程度较高固件的路由器,推荐使用该方式

在DHCP选项中填入:6,树莓派IP地址

 

注:若使用该方式,建议将上游DNS设置为路由器

方法3
如果这些选项在你的路由器里都没有,Pi-hole也内置了一个DHCP服务器,具体设置方法不多阐述。

方法4
如果你愿意的话,你也可以针对每一台设备进行设置,方便针对不同的设备进行过滤,具体设置方法不多阐述,自行百度。

不管你使用的哪一种方式,设置完之后都建议重启一下路由器或开关一下飞行模式,让设备使用新的DNS。

3.配置Pi-hole

实际上进行完上面的步骤就已经可以使用了,下面就针对一些常用的功能进行说明。

过滤规则
过滤的规则在这里,可以看到已内置了一些规则:

实际上,Pi-hole使用的是hosts方式对广告进行过滤(类似AdAway),默认的规则对于国内环境不太友好,下面就附上适用于国内环境的一些hosts:

  • yhosts:可以说是国内比较不错的hosts,定期有更新,奈何作者的唯我主义连带屏蔽了一些非广告网站,可以在终端中手动添加白名单:
pihole -w `curl -sSL https://raw.githubusercontent.com/vokins/yhosts/master/data/moot/cps.txt https://raw.githubusercontent.com/vokins/yhosts/master/data/moot/error.txt https://raw.githubusercontent.com/vokins/yhosts/master/data/moot/hate.txt | sed '/^@/d; /^#/d; s/127.0.0.1 //' | tr "\n" " "`
  • neoHosts:比较小众的hosts,看介绍貌似是不满于上面那位而弄出来的。
  • MWSL-hostslist:恶意网站实验室可能没多少人知道,不过其提供的OneDNS可就算是比较有名的了,这个hosts正是OneDNS中所用于屏蔽恶意网站的规则。
  • AdAway:也就是安卓上的AdAway所使用的规则,不过针对中国误杀现象严重,慎用。

如果还有哪些不错的去广告hosts,欢迎在评论区中留言。

相较于通常的过滤方式(如Adblock Plus、KoolProxy等),利用hosts过滤有自己的优缺点。优点是不会拖累网速(甚至可能有一定提升),缺点是过滤效果不尽如人意。

不过如果你要是想要利用这个做一些有违我国国情的事情,放心,那样的话就相当于在你家里再上了一道防火长城(Pi-hole只能将规则中的网站解析到自身以进行屏蔽)。

黑白名单

 

顾名思义就是对过滤规则的手动调整,黑名单分为匹配和通配两种,建议那些整天说退游的同学可以试试。白名单主要是针对一些有防过滤广告或被误杀的网站。

隐私设置

 

在这里你可以设置让一些不想在日志中出现的网站或设备隐藏起来,至于有什么用嘛……那各位自己心里的B数都很清楚啊……

Pi-hole上的一些其他高级功能这里就不多赘述了,有待自行探索。

4.Enjoy it!

到这里关于Pi-hole的全部设置就完成了,打开浏览器访问一些广告较多的网站,看看广告是否被屏蔽了、仪表盘上的数字是否增加,如果没有再仔细按照上面的步骤检查一遍,尤其是关于DNS设置的部分。

好了,和广告Say Goodbye吧!

本文来自:树莓派实验室

 

DietPi 6.0 发布,适用于树莓派

DietPi 是一个基于 Debian 的 Linux 发行版。它主要为 Raspberry Pi 树莓派等单板设备开发,但也可运行于包含 x86 及 Odroid 机器在内的其它架构中。其 1 月 28 日发布的最新版本 DietPi 基于 Debian Stretch (除了 Odroid 的之外)。

在这一版本中:所有镜像已被重新创建;已有的安装( v159 及以下)无法被升级且不再被支持;用户需安装最新的 v6.0 镜像以继续获得支持; 基于 ARMbian 的镜像目前包含主线内核 4.13+  。  Native PC (EFI) 现在是一个 ISO ,配备了 clonezilla ;通过 Rufus write 简化了安装。

详细内容或其它变化见于其发布通告

DietPI 拥有如下特性:

  • 轻量:镜像文件大小最低为 400 MB 左右,比 Raspbian Lite 轻 3 倍;
  • 优化了 CPU 及内存资源的使用,确保用户的单板设备的稳定运行;
  • 简单的界面:使用轻量级的 Whiptail 菜单,让用户花费更少的时间于命令行;
  • DietPi-Software 快速简便得安装经过优化的常见软件;
  • DietPi-Config: 个性化配置设备的软硬件;
  • DietPi-Backup: 快速简便地备份及恢复 DietPi 系统;
  • 用户可以选择日志程度,使用 DietPi-Ramlog 或 rsyslog 及 rotate 以记录重要事件并获得性能提升;
  • DietPi-Process 工具:控制软件的优先级( nice 、 affinity 及策略调度);
  • DietPi-Update 系统: DietPi 自动检查、提示可用更新并可以快速应用更新;
  • DietPi-Automation :允许用户在启动之前简单地配置好 dietpi.txt 以完全自动化 DietPi 的安装而无需用户输入。

DietPi 支持 RaspberryPi 、 NanoPi 、 OrangePi 等常见单板设备并提供了详细的教程文档

封面图片原由 mrneilypops 载于 deviantart.com 。

本文来自 cnBeta,部分文字转录或衍生自 DistroWatch.com 的对应页面与 DietPi 官网

DietPi——树莓派上的轻量级系统

随着树莓派的逐步发展,官方的Raspbian系统在一步步完善和开发,软件和资源越来越多,往往也意味着慢慢地臃肿。
而DietPi就是在这样的背景下被开发出来,在DietPi系统中,默认只有一些必要的软件,其他的任何不需要的软件在初始化的DietPi上面都是不存在的;在干净和简洁的同时,在DietPi上安装需要的软件也比其他软件方便和简单。

安装配置DietPi

初次配置时需要网络连接

下载镜像

在官网的下载页面下载对应的板子的镜像。

解压并烧写

  • 在Windows上可以使用Win32DiskImager
  • 在Linux上可以用dd命令
dd if=/path/to/DietPi_vXX.img of=/dev/sdb

配置WiFi(可选)

将烧写好的SD卡插入电脑,和Raspbian类似,DietPi上有一个文件dietpi.txt也是用来做开机时的配置。

在这个文件里面设置好

Wifi_Enabled=1
Wifi_SSID=MySSID
Wifi_KEY=MyWifiKey

给板子上电

把SD卡插入板子,然后上电,之后DietPi会自动重启两次来完成初次的配置。

登录

默认的用户名是root,密码是dietpi
假如上面的WiFi配置成功了,那么这里也能直接ssh了,DietPi默认装了Dropbear SSH Server。

DietPi默认是没有桌面环境的,所以这个时候还是只能终端操作。

选择软件安装

接下来就可以选择你需要的软件来进行个性化的安装了,可以安装的软件包括以下这么多选择:

桌面

  • LXDE – 高度优化的轻量级桌面
  • MATE – Gnome 2桌面
  • XFCE – 轻量级桌面
  • GnuStep – 另类的桌面系统
  • Chromium – 浏览器

远程桌面

  • Tightvnc Server – 远程桌面软件
  • VNC4 Server – 远程桌面软件
  • NoMachine – 功能更丰富的远程桌面软件
  • XRDP – 针对Windows客户端的远程桌面软件
  • RealVNC Server – 远程桌面软件

媒体系统

  • Kodi – Media Centre / Player
  • YMPD – Lightweight web Interface audio player for MPD
  • O!MPD – Feature-rich web Interface audio player for MPD
  • CAVA – Optional: Console-based audio visualizer for MPD
  • Mopidy – Web Interface Music /Radio Player
  • SubSonic – Feature rich media streaming server with web interface.
  • SqueezeBox – Also known as Logitech Media Server.
  • SqueezeLite – Audio player for SqueezeBox.
  • Shairport Sync – AirPlay audio player with multiroom sync
  • BruteFIR – EQ and Digital Room Correction via ALSA.
  • ReadyMedia – (MiniDLNA) Media streaming server (DLNA, UPnP)
  • Ampache – Web interface media streaming server.
  • Emby – Web interface media streaming server.
  • Plex Media Server – Web interface media streaming server.
  • PlexPy – Monitoring and tracking tool for Plex Media Server.
  • Murmur – Mumble VoIP Server
  • Roon Bridge – Turns your device into a Roon capable audio player
  • Roon Server – Turns your device into a Roon capable audio player and Roon core
  • NAA Daemon – Signalyst Network Audio Adaptor (NAA).
  • IceCast – Shoutcast Streaming Server, including DarkIce.
  • jRiver – Media Center
  • Koel – Web interface streaming server.

BT/下载

  • Transmission – Lightweight BitTorrent server with web interface
  • Deluge – Alternative BitTorrent server with web interface
  • qBitTorrent – Lightweight and fast (c++) BitTorrent server with web interface
  • rTorrent – BitTorrent server with ruTorrent web interface
  • Aria2 – Download manager with web interface
  • SABnzbd – NZB download manager with web interface.
  • SickRage – Automatically download TV shows
  • Sonarr – Automatically download TV shows
  • Radarr – Automatically download Movies
  • CouchPotato – Automatically download movies.
  • Jackett – API Support for your favorite torrent trackers.
  • NZBget – NZB download manager with web interface.
  • HTPC Manager – combines all your favorite software into one slick interface.

模拟器和游戏客户端

  • OpenTyrian – Gaming
  • Cuberite – Fast Minecraft server with web interface
  • MineOS – Multiple Minecraft servers with web interface
  • AmiBerry – Amiga emulation system, further developed optimized builds of uae4arm-rpi
  • DXX-Rebirth – Descent 1 & 2 OpenGL port
  • Steam – Steam client

相机

  • DietPi-Cam – RPi Camera / Web Interface Surveillance
  • MotionEye – Camera / Web Interface Surveillance

云/备份

  • OwnCloud – Your own personal cloud based backup/data storage system
  • NextCloud – Your own personal cloud based backup/data storage system
  • Pydio – Feature-rich backup and sync server with web interface.
  • UrBackup Server – Full backups for systems on your network
  • Gogs – GitHub style server, with web interface.
  • Syncthing – Backup and sync server with web interface.
  • Tonido – Lightweight backup and sync server with web interface, and, cloud access.

社交/搜索

  • Forums – phpbb3
  • WordPress – Website Blog and Publishing platform.
  • Image Gallery – Host and browse your images from a web interface.
  • BaiKal – Lightweight CalDAV + CardDAV server.
  • OpenBazaar – Decentralized peer to peer market server using BitCoin.
  • YaCy – Decentralized open source search engine.

WiFi/热点

  • WiFi HotSpot – Turn your device into a wireless hotspot/access point.
  • Tor HotSpot – Optional: Routes all WiFi HotSpot traffic through the Tor network.

智能家居

  • EmonPi – Lightweight Energy usage stats with EmonPi PCB.
  • Grasshopper – Web App to control Bticino MyHome

硬件项目

  • RPi.GPIO – GPIO Interface library for RPi (python).
  • WiringPi – GPIO Interface library (c).
  • WebIOPi – Web interface to control RPi GPIO.
  • I2c – Enables support for I2c based hardware.
  • Node-Red – Visual tool for wiring together hardware devices, APIs and online services.
  • Mosquitto – Message broker that implements MQTT protocol versions 3.1 and 3.1.1.
  • Blynk Server – iOs and Android apps to control Arduino, ESP8266, Raspberry Pi and similar microcontroller boards over the Internet.

远程访问

  • Remot3.it – (Weaved) Access your device over the internet.
  • VirtualHere – Share physically attached USB devices from your SBC, over the network.

系统管理和监控

  • DietPi-Cloudshell – Lightweight system stats for your LCD display or monitor.
  • Raspcontrol – Web interface system stats
  • Linux Dash – Web interface system stats
  • PhpSysInfo – Web interface system stats
  • RPi Monitor – Web interface system stats
  • NetData – Web interface system stats
  • Webmin – Remote system management with web interface
  • Open Media Vault – Web interface network attached storage (NAS) solution

系统安全

  • Fail2Ban – Protects your system from brute-force attacks

Web服务器栈

  • LAMP Webserver – Apache2 / MySql / PHP
  • LASP Webserver – Apache2 / SQLite / PHP
  • LAAP Webserver – Apache2 / MariaDB / PHP
  • LEMP Webserver – Nginx / MySql / PHP
  • LESP Webserver – Nginx / SQLite / PHP
  • LEAP Webserver – Nginx / MariaDB / PHP
  • LLMP Webserver – Lighttpd / MySql / PHP
  • LLSP Webserver – Lighttpd / SQLite / PHP
  • LLAP Webserver – Lighttpd / MariaDB/ PHP
  • phpMyAdmin – Web interface SQL admin tool
  • Certbot – Free, automated SSL cert creation and setup, allowing https.
  • Tomcat8 – Apache Tomcat server

DNS 服务器

  • Pi-hole – A DNS/Web server that will block ads for any device on your network.

文件服务器

  • ProFTP – Simple, efficient, lightweight FTP file server.
  • Samba – Feature rich file server.
  • vsFTPD – Feature rich FTP file server.
  • NFS – Network file system server

VPN 服务器

  • OpenVPN – Easy to use, minimal hassle VPN server
  • PiVPN – OpenVPN installer & management tool

网络负载均衡

  • HaProxy – High performance TCP/HTTP load balancer.

网站域名

  • NoIp – Website URL Address

打印

  • CloudPrint – CUPS print server, with support for Google cloud printing
  • OctoPrint – Web interface for controlling 3D printers

文件服务器选择

  • ProFTP – Simple, efficient, lightweight file server.
  • Samba – Feature rich file server.

SSH选择

  • Dropbear – Lightweight SSH Server
  • OpenSSH – Feature rich SSH server with SFTP/SCP support.

日志系统选择

  • DietPi-Ramlog – Lightweight RAM logging.
  • Full – Full logging system with Rsyslog and Logrotate.

网络服务器选择

  • Apache2 – Feature-rich webserver
  • Nginx – Lightweight webserver
  • Lighttpd – Extremely lightweight webserver

预装软件:

  • dietpi-launcher (Select and Run any of the DietPi programs, all from one place.)
  • dietpi-config (Feature rich configuration tool for your device)
  • dietpi-software (Install optimized and ready to run software)
  • dietpi-uninstall (Allows you to uninstall DietPi software)
  • dietpi-update (Update your version of DietPi)
  • dietpi-backup -(Backup or restore your DietPi system)
  • dietpi-sync – (Allows you to sync/duplicate one directory to another).
  • dietpi-services – (Takes control of software services, enabling DietPi a method of quick service control).
  • dietpi-process_tool – (Tweak system wide nice/priority levels, for most software and processes).
  • dietpi-cleaner (Remove unwanted “junk” from your DietPi system and free up filesystem space)
  • dietpi-bugreport (Sends a bug report to DietPi)
  • dietpi-cron (Allows you to modify all start times for Cron Jobs)
  • dietpi-logclear (Keep ontop of your log files, features clearing and backup modes).
  • dietpi-morsecode (Converts a text file to morse code, then outputs to your screen)
  • dietpi-letsencrypt (Frontend for Lets Encrypt and DietPi integration)
  • htop (The only resource monitor you’ll ever need)
  • DietPi-Ramlog #1 (Mounts /var/log to RAM. Saves your SD card writes, and, uses less than 0.1mb~ of RAM).
  • Dropbear (Lightweight SSH server)

预装脚本

  • treesize (Shows current directory/file sizes, recursive)

 

dietpi.txt 快速配置上手

DietPi是所谓的超级简单系统,一般人都知道怎么操作他,他非常轻量,也很容易适合本土化.引用老外的话就是:

“I’m a Linux noob and I have been playing around with installing things on my new Raspberry Pi–the hard way–doing manual installs! The amount of hours that goes into something like this is staggering. And then I discovered DietPi just yesterday. What a time-saver!”

他大概就是说,我就说个菜鸟,但是用这个我发现我会使用RPI了.而且对比DietPi和RPI官方系统Raspbian还是差别挺大的.

首先Raspbian的官方系统比较笨重,他已经没对比了,他对比的是Raspbian Lite.明显各项指标都很强大,但是经过我们的”优化”调整后,这更强大.其中我们很熟悉Raspbian的config.txt,而dietpi,他也有dietpi.txt,功能远比config.txt强大.

我们先打开这个文件:

逐一介绍下:

  • Ethernet_Enabled,如果设置为1,就是用以太网功能,如果设置为0,就是不用以太网功能.
  • Wifi_Enabled,如果设置为1,就是用以太网功能,如果设置为0,就是不用以太网功能.
  • Wifi_SSID和Wifi_KEY就是你的WIFI名字和密码,如果你没有屏幕键鼠,还没有以太网线,更没有串口调试,这个就非常实用了.
  • Use_Static就是使用静态IP地址,下面就是他的配置了.
  • Hostname就是你在路由上看到的名字,也就是设备名,如果你有多个设备就得区分下,他默认是DietPi,比如我一个Pi 3一个Pi B+,那么我要区分就得改他名字.
  • Ethernet_Force_Speed就是强制以太网速率,0就是自动识别,一般够用了,接百兆网就是百兆,1是10M网,1000是100M网,1000就是千兆网了.
  • Swapfile_Size就是设定SWAP文件的大小,一般设置为1就是自动,0就是不做SWAP,当然不推荐禁用SWAP了,除非你卡非常小,如果不是1也不是0,就你设定一个SWAP大小了.
  • AUTO_Install_Enable第一次启动DietPi总有提示,然后安装软件,比如在吃饭前你刚烧写好个系统,然后还要插上去操作一番才能离开,如果使能这个开关,设置为1,那么,这都不是问题.烧写好,然后插卡,去吃饭,回来就好了.
  • AUTO_Global_Password全局密码,包括用户名啊,Mysql啊各种都是默认这个密码,改掉安全一些.
  • AUTO_DietpiSoftware_Install_ID就是自动安装软件,比如我想首次启动帮我安装好LAMP,那么找到LAMP套件的ID,然后写AUTO_DietpiSoftware_Install_ID=74 (假设LAMP的ID是74.),当然,还要使能AUTO_Install_Enable,然后烧写好之后去吃饭,吃饭后回来软件也装好了.当然,这里软件包甚至包括WordPress.
  • AUTO_DietpiSoftware_SSHServerIndex是选择SSH服务提供组件,默认是dropbear,也就是-1,如果要用openssh,选-2就可以了,dropbear一般够用,openssh当然也有更多功能,如果根本用不到SSH,可以选0,关闭,更省资源.
  • AUTO_DietpiSoftware_FileServerIndex是选择文件服务器提供组件,默认是不开的,你可以用-1 ftp方式,-2 samba方式.
  • AUTO_DietpiSoftware_LoggingIndex是日志记录模式,默认是-1,也就是ramlog,0是跟Raspbian Lite一样记录方式,选ramlog的话,重启日志就没了,但是磁盘(TF)读写少,选-2 ramlog并每小时保存是个折中的办法.
  • AUTO_DietpiSoftware_WebserverIndex是Web提供组件,需要配合LAMP等一起用哦,0就是Apache2,最费资源但是最多功能,-1是Nginx,-2是Lighttpd.最后一个最轻量,也最少功能.
  • AUTO_AutoStartTarget是设置启动后默认状态,0是烧写系统后默认的,就是命令行不登录,2就是桌面环境.
  • AUTO_Timezone就是自动设置时区,这个也是依靠AUTO_Install_Enable使能的,比如我们设置成Asia/Shanghai,就是国内时间了.
  • AUTO_Locale就是本地化,类似语言,这个也是依靠AUTO_Install_Enable使能的,看英文看不爽,先预设成中文就爽了.默认是en_GB,我们换成zh_CN.
  • AUTO_KeyboardLayout就是键盘布局设置,这个也是依靠AUTO_Install_Enable使能的,当然你没键盘设置什么都是白扯的,有键盘的话,设置这个为us,就符合我们中国人键盘了.
  • rpi_hdmi_output 是针对RPI的了,如果设置为0,节省点RAM,因为不用启动显示功能了,做服务器尤为好用.
  • cpu_governor就是CPU调节器,对于Pi 3来说,performance就是固定1.2G,powersave就是固定600MHz,默认ondemand就是没事情时候低主频,有事时候快速升高.
  • SambaClient_ComputerName,SambaClient_ShareName,SambaClient_Username.SambaClient_Password就是配置samba的用户名密码名称的,很好理解啦.
  • boot_wait_for_network默认是1,就是等网络连接,可以设置成0,不等网络连接就开机,开机后网络也是不受影响的.对于没有网络连接的人来说,大胆禁用吧,对于只有无线但是不想在配置文件设置密码的,也可以禁用.
  • dietpi_check_for_updates为1就是可以检查更新,否则不检查更新.默认当然更新.
  • ntpd_update_mode是NTP时间同步的时刻,默认是开机同步,其实国内环境开机同步很费时,可以设置成4,后台同步.
  • wifi_hotspot_ssid,wifi_hotspot_key,wifi_hotspot_channel没有WIFI又没有以太网怎么操作,那就让Pi成为热点吧,这三个参数设置热点,名称啊密码,这样设置后,你就可以通过连接热点方式连接Pi,而不需要路由等附件了.
  • serial_console_enabled就是是否开启串口登录,又不想开热点,又没法连网络,比如在飞机上,你还是想玩玩,那么就用串口吧,默认是0,1是开启,开启后Pi的蓝牙就不好用了.
  • prefer_ipversion可以指定是否支持IPV6,默认auto是自动检测,其实国内IPV6除了高校,其他都支持不太好,可以直接force到ipv4,就是参数填ipv4,这样就很方便了.
  • Apt_Raspbian_Mirror就是设置树莓派源,还在讨厌树莓派源特别慢?不如把源改成http://mirrors.ustc.edu.cn/raspbian/raspbian/ 在国内可是神速.
  • dietpi_vncserver_*,如果没有显示器,还想要图形界面,VNC是你的选择,以前Raspbian时候VNC分辨率就是很小,感觉不好用,现在可以在配置文件直接配置VNC分辨率了.