前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >树莓派搭建无污染DNS服务器

树莓派搭建无污染DNS服务器

作者头像
T1Am0
发布2022-09-13 15:38:11
2.1K0
发布2022-09-13 15:38:11
举报
文章被收录于专栏:折腾小记
  1. 修改etc/bind/named.conf.options文件

代码语言:javascript
复制
+ tls dot-local-tls {
+         key-file "/etc/crt/dot.key";
+         cert-file "/etc/crt/dot.pem";
+ };
+ tls doh-local-tls {
+         key-file "/etc/crt/doh.key";
+         cert-file "/etc/crt/doh.pem";
+ };

options {
        directory "/var/cache/bind";

        // If there is a firewall between you and nameservers you want
        // to talk to, you may need to fix the firewall to allow multiple
        // ports to talk.  See http://www.kb.cert.org/vuls/id/800113

        // If your ISP provided one or more IP addresses for stable
        // nameservers, you probably want to use them as forwarders.
        // Uncomment the following block, and insert the addresses replacing
        // the all-0's placeholder.

-       forward only;
        forwarders { 127.0.0.1 port 54;
         };

        //========================================================================
        // If BIND logs error messages about the root key being expired,
        // you will need to update your keys.  See https://www.isc.org/bind-keys
        //========================================================================
        dnssec-validation yes;
        listen-on port 53 { any; };
        listen-on-v6 port 53 { any; };
        recursion yes;
-       allow-query { 0.0.0.0/0; };
+       allow-query { any; };
-       allow-recursion { any; };
+       listen-on port 853 tls dot-local-tls { any; };
+       listen-on port 443 tls doh-local-tls http default {any;};
};
  1. 配置/etc/apparmor.d/local/usr.sbin.named,允许证书访问

{% note modern info %}这里的路径是你自己证书放的地方{% endnote %}

代码语言:javascript
复制
/etc/crt/** r,
  1. 重新加载apparmor配置文件
代码语言:javascript
复制
apparmor_parser -r /etc/apparmor.d/usr.sbin.named

4.重启bind9的named服务

代码语言:javascript
复制
service.named restart

{% endtimenode %}

{% timenode 2021-07-28 %}

增加bind9和dnsproxy,加上AdGuardHome实现三者内部分流。

{% endtimenode %}

{% timenode 2021-07-13 %}

完成AdGuardHome的搭建与配置。

{% endtimenode %}

{% endtimeline %}

{% endfolding %}

0x00 前言

前几天在树莓派上ubuntu server,没错我又又又又又换系统了,生命不息,折腾不止嘛(就是手痒),按照流程我先后撞上了docker和AdGuardHome,并作为家庭路由器的DNS私服使用,感觉还可以,但还是有时候会加载比较慢,后来我看到了@冷莫大大的文章,发现国内的DNS还好,国外有可能会污染,所以按照他的文章来整了一下活。别说,还针不戳。

0x01 介绍

{% note modern info %}

冷莫大大的教程是基于Debian 10来操作的,我树莓派是Ubuntu server 20.04LTS,其实别的系统都差不多,只是包管理器和需要用到的文件的路径有些许不一样。然后准好双爪和一颗敢于折腾的心就好。(冰阔落、卫龙、瓜子、薯片:那我们走?)

{% endnote %}

本项目是基于bind9和dnsproxy来进行防污染DNS,缓存根服务器。内部DOH,内部分流,内部缓存,并且实现分流加速的功能。

0x02 干!

1、准备工作

{% note modern warning %}

本流程是根据我自己的调整来改写了一部分内容,大家可以酌情对比

{% endnote %}

{% folding yellow, 首先准备好基础环境(如果没有权限请在最前面加上sudo) %}

代码语言:javascript
复制
apt-get -y install wget nano dnsutils bind9-host apt-transport-https lsb-release ca-certificates curl
wget -O /etc/apt/trusted.gpg.d/bind-dev.gpg https://packages.sury.org/bind-dev/apt.gpg
sh -c 'echo "deb https://packages.sury.org/bind-dev/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/bind-dev.list
apt-get update

{% endfolding %}

2、bind9的安装与使用

以bind9 root身份安装 (端口:53)项目地址:https://www.isc.org/bind/

一、添加源

我们进入官网,找到BIND 9项目,点击Learn more进入详情:

isc-org
isc-org

上面的介绍大家有兴趣的可以自行查阅,然后我们向下翻,找到Installation框框,里面有几种linux发行版的下载方式,还有docker,我选择Unubtu:

isc-ins-ubu
isc-ins-ubu

进来之后找到Personal package archives这一栏,没错,这就是ubuntu大名鼎鼎的ppa源,把它整合到系统的软件源之后,更新列表就能找到这个软件有关的包了,这里我选的是开发版(BIND 9 Dev):

bind-ppa
bind-ppa

我们会看到加入ppa源有两种方式:

bind-addppa
bind-addppa

这里大家就可以凭喜好了,效果是一样的,都是把bind9-dev有关的软件加入软件仓库。

二、安装

好啦,这些步骤完成后就可以刷新我们的软件仓库并下载bind9啦:

代码语言:javascript
复制
sudo apt update
sudo apt install bind9
三、配置

设置bind9的自身服务监听端口和端口转发:

代码语言:javascript
复制
options {
        directory "/var/cache/bind";

        // If there is a firewall between you and nameservers you want
        // to talk to, you may need to fix the firewall to allow multiple
        // ports to talk.  See http://www.kb.cert.org/vuls/id/800113

        // If your ISP provided one or more IP addresses for stable
        // nameservers, you probably want to use them as forwarders.
        // Uncomment the following block, and insert the addresses replacing
        // the all-0's placeholder.

        forwarders { 127.0.0.1 port 54;
         };

        //========================================================================
        // If BIND logs error messages about the root key being expired,
        // you will need to update your keys.  See https://www.isc.org/bind-keys
        //=======================================================================
        dnssec-validation yes;
        listen-on port 53 { any; };
        listen-on-v6 port 53 { any; };
        recursion yes;
        allow-query { any; };
};

设置bind9正解和反解zone的值(我不懂,别问我:x):

代码语言:javascript
复制
// prime the server with knowledge of the root servers
zone "." {
        type hint;
        file "/usr/share/dns/root.hints";
};

// be authoritative for the localhost forward and reverse zones, and for
// broadcast zones as per RFC 1912

zone "localhost" {
        type master;
        file "/etc/bind/db.local";
};

zone "127.in-addr.arpa" {
        type master;
        file "/etc/bind/db.127";
};

zone "0.in-addr.arpa" {
        type master;
        file "/etc/bind/db.0";
};

zone "255.in-addr.arpa" {
        type master;
        file "/etc/bind/db.255";
};

zone "in-addr.arpa" {
        type master;
        file "/etc/bind/in-addr.arpa";
};

zone "root.zone" {
        type master;
        file "/etc/bind/root.zone";
};
3、dnsproxy的安装与使用

以dnsproxy root身份安装(端口:54 cpu型号arm64)项目地址 https://github.com/adguardTeam/dnsproxy

代码语言:javascript
复制
wget https://github.com/AdguardTeam/dnsproxy/releases/download/v0.39.0/dnsproxy-linux-arm64-v0.39.0.tar.gz
tar zxf dnsproxy-linux-arm64-v0.39.0.tar.gz
cd linux-arm64
mv linux-arm64 dnsproxy # 重命名文件夹
sudo chmod +x dnsproxy # 给予可执行权限

设置dnsproxy服务开机启动,并读取domain_full.txt的分流规则,编辑文件 /etc/ec.local

{% note modern info %}命令中的可执行文件dnsproxy和分流规则文件domain_full.txt的路径是我自己的,均可以自定义{% endnote %}

代码语言:javascript
复制
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/home/ubuntu/dnsproxy/dnsproxy -l 0.0.0.0 -p 54 --upstream /home/ubuntu/dnsproxy/domain_full.txt --all-servers &
exit 0
4、DNS分流文件的安装和使用

编写一个更新规则的脚本autoupdate.sh

代码语言:javascript
复制
#!/bin/bash
echo -e "in-addr.arpa"
rm /etc/bind/in-addr.arpa
curl -o /etc/bind/in-addr.arpa http://file.trli.club/dns/in-addr.arpa
echo -e "root.zone"
rm /etc/bind/root.zone
curl -o /etc/bind/root.zone http://file.trli.club/dns/root.zone
echo -e "update hosts"
rm /home/ubuntu/dnsproxy/domain_full.txt
curl -o /home/ubuntu/dnsproxy/domain_full.txt http://file.trli.club/dns/dns-txt/gfwlist2adguardhome/blacklist_domain_full.txt
exit 0;

基于该文件可执行权限

代码语言:javascript
复制
chmod +x autoupdate.sh

运行它,更新一下规则

代码语言:javascript
复制
sudo bash autoupdate.sh

下载完分流规则文件后即可启动服务了:

{% note modern warning %}注意这里有坑(都是我的血泪史我们之前安装的bind9的服务名不叫bind或是bind9,而是named){% endnote %}

代码语言:javascript
复制
chmod +x /etc/rc.local
sudo systemctl start rc-local.service
sudo systemctl enable named.service
sudo systemctl start named.service

重启系统,查看服务是否都启动了,没有的话手动运行开机自启

代码语言:javascript
复制
sudo systemctl status rc-local.service
sudo systemctl status named.service

0x03 测试

最后我们来使用bind9和dnsproxy分别来测试域名查询的速度,第一次可能有点慢,但是后来就会很快乐:

{% tabs 域名查询 %}

<!-- tab bind9 -->

代码语言:javascript
复制
dig bing.com @127.0.0.1 -p 53

<!-- endtab -->

<!-- tab dnsproxy -->

代码语言:javascript
复制
dig bing.com @127.0.0.1 -p 54

<!-- endtab -->

{% endtabs %} {% folding yellow, 另一种思路 %} 本文的思路是让dnsproxy作为bind9的上游服务,所以在bind9的配置文件中有forwards dnsproxy的端口这一个字段,那么如果我们加上了大名鼎鼎的AdGuardHome并作为主DNS服务器呢,那么我们可以让AdGuardHome监听53端口,然后让bind9作为AdGuardHome的上游服务器即可,就能很好的把这三个服务有机的串联在了一起。 {% endfolding %}

感谢?冷莫大大的文章和思路,没有他的文章也就不会有我的这片微调和补充。当然大佬们有更好的项目欢迎滴滴我哦?

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 前言
  • 0x01 介绍
  • 0x02 干!
    • 1、准备工作
      • 2、bind9的安装与使用
        • 一、添加源
        • 二、安装
        • 三、配置
      • 3、dnsproxy的安装与使用
        • 4、DNS分流文件的安装和使用
        • 0x03 测试
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档