声明:本篇文章仅供学习研究,禁止用于非法途径,如不听劝告,产生的一切后果由使用者承担

dns的分类

  • 递归dns:国内大厂的公共dns就属于递归dns
  • 权威dns:负责域名解析的dns服务器就属于权威dns
  • 转发dns:本地电脑上的默认的dns就属于转发dns

关于dns的分类就简单讲下,如果感兴趣的可以自己百度去了解

dns解析过程

就用本站域名来举个例子“www.xlxbk.cn”
当我们在浏览器输入“www.xlxbk.cn”的时候

  • 首先在本地进行查找是否存在“www.xlxbk.cn”的记录,如果存在就返回结果否则进入下一步
  • 将请求发送给本地配置的dns服务器进行处理,如果存在就返回结果否则进入下一步
  • 将请求直接发送给根dns(只返回顶级域名的权威服务器地址)进行查询
  • 根dns返回cn的权威服务器地址
  • cn的权威服务器返回xlxbk.cn的权威服务器地址
  • xlxbk.cn的权威服务器返回www.xlxbk.cn的ip地址
  • 客户端拿到地址进行访问

大致就是这样的一个流程,可能有些地方不太对,仅供参考

递归dns搭建

搭建递归dns常用的软件:unboud、bind9、PowerDNS 等等
本文使用unbound来进行搭建(编译安装)
unbound官网
unbound文档
系统选择方面,主流操作系统都可以(本文演示debian和centos,其它系统基本一样,看命令就知道了)

  • 安装依赖
# debian
# 非root用户请在命令前面加上sudo
apt update
apt install -y build-essential
apt install -y libssl-dev
apt install -y libexpat1-dev
apt-get install -y bison
apt-get install -y flex
# centos
# 非root用户请在命令前面加上sudo
yum install gcc openssl-devel expat-devel

  • 下载源码并解压
cd ~ && wget https://nlnetlabs.nl/downloads/unbound/unbound-latest.tar.gz && tar xzf unbound-latest.tar.gz
#解压之后请使用cd命令进入解压后的文件夹里(ls 查看当前目录下的文件)
  • 编译安装
./configure --enable-subnet --with-libevent && make && make install
# 这里可能会出现libevent 这个找不到的报错,这个请自行百度解决(没机子来复现这个问题)
# 这条命令后面的两个参数--enable-subnet和--with-libevent的作用如下
# --enable-subnet:增加EDNS功能
# --with-libevent:提升性能的

到这步已经把unbound给安装好了(如果报错请自己百度解决,查看/usr/local/etc/unbound这个目录下是否存在unbound.conf,存在代表安装好了,没有代表有错误没安装好),下面就开始进行配置了

  • 启用 DNSSEC
unbound-anchor
# 这里可能会报错,这里自己百度解决
# 如果控制台有输出就代表有错误,成功了是没有任何输出的
# 运行成功后会在/usr/local/etc/unbound目录下出现一个文件“root.key”
  • named.conf
server:
    verbosity: 1
    interface: 0.0.0.0
    port: 53
    do-ip4: yes
    do-udp: yes
    do-tcp: yes
    do-ip6: yes
    access-control: 0.0.0.0/0 allow
    root-hints: "root.hints"
    send-client-subnet: 0.0.0.0/0
    harden-glue: yes
    harden-large-queries: yes
    harden-dnssec-stripped: yes
    edns-buffer-size: 1232
    rrset-roundrobin: yes
    #下面注释的两行是缓存,自行决定是否需要启用
    #cache-min-ttl: 300
    #cache-max-ttl: 86400
    do-not-query-localhost: yes
    serve-expired: yes
    harden-algo-downgrade: yes
    harden-short-bufsize: yes
    hide-identity: yes
    hide-version: yes
    neg-cache-size: 4M
    qname-minimisation: yes
    minimal-responses: yes
    prefetch: yes
    prefetch-key: yes
    num-threads: 2
    msg-cache-size: 50m
    rrset-cache-size: 100m
    so-reuseport: yes
    so-rcvbuf: 4m
    so-sndbuf: 4m
    unwanted-reply-threshold: 100000
    auto-trust-anchor-file: "root.key"
    harden-dnssec-stripped: yes
    harden-below-nxdomain: yes
    aggressive-nsec: yes
    val-permissive-mode: no
    log-queries: no
    log-replies: no
    log-servfail: no
    log-local-actions: no
    logfile: "unbound.log"
    outgoing-range: 8192
    num-queries-per-thread: 4096
module-config: "subnetcache validator iterator"
target-fetch-policy: "0 0 0 0 0"
# 上面的配置文件用了一段时间感觉还不错,自己把默认的unbound.conf里面的内容删除然后复制粘贴进去
  • 注意事项和启动unbound

这里需要自己在/usr/local/etc/unbound目录下新建一个文件“unbound.log”

touch unbound.log && chmod 666 unbound.log

在启动之前我们还需要在/usr/local/etc/unbound目录下下载一个文件

wget -O root.hints https://www.internic.net/domain/named.root

到这里就可以启动unbound了

# 如果这个没用可以试试下面那个
unbound-control start
#上面那个没用用这个试试(可能会报错)
unbound
# 上面那个报错用下面命令解决,执行完后再执行一遍上面那个命令就可以启动了
groupadd unbound
useradd -m -g unbound -s /bin/false unbound
# 附带一个结束unbound的命令
pkill -f unbound

如果启动后过会自己关闭进程了请把unbound目录设置为777权限(/usr/local/etc/unbound)
如果有什么疑问可以通过网站上的联系方式联系我或者邮件联系我(邮件联系请附上报错截图)