linux中dns服务器主从配置教程

作者:袖梨 2022-11-14

bind版本:bind-9.9.5

主:192.169.1.100
从:192.169.1.101

tar zxvf bind-9.9.5.tar.gz
cd bind-9.9.5
./configure --prefix=/usr/local/named --enable-epoll --enable-threads --enable-ipv6 --disable-openssl-version-check --enable-backtrace --enable-symtable
make && make install

开始配置bind,接下来的过程是让rndc来管理bind

创建 rndc.conf文件,用bind自带程序生成
进入/usr/local/named/etc,将rndc.conf及named.conf生成

cd /usr/local/named
/usr/local/named/sbin/rndc-confgen >/usr/local/named/etc/rndc.conf

把rndc.conf 中的key信息输出到 named.conf 中
cd etc/
tail -10 rndc.conf | head -9 | sed -e s/# //g > named.conf

##rndc.conf与named.conf的key值必须完全一样,而且并不需要生成rndc.key

vi named.conf 在后面添加
options {
directory "/usr/local/named/etc";
version "Guess ";
allow-transfer { 192.169.1.101; };
pid-file "/var/run/named.pid";
recursion no;
notify explicit;
also-notify {
192.169.1.101;
};
};

zone "." {
type hint;
file "named.ca";
};

zone "0.0.127.IN-ADDR.ARPA" {
type master;
file "localhost.rev";
};

// RFC 3152
zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA" {
type master;
file "localhost-v6.rev";
};

// RFC 1886 -- deprecated
zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.INT" {
type master;
file "localhost-v6.rev";
};

zone "wangzhe.me" {
type master;
file "wangzhe.me.zone";
};

将根服务器的信息导入到/usr/local/named/etc/named.ca文件中
dig -t NS .>/usr/local/named/var/named/named.ca

启动bind来测试是否安装成功了
/usr/local/named/sbin/named -gc /usr/local/named/etc/named.conf &

加-gc参数,可以显示出启动日志,以便排错

如果运行结果最后一行显示Running
表明安装并启动成功。

测试rndc命令
[root@ns1 etc]# /usr/local/named/sbin/rndc status
version: 9.9.5 (Guess )
CPUs found: 8
worker threads: 8
UDP listeners per interface: 8
number of zones: 13
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/0/1000
tcp clients: 0/100
server is up and running

把named 添加到启动项,随操作系统一起启动。
echo "/usr/local/named/sbin/named -c /usr/local/named/etc/named.conf&" >> /etc/rc.local

#############从dns配置
编译安装、生成rndc.conf及named.conf 和主库操作一样

vi named.conf 在后面添加
options {
version "Guess ";
allow-transfer { none; };
directory "/usr/local/named/etc";
dump-file "/usr/local/named/var/cache_dump.db";
statistics-file "/usr/local/named/var/named_stats.txt";
memstatistics-file "/usr/local/named/var/named_mem_stats.txt";
allow-query { any; };
recursion no;

dnssec-enable no;
dnssec-validation no;
};

logging {
channel default_debug {
file "named.run";
severity dynamic;
};
};

zone "." IN {
type hint;
file "named.ca";
};

zone "localhost.localdomain" IN {
type master;
file "named.localhost";
allow-update { none; };
};

zone "localhost" IN {
type master;
file "named.localhost";
allow-update { none; };
};

zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};

zone "1.0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};

zone "0.in-addr.arpa" IN {
type master;
file "named.empty";
allow-update { none; };
};

zone "wangzhe.me" {
type slave;
file "slaves/wangzhe.me.zone";
masters { 192.168.1.100; };
};

注意:
如果需要允许外部对此dns进行查询,还需要named.conf里面options添加一条

allow-query { any; };

具体样式是
options {
allow-query { any; };
directory "/usr/local/named/var/named";
};

启动bind来测试是否安装成功了

/usr/local/named/sbin/named -gc /usr/local/named/etc/named.conf &

加-gc参数,可以显示出启动日志,以便排错

如果运行结果最后一行显示Running
表明安装并启动成功。成功会自动同步数据

测试rndc命令
[root@ns1 etc]# /usr/local/named/sbin/rndc status
version: 9.9.5 (Guess )
CPUs found: 8
worker threads: 8
UDP listeners per interface: 8
number of zones: 13
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/0/1000
tcp clients: 0/100
server is up and running

把named 添加到启动项,随操作系统一起启动。
echo "/usr/local/named/sbin/named -c /usr/local/named/etc/named.conf&" >> /etc/rc.local

主意:
从域名服务器成功获取到主域名服务器的区域文件,两者的文件类型并不一致,如下:
主区域的文件类型:chinadaily.com.cn.zone: ASCII text
从区域文件的类型:chinadaily.com.cn.zone: data

bind9默认就是这么设计的,主的zone用text文件,从的zone用data文件(bind官方说法是raw格式),所以从的zone文件里直接查看都是乱码,
若想检查从的zone配置文件可以这样子来:
sbin/named-compilezone -s relative -f raw -F text -o - abc.com etc/cnc/abc.com.zone

最后一行是ok的话就说明这个zone文件没问题。
-f用来指明从的zone的格式(一共2种:text和raw,乱码的那个就是raw格式的)
-F用来指明转换后输出的格式
-o用来指明转换完了输出到哪个文件里,紧跟的-表示转换完了直接在标准输出显示
-s用来指定输出样式(2种:full---方便脚本处理的样式;relative---方便人手工处理或者阅读的样式)

还有官网说之所以这么设计是因为data比text处理起来效率要高。

相关文章

精彩推荐