教程:如何搭建属于自己的DNS

AKHYui2019-01-19 12:19:00工具搭建
前一段时间发现路由器里面可以自定义DNS,就想如果把纯净的DNS放入路由器里会不会直接访问谷歌和pixiv之类的。

教程开始

我们制作私人DNS有两种方法:

1.dnsmasq

2.overture

制作环境使用CentOS7


1.dnsmasq

首先我们先更新源

yum -y update

然后安装dnsmasq

yum install dnsmasq -y

dnsmasq的配置文件在/etc/dnsmasq.conf中,编辑这个文件

vim /etc/dnsmasq.conf

里面的东西很多,需要添加的就以下几种

#这个是你指定的hosts文件,dnsmasq会使用你指定的hosts(有的时候hosts比较好的可以直接连YouTube)

addn-hosts=/etc/hosts

#这个是你自定义的上游DNS(第一个是阿里云的DNS,第二个是教育网的DNS)

server=223.5.5.5

server=202.38.93.153

#listen-address这个配置不可以有,一定要删掉

#如果看到listen-address=127.0.0.1这个可以注释掉或者删掉

dnsmasq的基本指令

#开启

sudo service dnsmasq start

#关闭

sudo service dnsmasq stop

#重启

sudo service dnsmasq restart

开启以后查看53端口

netstat -tunlp | grep 53

如果显示结果为

tcp        0      0 0.0.0.0:53                  0.0.0.0:*                   LIST                    EN      2346/dnsmasq

udp        0      0 0.0.0.0:53                  0.0.0.0:*                                                   2346/dnsmasq

dnsmasq正在运行了

dnsmasq有个很大的缺点,虽然操作比较简单,但是dnsmasq是从上游DNS的53端口获取流量,而无法确定上游DNS的53端口的流量是否已经遭到了污染,如果你一套稳定而且很棒的hosts可以用dnsmasq。


2.overture

和dnsmasq不同的是,overture可以从上游DNS的非标准端口(非53端口)接收流量,不过需要上游DNS的支持。

overture不仅可以用在VPS中,也可以用在树莓派甚至Android

overture可以在GITHUB上找到点此进入open in new window

根据你的架构下载对应的版本

cd ~

wget [要下载文件的网址]

我下载的是overture-linux-amd64.zip

mkdir overture && mv overture-linux-amd64.zip overture

chmod 774 overture

解压zip文件

cd overture

unzip overture-linux-amd64.zip

chmod 774 ./*

会有这些文件

[root@hyui overture]# ls

config.json                domain_primary_sample  hosts_sample                   ip_network_primary_sample  overture-linux-amd64.zip

domain_alternative_sample             ip_network_alternative_sample  overture-linux-amd64

修改config.json

vim config.json

其中需要修改一些地方

"PrimaryDNS": [  //你的主DNS信息,使用了DNSPOD

    {

      "Name": "DNSPod",

      "Address": "119.29.29.29:53",

      "Protocol": "udp",

      "SOCKS5Address": "",

      "Timeout": 6,

      "EDNSClientSubnet": {

        "Policy": "auto",

        "ExternalIP": "",

        "NoCookie": true

      }

    }

  ],

  "AlternativeDNS": [   //你的备用DNS信息,这个是外国DNS,为了防止对53端口的投毒,可以使用443端口(前提是他们支持443或其他端口)

    {

      "Name": "Opendns",

      "Address": "208.67.220.220:443",

      "Protocol": "tcp",

      "SOCKS5Address": "",

      "Timeout": 6,

      "EDNSClientSubnet": {

        "Policy": "disable",

        "ExternalIP": "",

        "NoCookie": true

      }

    }

  ],

关于EDNSClientSubnet的说明,作者给出的是

EDNSClientSubnet: Used to improve DNS accuracy. Please check RFC7871 for details.

Policy

auto: If client IP is not in the reserved IP network, use client IP. Otherwise, use external IP.

manual: Use external IP if this field is not empty, otherwise use client IP if it is not reserved IP.

disable: Disable this feature.

ExternalIP: If this field is empty, ECS will be disabled when the inbound IP is not an external IP.

NoCookie: Disable cookie.

EDNSClientSubnet:用于提高DNS准确性。

auto:如果客户端IP不在保留的IP网络中,请使用客户端IP。否则,请使用外部IP。

manual:如果此字段不为空,则使用外部IP,否则使用客户端IP(如果不是保留IP)。

disable:禁用此功能。

ExternalIP:如果此字段为空,则当入站IP不是外部IP时,将禁用ECS。

NoCookie:禁用cookie。

overture同样可以自定义hosts

"HostsFile": "/root/overture/hosts"   //这一行中的hosts即为自定义的hosts需要自己添加

设置完成后就可以运行了

./overture-linux-amd64

会显示

INFO[0000] Overture v1.5-rc3                            

INFO[0000] If you need any help, please visit the project repository: https://github.com/shawn1m/overture 

INFO[0000] Load domain ./domain_primary_sample successful with 1 records  

INFO[0000] Load domain ./domain_alternative_sample successful with 1 records  

INFO[0000] Load ./ip_network_primary_sample successful  

INFO[0000] Load ./ip_network_alternative_sample successful 

INFO[0000] Minimum TTL is disabled                      

INFO[0000] Cache is disabled                            

INFO[0000] Load hosts file successful                   

INFO[0000] Start overture on :53 

说明正在运行了

保持overture在后台运行可以用

setsid ./overture-linux-amd64

当然我们也可以写一个脚本让它开机自启动

mkdir /etc/overture

vim /etc/overture/overture.sh



#!/bin/bash

cd /root/overture/

setsid ./overture-linux-amd64

创建好以后标记为可执行文件

chmod +x /etc/overture/overture.sh

将rc.local也标记为可执行文件

chmod +x /etc/rc.d/rc.local

打开rc.local

vim /etc/rc.d/rc.local

添加脚本进去

/etc/overture/overture.sh

就可以开机被执行了。


后记:后来一直都觉得哪里不太好,觉得是不是哪里可以改进之类的,于是有了这个补充。

/root/overture这个目录我们完全可以放在/usr/local/目录中

然后进入/usr/local/overture将overture-linux-amd64软链接到/usr/bin

这样就可以不用在cd进overture的目录了

# 代码实现

## 移动前可以先停止overture的运行

mv /root/overture /usr/local/

cd /usr/local/overture

## 软链接的实现 

ln -s /usr/local/overture/overture-linux-amd64 /usr/bin/overture

## 运行

setsid overture -c /usr/local/overture/config.json

这个时候就不需要那个开机脚本了 直接把运行命令添加到/etc/rc.d/rc.local就够了

Last Updated 9/17/2025, 7:13:55 AM