设为首页 收藏本站
查看: 547|回复: 0

[经验分享] RHEL7上配置DNS服务

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-6-9 08:19:01 | 显示全部楼层 |阅读模式
1.课程目标
  • 了解什么是DNS及DNS的重要性;
  • 掌握DNS的层次化区分:根域,顶级域,二级域等;
  • 掌握DNS的配置并能熟练运用;


2.DNS简介
DNS (Domain Name Server)域名解析服务,使用TCP&UDP的53号端口(主从DNS之间用TCP,客户端查询使用UDP)。它可以完成域名与IP地址的互换,可以通过IP地址解析到域名;也可以通过域名解析到IP地址。


2.1.DNS的层次化
  • 根域:根域位于层次化结构的最顶部并用点“.”表示全球有十三个根服务器。一个主根服务器,十二个辅助根服务器。
  • 顶级域:顶级域是按照组织类别或地理位置来划分的,如下:

.gov
政府组织
.com
商业组织
.net
网络中心
.org
非盈利性组织
.edu
教育部门
. cn  .uk  .us
国家国别的代码,cn表示中国,uk表示英国,us表示美国
.com.cn
国内商业机构
.net.cn
国内互联网机构
.org.cn
国内非盈利性组织
注:
  • .com &.net由internic国际组织管理,而以.cn结尾的是由cnnic中国互联网中心管理的。
  • 二级域:有国际域名组织为互联网中的个人或部门制定和登记的二级域(如:baidu.com)



2.2.DNS的查询方式
  • 迭代查询:服务器与服务器之间的查询。本地域名服务器向根域名服务器的查询通常是采用迭代查询(反复查询)。当根域名服务器收到本地域名服务器的迭代查询请求报文时,要么给出所要查询的IP地址,要么告诉本地域名服务器下一步应向那个域名服务器进行查询。然后让本地域名服务器进行后续的查询;
  • 递归查询:客户端与服务器之间的查询。主机向本地域名服务器的查询一般都是采用递归查询。如果主机所询问的本地域名服务器不知道被查询域名的 IP 地址,那么本地域名服务器就以 DNS 客户的身份,向其他根域名服务器继续发出查询请求报文。最后会给客户端一个准确的返回结果,无论是成功与否。

查询举例方式如下:
wKiom1V1qB2zmKJuAAHklivCipc580.jpg
步骤1:当客户端在IE输入www.baidu.com,客户端去请求本地域服务器解析,此过程为发起递归查询;步骤2:本地服务器接到查询请求后,查看区域文件,发现不是自己管制的区域,则发送给根域进行解
       析。此过程为迭代查询;
步骤3:根域服务接收到请求后,查看区域文件,回复请求的主机,
       去找.com域服务器。此过程是迭代查询;
步骤4:本地域服务器,接收到根域服务器的回复后,去找.com域服务器,请求解析。此过程序是迭代
       查询;
步骤5:.com服务器接收到请求之后,查看自己的区域文件,回复主机去找baidu.com域服务,此过程是
       迭代查询;
步骤6:本地域服务器,接收到.com域服务器的回复后,去找baidu.com域服务器,请求解析,此过程是
       迭代查询;
步骤7:baidu.com域服务器接收到请求后,查看自己的区域文件,发现是自己所管制的区域,然后查看
       区域解析文件,把对应的IP地址发送给请求的服务器,此过程是迭代查询;
步骤8:本地服务器接收到baidu.com域服务器的回复,把FQDN对应IP地址传送给客户端,此过程为递
       归查询。


2.3.DNS解析类型
  • FQDN(Fully Qualified Domain Name) 完全合格域名,由主机+域名组成,如:www.baidu.com
  • 正向解析:由FQDN解析到IP地址;
  • 反向解析:由IP地址解析到FQDN。



2.4./etc/hosts解析
默认的,linux的解析是先经过/etc/hosts文件,再经过DNS的解析,此解析方式在/etc/nsswitch.conf文件中是由明确规定的,如下:
[iyunv@freeit ~]# vim /etc/nsswitch.conf
……
passwd:     files sss
shadow:     files sss
group:      files sss
#initgroups: files

#hosts:     db files nisplus nis dns
hosts:      files dns
……
在/etc/hosts文件中填写对192.168.10.10的解析
---------------------------------------填写之前首先验证下--------------------------
[iyunv@gyh ~]# ping freeit.example.com
ping: unknown host freeit.example.com
---------------------------------------填写之后验证-------------------------------------
[iyunv@gyh ~]# echo 192.168.10.10    freeit.example.com >> /etc/hosts
[iyunv@gyh ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.10 freeit.example.com

[iyunv@gyh ~]# ping freeit.example.com                              
PING freeit.example.com (192.168.10.10) 56(84) bytes of data.
64 bytes from freeit.example.com (192.168.10.10): icmp_seq=1 ttl=64 time=3.89 ms
64 bytes from freeit.example.com (192.168.10.10): icmp_seq=2 ttl=64 time=0.307 ms
64 bytes from freeit.example.com (192.168.10.10): icmp_seq=3 ttl=64 time=0.442 ms
^B64 bytes from freeit.example.com (192.168.10.10): icmp_seq=4 ttl=64 time=0.426 ms
64 bytes from freeit.example.com (192.168.10.10): icmp_seq=5 ttl=64 time=0.635 ms
如上:/etc/hosts这个文件同样能实现解析的目的。但是没有DNS的功能强大。下面我们就来介绍DNS的强大之处。



3.DNS相关配置

3.1.安装DNS
我们知道,RHEL5.x&6.x之前的DNS软件包名为bind。但是在RHEL7中,DNS名字有所改变,为:unbound。下面我们就来其安装。
[iyunv@freeit ~]# yum -y install unbound
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package unbound.x86_64 0:1.4.20-19.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
……
----------------------------------------------启动服务---------------------------------------------
[iyunv@freeit ~]# systemctl restart unbound
//启动DNS服务
[iyunv@freeit ~]# systemctl enable unbound   
ln -s '/usr/lib/systemd/system/unbound.service' '/etc/systemd/system/multi-user.target.wants/unbound.service'
//下次系统重启自动启动DNS服务



3.2.配置文件修改
说到服务,就离不开配置文件的描述。Unbound安装好之后,缺省配置文件在/etc/unbound/ unbound.conf.
例1:修改端口监听地址
----------------------------------------------查看默认监听地址-----------------------------------------
[iyunv@freeit ~]# netstat -tunlp |grep unbound
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      3333/unbound        
tcp        0      0 127.0.0.1:8953          0.0.0.0:*               LISTEN      3333/unbound        
tcp6       0      0 ::1:53                  :::*                    LISTEN      3333/unbound        
tcp6       0      0 ::1:8953                :::*                    LISTEN      3333/unbound        
udp        0      0 127.0.0.1:53            0.0.0.0:*                           3333/unbound        
udp6       0      0 ::1:53                  :::*                                3333/unbound        
//默认监听本地回环地址,也就是现在只有自己能访问DNS服务,其它主机不能访问本机的DNS服务
-------------------------------------------修改监听地址----------------------------------------
[iyunv@freeit ~]# vim /etc/unbound/unbound.conf
……
38         # interface: 0.0.0.0
39         interface: 0.0.0.0
……
//找到38行,yy复制并p粘贴到下面一行,去掉注释行,打开监听全网功能。
------------------------------------------重启服务查看-----------------------------------------
[iyunv@freeit ~]# systemctl restart unbound         
[iyunv@freeit ~]# netstat -tunlp |grep unbound  
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      3461/unbound        
tcp        0      0 127.0.0.1:8953          0.0.0.0:*               LISTEN      3461/unbound        
tcp6       0      0 ::1:8953                :::*                    LISTEN      3461/unbound        
udp        0      0 0.0.0.0:53              0.0.0.0:*                           3461/unbound
//如上,现在53号端口监听的是0.0.0.0,即所有网段。
让53号端口监听全网,相当于RHEL6配置文件中的:listen-on port 53 { any; };

例2:修改允许查询的范围
在RHEL6中,DNS配置文件中有这样一句:allow-query     { localhost; };。此句定义的是允许向本机查询(迭代&递归)的主机范围,localhost代表只有本机可以向本机查询。而在配置中,经常改localhost为any,让所有主机能够向本机查询DNS。所以,在RHEL7中,也要做这样的修改,只不过修改内容不同而已,如下:
[iyunv@freeit ~]# vim /etc/unbound/unbound.conf
……
177         # access-control: 0.0.0.0/0 refuse
178         access-control: 0.0.0.0/0 allow
179         # access-control: 127.0.0.0/8 allow
……
找到配置文件/etc/unbound/unbound.conf的第177行,缺省为注释行,且内容为拒绝访问。复制本行内容到下面一行,去掉注释“#“,改refuse为allow。然后保存退出,重启服务即可。

例3:去除IPV6
实际生产环境中,使用IPV6的公司很少,基本没有(当然不排除有)。所以,DNS监听Ipv6是没有必要的,这里,我们可以去除IPV6的监听(如果公司环境中有IPV6环境,可忽略此部分实验)。
---------------------------------------修改前查看监听内容----------------------------------
[iyunv@freeit ~]# netstat -tunlp |grep unbound
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      3461/unbound        
tcp        0      0 127.0.0.1:8953          0.0.0.0:*               LISTEN      3461/unbound        
tcp6       0      0 ::1:8953                :::*                    LISTEN      3461/unbound        
udp        0      0 0.0.0.0:53              0.0.0.0:*                           3461/unbound
//有IPV6的监听内容
---------------------------------------------去除对Ipv6监听-----------------------------------------
152         # do-ip4: yes
153         
154         # Enable IPv6, "yes" or "no".
155         # do-ip6: yes
156          do-ip6: no
//找到155行内容,在其下复制一行并去除注释,改yes为no,重启服务即可去除对Ipv6的监听
-----------------------------------------验证-------------------------------------------------------
[iyunv@freeit ~]# systemctl restart unbound     
[iyunv@freeit ~]# netstat -tunlp |grep unbound
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      3986/unbound        
tcp        0      0 127.0.0.1:8953          0.0.0.0:*               LISTEN      3986/unbound        
udp        0      0 0.0.0.0:53              0.0.0.0:*                           3986/unbound   
//没有了对IPV6的监听。   
注:
  • 缺省情况下,53号端口即监听IPV4也监听IPV6,要去除对IPV6的监听,需设置对IPV6的监听参数yes为no;
  • 通过去掉对IPV4监听的注释“#“来移除对IPV6监听的办法不能生效。




3.3.创建解析文件
RHEL5.x&6.x系统中,DNS的解析文件分正向和反向两个解析文件,并且有解析文件的模板文件。但是在RHEL7中,正反向解析文件合并为一个,并且无模板文件,需自己创建,路径可以在主配置文件中查看:
[iyunv@freeit ~]# vim /etc/unbound/unbound.conf
……
453         # You can add locally served data with
454         # local-zone: "local." static
455         # local-data: "mycomputer.local. IN A 192.0.2.51"
//正向解析可参考语法
456         # local-data: 'mytext.local TXT "content of text record"'
457         #
458         # You can override certain queries with
459         # local-data: "adserver.example.com A 127.0.0.1"
460         #
461         # You can redirect a domain to a fixed address with
462         # (this makes example.com, www.example.com, etc, all go to 192.0.2.3)
463         # local-zone: "example.com" redirect
464         # local-data: "example.com A 192.0.2.3"
465         #
466         # Shorthand to make PTR records, "IPv4 name" or "IPv6 name".
467         # You can also add PTR records using local-data directly, but then
468         # you need to do the reverse notation yourself.
469         # local-data-ptr: "192.0.2.3 www.example.com"
//反向解析参考语法
470
471         include: /etc/unbound/local.d/*.conf
472
473         # service clients over SSL (on the TCP sockets), with plain DNS inside
……
如上:471行加粗字体,此句规定了解析文件的位置在/etc/unbound/local.d下,并且必须以“.conf“结尾。

解析文件的创建,也并不是没有一点可用的模板,主配置文件中可以在454行找到语法格式,复制到创建的解析文件然后自己补充完善即可(此解析文件建议参考RHEL6的解析文件理解)。
--------------------------------------------查看本机FQDN------------------------------------
[iyunv@freeit ~]# hostname
freeit.example.com
//由此可知,域名为example.com
-------------------------------------------创建解析文件--------------------------------------------
[iyunv@freeit ~]# vim /etc/unbound/local.d/example.conf
local-zone: "example.com." static
local-data: "example.com. 86400 IN SOA ns.example.com. root 1 1D 1H 1W 1H"
local-data: "ns.example.com.            IN A 192.168.10.10"
local-data: "freeit.example.com.         IN A 192.168.10.10"
local-data: "gyh.example.com.           IN A 192.168.10.11"
local-data-ptr: "192.168.10.10           ns.example.com."
local-data-ptr: "192.168.10.10           freeit.example.com."
local-data-ptr: "192.168.10.11           gyh.example.com."
----------------------------------------查看RHEL6上解析文件以作对比--------------------
[iyunv@freeit ~]# vim /var/named/named.localhost
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
        AAAA    ::1
说明:
  • 第一行内容定义静态的域名为:example.com;
  • 第二行:定义example.com相当于RHEL6中的@,即FQDN;

                       定义86400相当于$TTL 1D;
                       定义的SOA记录一致;
  定义的root相当于@ rname.invalid,而RHEL6中,rname.invalid我们经常改为root,即邮件发给那个用户;
Root后面内容与RHEL6中的serial、refresh、retry、expore、minimum对应。
  • 第三、四、五行为正向解析记录;
  • 第六、七、八行为反向解析记录。




3.4.禁用服务用户
每个服务都是有其专用的服务用户。而DNS的服务用户为unbound,配置文件中默认是启用了此用户。而实际情况下,此用户是没有必要启用的。所以,这里要清除此用户,即禁用服务用户。
[iyunv@freeit ~]# vim /etc/unbound/unbound.conf
…….
211         # if given, user privileges are dropped (after binding port),
212         # and the given username is assumed. Default is user "unbound".
213         # If you give "" no privileges are dropped.
214         username: "unbound"
215
216         # the working directory. The relative files in this config
……
如上,找到配置文件的第214行,删除unbound即可,删除后为:【username  ” “】。



3.5.验证
3.5.1.验证DNS配置

[iyunv@freeit ~]# unbound-checkconf
unbound-checkconf: no errors in /etc/unbound/unbound.conf
验证无配置问题,即可重启服务
[iyunv@freeit ~]# systemctl restart unbound


3.5.2.localhost验证

-----------------------------------------------修改本机DNS-------------------------------------------------
[iyunv@freeit ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
HWADDR=00:0C:29:70:F0:64
TYPE=Ethernet
BOOTPROTO=static
IPADDR="192.168.10.10"
PREFIX="24"
GATEWAY="192.168.10.1"
DNS1=192.168.10.10
DEFRUTE=yesPEERNS=yes
PEEROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=4765ab39-581e-4412-8642-af58cbf93140
ONBOOT=no

[iyunv@freeit ~]# systemctl restart network
----------------------------------------------------nslookup验证--------------------------------------------
[iyunv@freeit ~]# nslookup
> 192.168.10.10
Server:         127.0.0.1
Address:        127.0.0.1#53

10.10.168.192.in-addr.arpa      name = ns.example.com.
10.10.168.192.in-addr.arpa      name = freeit.example.com.
> 192.168.10.11
Server:         127.0.0.1
Address:        127.0.0.1#53

11.10.168.192.in-addr.arpa      name = gyh.example.com.
> gyh.example.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   gyh.example.com
Address: 192.168.10.11
> freeit.example.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   freeit.example.com
Address: 192.168.10.10
//验证通过,无问题


3.5.3.登录远程主机11验证

修改DNS
[iyunv@gyh ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DNS1=192.168.10.10
DEFROUTE=no
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=210db2a8-848e-46e2-83c2-23f0c30e227f
ONBOOT=yes
IPADDR0=192.168.10.11
PREFIX0=24
GATEWAY0=192.168.10.1
HWADDR=00:0C:29:02:1C:E7
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

[iyunv@gyh ~]# systemctl restart network

Nslookup验证
[iyunv@gyh ~]# nslookup
> freeit.example.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
验证失败,这是为什么呢?
回想前面的Firewall,缺省区域为public,缺省services策略进允许ssh和ipv6-client。而Firewall除了这些缺省勾选的服务允许外,其他一律拒绝。所以可知,解析请求是被DNS服务器的Firewall阻挡。

在DNS服务器上开放DNS服务
[iyunv@freeit ~]# ifconfig eth0 | grep "netmask" | cut -d " " -f10
192.168.10.10
[iyunv@freeit ~]# firewall-cmd --add-service=dns --permanent
success
[iyunv@freeit ~]# firewall-cmd --reload
success
[iyunv@freeit ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client dns ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:
//DNS服务器上Firewall开放DNS访问

再次验证
[iyunv@gyh ~]# ifconfig eth0 |grep "netmask" |cut -d " " -f10
192.168.10.11
[iyunv@gyh ~]# nslookup
> freeit.example.com
Server:         192.168.10.10
Address:        192.168.10.10#53

Name:   freeit.example.com
Address: 192.168.10.10
> gyh.example.com
Server:         192.168.10.10
Address:        192.168.10.10#53

Name:   gyh.example.com
Address: 192.168.10.11
> 192.168.10.10
Server:         192.168.10.10
Address:        192.168.10.10#53

10.10.168.192.in-addr.arpa      name = ns.example.com.
10.10.168.192.in-addr.arpa      name = freeit.example.com.
验证通过。DNS Server设置成功。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-75274-1-1.html 上篇帖子: RHEL7上配置NFS服务 下篇帖子: linux下搭建HA高可用集群
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表