搭建并实现智能DNS

发布时间 2023-10-07 14:55:38作者: 小糊涂90

1)环境需要五台主机:

DNS主服务器和web服务器1:10.0.0.150/24,192.168.33.150/24

web服务器2:10.0.0.151/24

web服务器3:192.168.33.151/24

DNS客户端1:10.0.0.152/24 ,dns指向10.0.0.150

DNS客户端2:192.168.33.152/24 ,dns指向10.0.0.150

2)dns服务器网卡配置:

#配置两个IP地址
#eth0:10.0.0.150/24
#eth1: 192.168.33.150/24
[root@centos8 ~]#ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.150 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::20c:29ff:fee2:3cae prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:e2:3c:ae txqueuelen 1000 (Ethernet)
      RX packets 8546 bytes 6754051 (6.4 MiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 4690 bytes 470482 (459.4 KiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@centos8 ~]#ifconfig eth1
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255
      inet6 fe80::20c:29ff:fee2:3cb8 prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:e2:3c:b8 txqueuelen 1000 (Ethernet)
      RX packets 543 bytes 50318 (49.1 KiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 79 bytes 8406 (8.2 KiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3)主DNS服务器端配置文件实现view

[root@centos8 ~]#yum install -y bind

[root@centos8 ~]#vim /etc/named.conf
acl beijingnet {
 10.0.0.0/24;
};
acl shanghainet {
 192.168.33.0/24;
};
acl othernet {
  any;
};
#注释掉下面两行
// listen-on port 53 { 127.0.0.1; };
// allow-query     { localhost; };
#其他略
view beijingview {
      match-clients { beijingnet;};
      include "/etc/named.rfc1912.zones.bj";
};
view shanghaiview {
      match-clients { shanghainet;};
      include "/etc/named.rfc1912.zones.sh";
};
view otherview {
      match-clients { othernet;};
      include "/etc/named.rfc1912.zones.other";
};
include "/etc/named.root.key";

4)实现区域配置文件

[root@centos8 ~]#cp -p /etc/named.rfc1912.zones  /etc/named.rfc1912.zones.bj
[root@centos8 ~]#cp -p /etc/named.rfc1912.zones /etc/named.rfc1912.zones.sh
[root@centos8 ~]#cp -p /etc/named.rfc1912.zones /etc/named.rfc1912.zones.other
[root@centos8 ~]#vim /etc/named.rfc1912.zones.bj
zone "." IN {
      type hint;
      file "named.ca";
};


zone "tan.org" {
      type master;
      file "tan.org.zone.bj";
};

[root@centos8 ~]#vim /etc/named.rfc1912.zones.sh
zone "." IN {
      type hint;
      file "named.ca";
};


zone "tan.org" {
      type master;
      file "tan.org.zone.sh";
};
[root@centos8 ~]#vim /etc/named.rfc1912.zones.other
zone "." IN {
      type hint;
      file "named.ca";
};


zone "tan.org" {
      type master;
      file "tan.org.zone.other";
};

5)创建区域数据库文件


[root@centos8 ~]#cat /var/named/tan.org.zone.bj
$TTL 1D
@       IN SOA master admin.tan.org. (
                                       3       ; serial
                                      1D     ; refresh
                                      1H     ; retry
                                      1W     ; expire
                                      3H )   ; minimum
      NS     master
master A       10.0.0.150
websrv A       10.0.0.151
www     CNAME   websrv
[root@centos8 ~]#cat /var/named/tan.org.zone.sh
$TTL 1D
@       IN SOA master admin.tan.org. (
                                       3       ; serial
                                      1D     ; refresh
                                      1H     ; retry
                                      1W     ; expire
                                      3H )   ; minimum
      NS     master
master A       10.0.0.150
websrv A       192.168.33.151
www     CNAME   websrv
[root@centos8 ~]#cat /var/named/tan.org.zone.other
$TTL 1D
@       IN SOA master admin.tan.org. (
                                       3       ; serial
                                      1D     ; refresh
                                      1H     ; retry
                                      1W     ; expire
                                      3H )   ; minimum
      NS     master
master A       10.0.0.150
websrv A       127.0.0.1
www     CNAME   websrv

6)检查配置文件

[root@centos8 ~]#named-checkconf

7)重启服务


[root@centos8 ~]#systemctl restart named
[root@centos8 ~]#rndc reload
server reload successful

8)配置三个web网站

#分别在三台主机上安装http服务
#在web服务器1:10.0.0.150/24实现
yum install httpd                        
echo www.tan.org in Other > /var/www/html/index.html
systemctl start httpd  
#在web服务器2:10.0.0.151/24
echo www.tan.org in Beijing > /var/www/html/index.html
systemctl start httpd  
#在web服务器3:192.168.33.151/24
yum install httpd                        
echo www.tan.org in Shanghai > /var/www/html/index.html
systemctl start httpd  

9)客户端测试

[root@localhost ~]# hostname -I
10.0.0.152
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.0.0.150
[root@localhost ~]# curl www.tan.org
www.tan.org in beijing

[root@centos8 ~]#hostname -I
192.168.33.152
[root@centos8 ~]#cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.33.150
[root@centos8 ~]#curl www.tan.org
www.tan.org in shanghai