自己搭建内网DNS服务器,帮助内网网络安全。除Windows部署外,我们还可以用Ubuntu搭建一套DNS服务器。国内很多linux同学习惯用centos,不过centos马上不再更新了,建议还是用Ubuntu。
Ubuntu 部署DNS服务器可以用bind9 服务,我们的Ubuntu 版本是18.04。
先安装bind9服务:
- sudo apt-get install bind9
-
安装过程中,还会安装和更新依赖包:
The following additional packages will be installed:
bind9-host bind9utils dnsutils libbind9-160 libdns1100 libirs160 libisc169 libisccc160 libisccfg160 liblwres160
python3-ply
Suggested packages:
bind9-doc resolvconf rblcheck python-ply-doc
The following NEW packages will be installed:
bind9 bind9utils python3-ply
The following packages will be upgraded:
bind9-host dnsutils libbind9-160 libdns1100 libirs160 libisc169 libisccc160 libisccfg160 liblwres160
内网搭建DNS服务器,大多数是解析纯内网地址使用。但是偶尔也需要解析外网的地址,所以我们可以配置DNS没有添加A记录的URL时,forward到外网DNS服务器或者内网的其他DNS服务器解析。
打开配置文件:
- sudo vi /etc/bind/named.conf.options
-
编辑forward,指定转发服务器public DNS:114.114.114.114:
- forwarders {
- 114.114.114.114;
- };
-
接下来,我们配置DNS服务器在IPv4工作:
- sudo vi /etc/default/bind9
-
将options=“-u bind ”修改为:将options=“-u bind -4 ”
- #
- # run resolvconf?
- RESOLVCONF=no
-
- # startup options for the server
- OPTIONS="-u bind -4"
-
保存退出。
更改配置后,重启bind9 服务:
- sudo service bind9 restart
-
- mirror@DESKTOP-6EEG99D:~$ sudo service bind9 restart
- * Stopping domain name service... bind9 rndc: connect failed: 127.0.0.1#953: connection refused
- [ OK ]
- * Starting domain name service... bind9 [ OK ]
- mirror@DESKTOP-6EEG99D:~$
-
如果要指定可以递归查询的DNS服务器,需要添加ACL:
- ACL "trusted"{
- 114.114.114.114;
- 8.8.8.8;
- 192.168.0.0/16
- };
-
- options {
- directory "/var/cache/bind";
- recursion yes;
- allow-recursion{trusted;}
- listen-on{192.168.31.170;}
- allow-transfer {none;}
- forwarders {
- 114.114.114.114
- 8.8.8.8
- }
- }
-
上述配置指定只有我们自己的服务器才能在DNS服务器中查询外部域。
保存退出。
- sudo vi /etc/bind/named.conf.local
-
这个文件内,除注释文件外,为空。添加如下正向区域文件。
domain.com为内网的域名,内网子网wei192.168.0.0/16
- zone "domain.com" {
- type master;
- file "/etc/bind/zones/db.domain.com";
- };
-
- zone "168.192.in-addr.arpa"{
- type master;
- file "/etc/bind/zones/db.168.192";
- };
-
创建zones区域文件:
- sudo mkdir /etc/bind/zones
-
将db.local文件复制到zones目录下:
- sudo cp /etc/bind/db.local /etc/bind/zones/db.domain.com
-
编辑区域文件:
- sudo vi /etc/bind/zones/db.domain.com
-
需要编辑SOA记录,将localhost和root.localhost更改为我们的domain:
- @ IN SOA localhost. root.localhost.
-
- $TTL 604800
- @ IN SOA domain.com. domain.com. (
- 3 ; Serial
-
添加NS和A记录:
- @ IN NS localhost. ;delete
- IN NS dns.domain.com
- IN NS mail.domain.com
-
- @ IN A 127.0.0.1 ;Delete
- dns.comain.com IN A 192.168.1.1
- mail.domain.com IN A 192.168.10.1
-
- @ IN AAAA ::1 ;delete
-
保存退出。至此正向区域就完成了。
反向区域方法与正向相同,只是A records更改为PTR records即可。
反向区域文件是我们为反向DNS查找定义DNS PTR记录的地方。 也就是说,当DNS通过IP地址接收查询时,它将查看反向区域文件以解析相应的FQDN。
- sudo cp /etc/bind/db.127 /etc/bind/zones/db.168.192
- sudo vi /etc/bind/zones/db.168.192
-
创建PTR:
- $TTL 604800
- @ IN SOA domain.com. domain.com. (
- 3 ; Serial
- 604800 ; Refresh
- 86400 ; Retry
- 2419200 ; Expire
- 604800 ) ; Negative Cache TTL
- ;
- ;@ IN NS localhost.
-
- ;1.0.0 IN PTR localhost.
- 192.168.1.1 IN PTR dns.domain.com
- 192.168.10.1 IN PTR mail.domain.com
-
完成后保存退出,并重启bind9服务。
- sudo service bind9 restart
-
最后验证测试:
验证正向区域
- sudo named-checkzone domain.com db.domain.com
-
验证反向区域
- sudo named-checkzone 168.192.in-addr.arpa /etc/bind/zones/db.168.192
-
过程中还需要注意firewall是否有允许bind9同行。
- sudo ufw allow Bind9
-
以上就是Ubuntu 18.04 bind9 DNS服务器的搭建过程。Linux搭建DNS服务器比windows要麻烦一些,没有特别要求的话,有条件用windows server会方便很多。