Centos Bind9 DNS CentOS 自建内网DNS

内网自建DNS服务,使用bind9。

准备和安装

  • 安装 Bind9及工具yum install -y bind bind-utils net-tools

  • 设置主机名hostnamectl set-hostname tty1-13.panda.com

配置

修改/etc/named.conf配置

options {
	// IPV4监听地址和端口
	listen-on port 53 { 10.1.1.13; };
	//listen-on-v6 port 53 { ::1; };
	directory 	"/var/named";
	dump-file 	"/var/named/data/cache_dump.db";
	statistics-file "/var/named/data/named_stats.txt";
	memstatistics-file "/var/named/data/named_mem_stats.txt";
	recursing-file  "/var/named/data/named.recursing";
	secroots-file   "/var/named/data/named.secroots";
	
	// 允许访问的机器清单,any为所有。
	allow-query     { any; };
	// 上级dns,此为虚机的网关地址。
	forwarders	{ 10.1.1.254; };

	/* 
	 - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
	 - If you are building a RECURSIVE (caching) DNS server, you need to enable 
	   recursion. 
	 - If your recursive DNS server has a public IP address, you MUST enable access 
	   control to limit queries to your legitimate users. Failing to do so will
	   cause your server to become part of large scale DNS amplification 
	   attacks. Implementing BCP38 within your network would greatly
	   reduce such attack surface 
	*/
	
	// recursion 是否允许递规查询(recursion)如果设置为”yes”,则允许服务器采用递归的方式进行查询,也就是当要查询的地址不在服务器的数据库列表中时,服务器将一级一级的查询,直到查到为止(一般对局域网都打开)。设置为”no”,并不意味着服务器对于请求的递归查询不给予回答,而是对于请求的递归查询,不再向上级服务器请求,也不缓存,如果不对请求的递归查询回答,可以清空缓存,然后设置为“NO”。
	recursion yes;

	// dns安全相关,关闭节省小霸王的资源。
	dnssec-enable no;
	dnssec-validation no;

	/* Path to ISC DLV key */
	bindkeys-file "/etc/named.root.key";

	managed-keys-directory "/var/named/dynamic";

	pid-file "/run/named/named.pid";
	session-keyfile "/run/named/session.key";
};

区域配置文件

修改/etc/named.rfc1912.zones,添加内网机器得设置的主机域。

// panda.com 主机域的域名
zone "panda.com" IN {
	type master;
	// 配置区域数据文件的名称
	file "panda.com.zone";
	// 运行更新的IP或者网段
	allow-update { 10.1.1.13; };
};

配置区域数据文件

编辑在区域配置文件中的file中定义的文件。

cp -a /var/named/named.localhost /var/named/panda.com.zone
vim /var/named/panda.com.zone

配置详情。

; 分号问注释,
; 原始域名
$ORIGIN	panda.com.
$TTL 600 ; 10minutes
; 配置SOA
@	IN SOA	dns.panda.com. dnsadmin.panda.com. (
				2021080701	; serial	yyyymmddxx
				10800		; refresh
				900		; retry
				604800		; expire
				86400		; minimum
				)
	NS	dns.panda.com.

$TTL	60 ; 1 minutes
; 配置具体的解析。
dns	A	10.1.1.13
tty1-13	A	10.1.1.13
tty1-21	A	10.1.1.21
tty1-22	A	10.1.1.22
tty1-23	A	10.1.1.23

以上配置基本完成,还缺逆向解析。现在可以使用了。

  • 检查配置文件,没有输出就是配置正确。named-checkconf
  • 重启bind服务systemctl restart named
  • 检查服务是否启动netstat -luntp | grep 53

测试DNS解析

[root@tty1-13 ~]# named-checkconf
[root@tty1-13 ~]# systemctl restart named
[root@tty1-13 ~]# netstat -luntp | grep 53
tcp        0      0 10.1.1.13:53            0.0.0.0:*               LISTEN      2373/named          
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      2373/named          
tcp6       0      0 ::1:953                 :::*                    LISTEN      2373/named          
udp        0      0 10.1.1.13:53            0.0.0.0:*                           2373/named          
[root@tty1-13 ~]# dig -t A tty1-13.panda.com @10.1.1.13 +short
10.1.1.13
[root@tty1-13 ~]# dig -t A tty1-23.panda.com @10.1.1.13 +short
10.1.1.23
[root@tty1-13 ~]# 

以上信息说明配置成功了,接下配置客户端的DNS为本机。

vim /etc/sysconfig/network-scripts/ifcfg-ens33
#......
#IPADDR=10.1.1.13
#NETMASK=255.255.255.0
#GATEWAY=10.1.1.254
#DNS1=10.1.1.13
systemctl restart network
cat /etc/resolv.conf
## Generated by NetworkManager
#search panda.com
#nameserver 10.1.1.13
ping baidu.com
#PING baidu.com (220.181.38.251) 56(84) bytes of data.
#64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=5.31 ms
#64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=5.30 ms
#...