私有云搭建DNS服务器的方法
1. 服务器准备
172.16.23.137 DNS服务器, 以下简称 server
172.16.23.139 客户端测试服务器, 以下简称 client
2. 首关闭防火墙和selinux (两个服务器都操作)
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
以下是server服务器的操作
1. 查看ip
[root@localhost ~]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.16.23.137 netmask 255.255.255.0 broadcast 172.16.23.255
inet6 fe80::2a6e:d4ff:fe88:c794 prefixlen 64 scopeid 0x20
ether 28:6e:d4:88:c7:94 txqueuelen 1000 (Ethernet)
RX packets 7195317 bytes 2342937901 (2.1 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 199376 bytes 25216420 (24.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2、DNS服务端安装bind包,并设置开机自启
[root@DNSserver ~]# yum install -y bind
[root@DNSserver ~]# systemctl start named
[root@DNSserver ~]# systemctl enable named
Created symlink /etc/systemd/system/multi-user.target.wants/named.service → /usr/lib/systemd/system/named.service.
[root@localhost ~]# systemctl status named
● named.service - Berkeley Internet Name Domain (DNS)
Loaded: loaded (/usr/lib/systemd/system/named.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-12-01 14:06:49 CST; 19h ago
Process: 233243 ExecStop=/bin/sh -c /usr/sbin/rndc stop > /dev/null 2>&1 || /bin/kill -TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 233260 ExecStart=/usr/sbin/named -u named -c ${NAMEDCONF} $OPTIONS (code=exited, status=0/SUCCESS)
Process: 233255 ExecStartPre=/bin/bash -c if [ ! "$DISABLE_ZONE_CHECKING" == "yes" ]; then /usr/sbin/named-checkconf -z "$NAMEDCONF"; else echo "Checking of zone files is disabled"; fi (code=exited, status=0/SUCCESS)
Main PID: 233261 (named)
Tasks: 11
CGroup: /system.slice/named.service
└─233261 /usr/sbin/named -u named -c /etc/named.conf
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './DNSKEY/IN': 2001:500...#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './NS/IN': 2001:500:2d::d#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './DNSKEY/IN': 2001:500...#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './NS/IN': 2001:500:2::c#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './DNSKEY/IN': 2001:500...#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './NS/IN': 2001:500:9f::42#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './DNSKEY/IN': 2001:dc3...#53
Dec 02 09:09:59 localhost.localdomain named[233261]: network unreachable resolving './NS/IN': 2001:dc3::35#53
Dec 02 09:10:09 localhost.localdomain named[233261]: resolver priming query complete
Dec 02 09:10:09 localhost.localdomain named[233261]: managed-keys-zone: Unable to fetch DNSKEY set '.': ti...out
3、在/etc/named.conf文件里面修改全局配置信息
[root@DNSserver ~]# vim /etc/named.conf
..........
options {
listen-on port 53 { any; }; //中括号里面讲IP地址换成any
listen-on-v6 port 53 { any; }; //同上
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";
secroots-file "/var/named/data/named.secroots";
recursing-file "/var/named/data/named.recursing";
allow-query { any; }; //同上
..........
4、在/etc/named.rfc1912.zones子配置文件里面添加正向和反向的解析区域
[root@localhost ~]# arpaname 172.16.23.137
137.23.16.172.IN-ADDR.ARPA
## 在文件的最后添加正向和反向的解析文件
[root@DNSserver ~]# vim /etc/named.rfc1912.zones
zone "moyu.com" IN {
type master;
file "moyu.zone";
};
zone "23.16.172.in-addr.arpa" IN {
type master;
file "172.16.23.arpa";
};
正向:
zone:代表一个区域
" " : 双引号(英文)里面写入的是自己域名
type master : master 代表是主域名服务器
file " ":双引号里面写文件与在 /var/named下创建的文件名相同
反向:
" ":双引号里面写入的是反ip,例如:0.0.10.id-addr.arpa说明反向可以解析10.0.0.X网段的所有IP地址域名解析
可以使用:arpaname IP 查看自己的服务器IP的反IP
注释
5、复制生成正向和反向区域解析数据库文件
[root@DNSserver ~]# cd /var/named/
[root@DNSserver named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@DNSserver named]# cp -a named.localhost moyu.zone
[root@DNSserver named]# cp -a named.loopback 172.16.23.arpa
6、编辑正向区域解析数据库文件
[root@DNSserver named]# vim moyu.zone
$TTL 1D
@ IN SOA moyu.com. root.moyu.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1.moyu.com.
A 127.0.0.1
AAAA ::1
ns1 A 172.16.23.137
www A 172.16.23.139
cloud A 172.16.23.139
7、编辑反向区域解析数据库文件
[root@localhost named]# vim 172.16.23.arpa
$TTL 1D
@ IN SOA moyu.com. root.moyu.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1.moyu.com.
A 127.0.0.1
AAAA ::1
PTR localhost.
ns1 A 172.16.23.137
139 PTR www.moyu.com
139 PTR cloud.moyu.com
8、配置文件检测
[root@DNSserver ~]# named-checkconf /etc/named.conf
[root@DNSserver ~]# named-checkconf /etc/named.rfc1912.zones
# 回车后,没有报错信息说明没有错误
9、正向和反向区域解析测试
[root@localhost named]# named-checkzone moyu.com /var/named/moyu.zone
zone moyu.com/IN: loaded serial 0
OK
[root@localhost named]# named-checkzone moyu.com /var/named/172.16.23.arpa
zone moyu.com/IN: loaded serial 0
OK
10、重启DNS服务,查看端口
[root@localhost named]# netstat -tunlp | grep named
tcp 0 0 172.16.23.137:53 0.0.0.0:* LISTEN 233261/named
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 233261/named
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 233261/named
tcp6 0 0 :::53 :::* LISTEN 233261/named
tcp6 0 0 ::1:953 :::* LISTEN 233261/named
udp 0 0 192.168.122.1:53 0.0.0.0:* 233261/named
udp 0 0 172.16.23.137:53 0.0.0.0:* 233261/named
udp 0 0 127.0.0.1:53 0.0.0.0:* 233261/named
udp6 0 0 :::53 :::* 233261/named
到这里DNS服务端搭建完成
以下是client 服务器的操作
1、查看客户端主机的IP(Centos7)
[root@localhost named]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.16.23.139 netmask 255.255.255.0 broadcast 172.16.23.255
2、测试是否能与服务器端互通
[root@localhost ~]# ping -c 3 172.16.23.137
PING 172.16.23.137 (172.16.23.137) 56(84) bytes of data.
64 bytes from 172.16.23.137: icmp_seq=1 ttl=64 time=0.505 ms
64 bytes from 172.16.23.137: icmp_seq=2 ttl=64 time=0.282 ms
64 bytes from 172.16.23.137: icmp_seq=3 ttl=64 time=0.366 ms
--- 172.16.23.137 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.282/0.384/0.505/0.093 ms
3、安装bind-utils包
[root@localhost ~]# rpm -ql nslookup
package nslookup is not installed
[root@dnsclient ~]# yum install -y bind-utils
[root@localhost ~]# rpm -qa | grep bind-utils
bind-utils-9.11.4-26.P2.el7_9.2.x86_64
4、在客户端 /etc/resolv.conf 加入服务端的DNS
[root@dnsclient ~]# vim /etc/resolv.conf
[root@dnsclient ~]# cat /etc/resolv.conf
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 172.16.23.137
nameserver 8.8.8.8
5、测试
[root@localhost ~]# nslookup www.moyu.com
Server: 172.16.23.137
Address: 172.16.23.137#53
Name: www.moyu.com
Address: 172.16.23.139
[root@localhost ~]# nslookup 172.16.23.139
139.23.16.172.in-addr.arpa name = cloud.moyu.com.23.16.172.in-addr.arpa.
139.23.16.172.in-addr.arpa name = www.moyu.com.23.16.172.in-addr.arpa.
完成!