搭建基于bind9的DNS解析服务器和基于dnscontrol和rndc的dns管理平台

【搭建基于bind9的DNS解析服务器和基于dnscontrol和rndc的dns管理平台】仰天大笑出门去,我辈岂是蓬蒿人。这篇文章主要讲述搭建基于bind9的DNS解析服务器和基于dnscontrol和rndc的dns管理平台相关的知识,希望能为你提供帮助。
现有生产环境是centos6+bind9.8.2+namedmanager解决方案,该方案使用多年都十分稳定,性能优良,但由于centos平台无法再升级bind版本了,导致漏扫一直能扫出各种dns漏洞,必须寻找替代方案。 新架构:4台debian服务器安装最新版bind,官方版本到9.16.15了,而且持续更新,生产环境两两用keepalived起虚IP(本笔记只使用了192.168.118.250这个IP作为实验),管理端为192.168.118.25,管理端安装dnscontrol。管理端使用scp传输和bind提供的rndc工具远程控制4台服务器。 安装准备 1. https://www.debian.org/ 下载网络安装镜像(查了bind官方文档推荐使用debian发行版,可直接用包管理器安装最新版bind) 2. 安装4台服务器操作系统为debian10 IP地址分配 3. 根据主机基线对4台服务器进行主机安全加固 4. 安装bind9 1)添加isc官方源 2)sudo apt-get install bind9 就会安装最新版本的bind9,以后升级也十分方便,只需执行apt-get upgrade bind9即可。 5. 安装keepalived6. 更改 dns地址为自己 一、named(bind9) 配置

// Thi is the primary configuration file for the BIND DNS server named. // // Please read /usr/share/doc/bind9/README.Debian.gz for information on the // structure of BIND configuration files in Debian, *BEFORE* you customize // this configuration file. // // If you are just adding zones, please do that in /etc/bind/named.conf.local//include "/etc/bind/named.conf.options"; //include "/etc/bind/named.conf.local"; options { listen-on port 53 { any; }; listen-on-v6 { any; }; directory "/var/cache/bind"; dump-file "/var/cache/bind/xcache_dump.db"; statistics-file "/var/cache/bind/named_stats.txt"; memstatistics-file "/var/cache/bind/named_mem_stats.txt"; allow-query { any; }; allow-new-zones yes; allow-query-cache { any; }; forwarders { 202.103.224.68; 202.103.225.68; 119.29.29.29; 223.6.6.6; }; recursion yes; dnssec-enable no; dnssec-validation no; }; logging { channel query-errors_log { file "query-errors" versions 5 size 20m; print-time yes; print-category yes; print-severity yes; severity dynamic; }; channel default_debug { file "named.run" versions 5 size 20m; print-time yes; print-category yes; print-severity yes; severity dynamic; }; category queries { null; }; category query-errors {query-errors_log; }; category default { default_debug; }; }; include "/etc/bind/named.conf.default-zones";

配置开机启动 此时单台DNS服务器已安装完成,windows客户端可以用nslookup www.qq.com 192.168.118.250 检查解析是否正常。linux客户端使用dig @192.168.118.250 www.qq.com 二、dnscontrol安装 在DNS管理机上安装dnscontrol dnscontrol是使用go语言编写的,运行时需要go语言运行环境,所以要提前安装golang,这里推荐使用root权限用户手动安装,不要用管理器apt-get install golang-go命令安装,这种安装方式有弊端,因为go版本才到1.15,而dnscontrol必须使用1.16,故需要手动安装golang环境才能用)
golang手动安装过程 cd /opt/ wget https://studygolang.com/dl/golang/go1.16.6.linux-amd64.tar.gz tar zxvf go1.16.6.linux-amd64.tar.gzvi /etc/profile 注入环境变量 export GOPATH="/opt/go" export PATH="$PATH:$GOPATH/bin"source /etc/profile配置生效go version 查看安装版本 go version go1.16.6 linux/amd64

安装dnscontrol 如果按照文档直接使用GO111MODULE=on go get github.com/StackExchange/dnscontrol/v3 命令下载编译安装dnscontrol的话,你会发现非常卡慢,需要先执行代理加速命令
以下为我的正常安装过程 export GOPROXY="https://goproxy.io" GO111MODULE=on go get github.com/StackExchange/dnscontrol/v3go: downloading github.com/Azure/azure-sdk-for-go v0.2.0-beta go: downloading github.com/cenkalti/backoff v1.1.0 go: downloading github.com/go-acme/lego v1.2.1 省略了很多行 go: downloading github.com/form3tech-oss/jwt-go v1.0.2 go: downloading github.com/gofrs/uuid v1.2.0 go: downloading github.com/patrickmn/go-cache v1.0.0 go: downloading github.com/pierrec/lz4 v1.0.1

三、dnscontrol基本使用 这里有两个官方配置手册 入门手册 https://stackexchange.github.io/dnscontrol/getting-started 迁移手册 https://stackexchange.github.io/dnscontrol/migrating 我们先简单学习一下基本使用1)配置授信文件(dnscontrol支持各种各样的DNS服务商,支持同时推送,bind是其中之一)
vi creds.json指定生成文件为zone文件格式 { "bind": { "directory": "zones", "filenameformat": "%U.zone" } }

vi dnsconfig.js 这是主配置文件,所有自定义DNS记录都在里面添加、配置、修改、删除,可以结合svn或者git进行版本管理。
// Providers:var REG_NONE = NewRegistrar(\'none\', \'NONE\'); // No registrar. var DNS_BIND = NewDnsProvider(\'bind\', \'BIND\', { \'default_soa\': { \'master\': \'ns1.example.com.\', \'mbox\': \'ns1.example.com.\', \'refresh\': 3600, \'retry\': 600, \'expire\': 604800, \'minttl\': 1440, }, \'default_ns\': [ \'ns1.example.com.\', \'ns2.example.com.\' ] })// Domains:D(\'example.com\', REG_NONE, DnsProvider(DNS_BIND), A(\'ns1\', \'192.168.118.118\'), A(\'ns2\', \'192.168.117.117\'), A(\'test1\', \'192.168.118.15\'), A(\'www\', \'192.168.117.5\'), A(\'www2\', \'192.168.117.5\'), A(\'ntp\', \'192.168.118.5\') );

推送,可以看到目录多了个zone文件。
root@tmpl-debian10:~/dnscontrol# dnscontrol push ******************** Domain: example.com ----- Getting nameservers from: bind ----- DNS Provider: bind... File does not yet exist: "/opt/dnscontrol/zones/example.com.zone" 1 correction #1: GENERATE_ZONEFILE: \'example.com\' (new file with 9 records)WRITING ZONEFILE: /opt/dnscontrol/zones/example.com.zone SUCCESS! ----- Registrar: none... 0 corrections Done. 1 corrections.


好了,顺利生成bind的zone配置文件,下一步使用scp和rndc工具进行推送,推送之前要先配置好rndc组件以实现dns服务器允许控制端控制。 四、配置rndc通讯 需要先在DNS管理机上安装好组件生成密钥
rndc-confgen -r /dev/urandom会自动生成一段配置内容,注意添加修改dns服务器ip地址# Start of rndc.conf key "rndc-key" { algorithm hmac-md5; secret "Jb8UIcC9WJAWeRQaKZ3E+Q=="; }; options { default-key "rndc-key"; default-server DNS服务器IP地址; default-port 953; }; # End of rndc.conf将以上这段复制到控制机 /etc/rndc.conf,以下内容追加复制到DNS服务器named.conf,注意放通防火墙rndc TCP953端口,并加入管理机ip地址,重启服务# Use with the following in named.conf, adjusting the allow list as needed: key "rndc-key" { algorithm hmac-md5; secret "Jb8UIcC9WJAWeRQaKZ3E+Q=="; }; controls { inet 0.0.0.0 port 953 allow { 管理机ip; } keys { "rndc-key"; }; }; # End of named.conf

配置完成后在控制端进行联通性测试
ncvtadm@ops:~$ rndc -s 192.168.118.250 status version: BIND 9.16.15-Debian (Stable Release) < id:4469e3e> running on tmpl-debian10: Linux x86_64 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) boot time: Sun, 22 Aug 2021 11:28:34 GMT last configured: Sun, 22 Aug 2021 11:28:34 GMT configuration file: /etc/bind/named.conf CPUs found: 16 worker threads: 16 UDP listeners per interface: 16 number of zones: 102 (97 automatic) debug level: 0 xfers running: 0 xfers deferred: 0 soa queries in progress: 0 query logging is ON recursive clients: 0/900/1000 tcp clients: 0/150 TCP high-water: 0 server is up and running ncvtadm@ops:~$


五、推送配置 下面以在线添加第三步生成的一个测试域名为例example.com 配置推送机制,将该文件传送到各台DNS服务器的/var/cache/bind/目录上,管理机上执行
配置免密登录,便于使用scp命令传输文件,可写成一个脚本 cd /opt/dnscontrolssh-keygen ssh-copy-id root@dns服务器ip -p ssh的端口scp -P 22 zones/*.zone root@192.168.118.250:/var/cache/bind/

第一次传输是新增域名,故管理机上执行
rndc -s 192.168.118.250 addzone example.com \'{type master; file "example.com.zone"; }; \' rndc -s 192.168.118.250 reload example.com

后续都是修改解析记录场景,只需传输新zone文件后在管理机上执行
rndc -s 192.168.118.250 reload example.com

域名停止解析或者想删除,只需在管理机上执行
rndc -s 192.168.118.250 delzone example.com










    推荐阅读