# 修改主机名[root@localhost ~]# hostnamectl set-hostname dns1[root@localhost ~]# bash# 配置本地yum源[root@dns1 ~]# mv /etc/yum.repos.d/ /media/[root@dns1 ~]# vi /etc/yum.repos.d/local.repo[centos]name=centosbaseurl=file:///opt/centosgpgcheck=0enabled=1[root@dns1 ~]# mkdir -p /opt/centos[root@dns1 ~]# mount /dev/sr0 /opt/centos/mount: /dev/sr0 is write-protected, mounting read-only[root@dns1 ~]# yum repolistLoaded plugins: fastestmirrorcentos | 3.6 kB 00:00:00(1/2): centos/group_gz | 155 kB 00:00:00(2/2): centos/primary_db | 2.8 MB 00:00:00Determining fastest mirrorsrepo id repo name statuscentos centos 3,723repolist: 3,723# 关闭防火墙[root@dns1 ~]# systemctl stop firewalld[root@dns1 ~]# systemctl disable firewalldRemoved symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.[root@dns1 ~]# setenforce 0[root@dns1 ~]# vi /etc/selinux/configSELINUX=disabled# 安装常用工具[root@dns1 ~]# yum install -y vim net-tools bash-c
服务搭建# 安装服务[root@dns1 ~]# yum -y install bind# 配置注意事项 所有内容以;结尾 ,, 大括号两侧内用空格分隔# 配置主配置文件[root@dns1 named]# vim /etc/named.confoptions {listen-on port 53 { any; }; //设置服务器监听网卡,any 所有的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";allow-query { any; }; //设置访问服务器的客户端地址, any 所有include "/etc/named.rfc1912.zones"; // 最下面有一行配置文件,指定了区域配置文件# 配置区域配置文件[root@dns1 ~]# vim /etc/named.rfc1912.zoneszone "bilibili.com" IN {type master;file "bilibili.localhost";allow-update { none; };};zone "59.30.10.in-addr.arpa" IN {type master;file "bilibili.loopback";allow-update { none; };};[root@dns1 ~]# cd /var/named/[root@dns1 named]# lsdata dynamic named.ca named.empty named.localhost named.loopback slaves# 注意与区域配置文件相一致[root@dns1 named]# cp -a named.localhost bilibili.localhost[root@dns1 named]# cp -a named.loopback bilibili.loopback# 正向解析[root@dns1 named]# vim bilibili.localhost$TTL 1D@ IN SOA bilibili.com. rname.invalid. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS dns.bilibili.com.dns A 10.30.59.193www A 10.30.59.195# 反向解析[root@dns1 named]# vim bilibili.loopback$TTL 1D@ IN SOA bilibili.com. rname.invalid. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS dns.bilibili.com.93 PTR dns.bilibili.com.95 PTR www.bilibili.com.# 启动服务[root@dns1 named]# systemctl restart named[root@dns1 named]# netstat -ntlp |grep namedtcp 0 0 10.30.59.193:53 0.0.0.0: LISTEN 12550/namedtcp 0 0 127.0.0.1:53 0.0.0.0: LISTEN 12550/namedtcp 0 0 127.0.0.1:953 0.0.0.0: LISTEN 12550/namedtcp6 0 0 ::1:53 ::: LISTEN 12550/namedtcp6 0 0 ::1:953 ::: LISTEN 12550/named
测试# 使用第二台虚拟机, 配置dns为DNS服务器,测试[root@localhost ~]# hostnamectl set-hostname dns2[root@localhost ~]# bash[root@dns2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno16780032DNS1=10.30.59.193[root@dns2 ~]# systemctl restart network# bilibili地址显示为10.30.59.195,说明配置成功[root@dns2 ~]# ping www.bilibili.comPING www.bilibili.com (10.30.59.195) 56(84) bytes of data.From 10.30.59.194 icmp_seq=1 Destination Host UnreachableFrom 10.30.59.194 icmp_seq=2 Destination Host UnreachableFrom 10.30.59.194 icmp_seq=3 Destination Host UnreachableFrom 10.30.59.194 icmp_seq=4 Destination Host Unreachable
4.2主从DNS服务器目的:减轻主服务器的压力,备份环境准备:centos7-1511,关闭防火墙 selinux# 主从两个节点# 配置本地yum源[root@dns1 ~]# cat /etc/yum.repos.d/local.repo[centos]name=centosbaseurl=file:///opt/centosgpgcheck=0enabled=1[root@dns2 ~]# cat /etc/yum.repos.d/local.repo[centos]name=centosbaseurl=file:///opt/centosgpgcheck=0enabled=1# 安装服务[root@dns1 ~]# yum install -y bind[root@dns2 ~]# yum install -y bind主节点配置# 主节点配置[root@dns1 ~]# vim /etc/named.confoptions {listen-on port 53 { 10.30.59.193; };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";allow-query { any; };# 区域配置文件只保留一个模板文件[root@dns1 ~]# vim /etc/named.rfc1912.zoneszone "bilibili.com" IN {type master;file "bilibili.localhost";allow-update { 10.30.59.194; };};# 修改正向解析配置文件 相对于基本服务,这里多配置一个serial充当版本作用[root@dns1 ~]# cd /var/named/[root@dns1 named]# cp -a named.localhost bilibili.localhost[root@dns1 named]# vim bilibili.localhost$TTL 1D@ IN SOA bilibili.com. rname.invalid. (20211208 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS dns.bilibili.com.dns A 10.30.59.193www A 10.30.59.195[root@dns1 named]# service named restartRedirecting to /bin/systemctl restart named.service
从节点配置[root@dns2 ~]# vim /etc/named.conf10 options {11 listen-on port 53 { 10.30.59.194; };12 listen-on-v6 port 53 { ::1; };13 directory "/var/named";14 dump-file "/var/named/data/cache_dump.db";15 statistics-file "/var/named/data/named_stats.txt";16 memstatistics-file "/var/named/data/named_mem_stats.tx t";17 allow-query { any; };# 配置区域配置文件 只保留一个模板文件[root@dns2 ~]# vim /etc/named.rfc1912.zoneszone "bilibili.com" IN {type slave;masters { 10.30.59.193; };file "slaves/bilibili.localhost";allow-update { none; };};# 从服务器不需要配置解析文件,会自动同步master节点服务器文件到slaves目录下# 启动前,确认为空目录[root@dns2 ~]# ls /var/named/slaves/# 启动服务[root@dns2 ~]# service named startRedirecting to /bin/systemctl start named.service[root@dns2 ~]# ls /var/named/slaves/bilibili.localhost
验证结果给测试机配置dns为从服务器的地址[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16780032# 从节点的地址DNS1=10.30.59.194[root@localhost ~]# systemctl restart netwoprkFailed to restart netwoprk.service: Unit netwoprk.service failed to load: No such file or directory.[root@localhost ~]# systemctl restart network[root@localhost ~]# ping www.bilibili.comPING www.bilibili.com (10.30.59.195) 56(84) bytes of data.64 bytes from 10.30.59.195: icmp_seq=1 ttl=64 time=0.028 ms64 bytes from 10.30.59.195: icmp_seq=2 ttl=64 time=0.042 ms64 bytes from 10.30.59.195: icmp_seq=3 ttl=64 time=0.040 ms^C--- www.bilibili.com ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2001msrtt min/avg/max/mdev = 0.028/0.036/0.042/0.009 ms
4.3DNS缓存服务器目的:加快解析速度,提高工作效率实验软件:dnsmasq操作:接上一个环境继续操作,把从服务器当作缓存服务器# 停掉从服务器[root@dns2 ~]# service named stopRedirecting to /bin/systemctl stop named.service# 安装dnsmasq 可能已经安装yum install -y dnsmasq# 修改配置文件[root@dns2 ~]# vim /etc/dnsmasq.confdomain=bilibili.comserver=10.30.59.193cache-size=150# 重启服务[root@dns2 ~]# service dnsmasq restartRedirecting to /bin/systemctl restart dnsmasq.service# 测试 如果没有nslookup命令,下载bind-utils[root@localhost ~]# nslookup www.bilibili.comServer: 10.30.59.194Address: 10.30.59.194#53Name: www.bilibili.comAddress: 10.30.59.195# 关掉主服务器[root@dns1 ~]# systemctl stop named# 再次测试 非权威回答[root@localhost ~]# nslookup www.bilibili.comServer: 10.30.59.194Address: 10.30.59.194#53Non-authoritative answer:Name: www.bilibili.comAddress: 10.30.59.195
请求顺序# 客户端从缓存服务器请求, 缓存服务器没有,去主服务查找, 主服务器没启动,测试没反应, 启动主服务器,再次测试, 缓存服务器再次请求主服务器,获取域名,返回给客户端# 主服务器没启动[root@localhost ~]# nslookup dns.bilibili.com^C# 启动主服务器[root@dns1 ~]# systemctl start named[root@localhost ~]# nslookup dns.bilibili.comServer: 10.30.59.194Address: 10.30.59.194#53Name: dns.bilibili.comAddress: 10.30.59.193# 再次关闭,有缓存可以解析到[root@dns1 ~]# systemctl stop named[root@localhost ~]# nslookup dns.bilibili.comServer: 10.30.59.194Address: 10.30.59.194#53Non-authoritative answer:Name: dns.bilibili.comAddress: 10.30.59.193
4.4 智能DNS(分离解析)目的:NDS分离解析即将相同域名解析为不同的IP地址,实现网络中一些网站为了让用户有更好的体验效果解析速度更快,就把来自不通运营商的用户解析到相对应的服务器,这样就大大提升了访问速度实验环境:配置一台apache服务器,两个网卡模拟内外网, 两台测试机从内网外网分别访问dns能够正确解析(内网地址访问解析内网地址,外网地址访问解析外网地址)# 内网测试机[root@localhost ~]# hostnamectl set-hostname net1[root@localhost ~]# bash[root@int ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736TYPE=EthernetBOOTPROTO=staticDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noNAME=eno16777736ONBOOT=yesIPADDR=192.168.100.10NETMASK=255.255.255.0GATEWAY=192.168.100.30DNS1=192.168.100.30# 外网测试机[root@localhost ~]# hostnamectl set-hostname ext[root@localhost ~]# bash[root@ext ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736TYPE=EthernetBOOTPROTO=staticDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noNAME=eno16777736ONBOOT=yesIPADDR=192.168.200.10NETMASK=255.255.255.0GATEWAY=192.168.200.2DNS1=192.168.200.30# 配置双网卡出了一个问题: 一开始本着第一块网卡仅主机模式,然后第二块网卡net模式,就调整第一块网卡为仅主机模式,新加的第二块网卡为net模式, 结果网络不行,就重新配置为第一块网卡为net模式,第二块网卡为net模式 不影响本案例# apache[root@localhost ~]# hostnamectl set-hostname apache[root@localhost ~]# bash[root@apache ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736TYPE=EthernetBOOTPROTO=staticDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noNAME=eno16777736ONBOOT=yesIPADDR=192.168.200.20NETMASK=255.255.255.0GATEWAY=192.168.200.2DNS1=114.114.114.114[root@apache ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno33554984TYPE=EthernetBOOTPROTO=staticDEFROUTE=yesPEERDNS=yesPEERROUTES=yesNAME=eno33554984ONBOOT=yesIPADDR=192.168.100.20NETMASK=255.255.255.0# dns[root@localhost ~]# hostnamectl set-hostname dns[root@localhost ~]# bash[root@dns ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736TYPE=EthernetBOOTPROTO=staticDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noNAME=eno16777736ONBOOT=yesIPADDR=192.168.200.30NETMASK=255.255.255.0GATEWAY=192.168.200.2DNS1=114.114.114.114[root@dns ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno33554984TYPE=EthernetBOOTPROTO=staticDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noNAME=eno33554984ONBOOT=yesIPADDR=192.168.100.30NETMASK=255.255.255.0# apache 节点安装httpd[root@apache ~]# yum install -y httpd[root@apache ~]# vim /var/www/html/index.htmldns分离解析验证成功
[root@apache ~]# systemctl start httpd[root@apache ~]# systemctl enable httpdCreated symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.# dns节点安装bind[root@dns ~]# yum install -y bind
测试一下直接地址访问看看有没有问题分离解析配置# dns节点永久开启路由转发[root@dns ~]# vim /etc/sysctl.confnet.ipv4.ip_forward = 1[root@dns ~]# sysctl -pnet.ipv4.ip_forward = 1#修改主配置文件# 两个any 监听任意地址# 加两个视图 分别监听内网和其他地址 ,视图匹配规则为自上而下匹配,注意顺序 match-client匹配地址, include 指定区域配置文件[root@dns ~]# vim /etc/named.conflisten-on port 53 { any; };allow-query { any; };view lan {match-clients { 192.168.100.0/24; };zone "." IN {type hint;file "named.ca";};include "/etc/lan.zones";};view wan {match-clients { any; };zone "." IN {type hint;file "named.ca";};include "/etc/wan.zones";};#include "/etc/named.rfc1912.zones";# 修改区域配置文件[root@dns ~]# cp -a /etc/named.rfc1912.zones /etc/lan.zones[root@dns ~]# vim /etc/lan.zoneszone "bilibili.com" IN {type master;file "lan.localhost";allow-update { none; };};[root@dns ~]# cp -a /etc/lan.zones /etc/wan.zones[root@dns ~]# vim /etc/wan.zoneszone "bilibili.com" IN {type master;file "wan.localhost";allow-update { none; };};# 修改解析文件[root@dns etc]# cd /var/named/[root@dns named]# lsdata dynamic named.ca named.empty named.localhost named.loopback slaves[root@dns named]# cp -a named.localhost lan.localhost[root@dns named]# vim lan.localhost$TTL 1D@ IN SOA bilibili.com. rname.invalid. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS dns.bilibili.com.dns A 192.168.100.30www A 192.168.100.20[root@dns named]# cp -a lan.localhost wan.localhost[root@dns named]# vim wan.localhost$TTL 1D@ IN SOA bilibili.com. rname.invalid. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS dns.bilibili.com.dns A 192.168.200.30www A 192.168.200.20
测试# 启动服务[root@dns named]# systemctl start named# 内网机测试[root@int ~]# curl www.bilibili.comdns分离解析验证成功
\\ 查看是否显示你在主页显示的内容# 外网机测试[root@ext ~]# curl www.bilibili.comdns分离解析验证成功
\\查看是否同理# 再使用nslookup测试# 两个节点都下载[root@ext ~]# yum install -y bind-utils[root@int ~]# nslookup www.bilibili.comServer: 192.168.100.30Address: 192.168.100.30#53Name: www.bilibili.comAddress: 192.168.100.20[root@ext ~]# nslookup www.bilibili.comServer: 192.168.200.30Address: 192.168.200.30#53Name: www.bilibili.comAddress: 192.168.200.20# 可以发现内外网使用的dns地址不同,并且解析出来的ip不同实验成功
如果ip更多,就多写视图,多谢区域配置文件与解析文件拓展知识: 邮件服务(图片来源网络,侵删)
0 评论