# DNS

DNS 53

/etc/resolv.conf

在/etc/resolv.conf 配置文件:关键字domain和search作用是一样的。search 它的多个参数指明域名查询顺序。当要查询没有域名的主机,主机将在由search 声明的域中分别查找。domain 和search 不能共存;如果同时存在,后面出现的将会被使用。

]# cat vi /etc/resolv.conf
    # Generated by NetworkManager
    search openstack.local dev.com example.local
    nameserver 192.168.122.21

例1:查询主机名,因为主机名后面没有点,就认为是主机名,所以先添加search里的每一项依次组成FQDN(完全合格域名)来查询,完全合格域名查询未找到,就再认为主机名是完全合格域名来查询。
]# host -a centos7-bind-1
    Trying "centos7-bind-1.openstack.local"
    Trying "centos7-bind-1.dev.com"
    Trying "centos7-bind-1.example.local"
    Trying "centos7-bind-1"
    ;; connection timed out; no servers could be reached

例2:查询主机名,因为主机名中有点(不是末尾有点),就认为是完全合格域名,先用它来查询,查询失败就把它当成是主机名来进行,添加search里的每一项组成FQDN(完全合格域名)来查询。
]# host -a centos7-bind-1.com
    Trying "centos7-bind-1.com"
    Received 109 bytes from 192.168.122.21#53 in 177 ms
    Trying "centos7-bind-1.com.openstack.local"
    Trying "centos7-bind-1.com.dev.com"
    Trying "centos7-bind-1.com.example.local"
    Host centos7-bind-1.com not found: 3(NXDOMAIN)
    Received 125 bytes from 192.168.122.21#53 in 55 ms

例3:查询主机名,因为主机名中末尾有点,则认为是完全合格域名,只用它来查询(不会再添加search里的每一项)。查询次数会与search里项域名个数有关。
]# host -a centos7-bind-1.
    Trying "centos7-bind-1"
    ;; connection timed out; trying next origin
    Trying "centos7-bind-1"
    ;; connection timed out; trying next origin
    Trying "centos7-bind-1"
    ;; connection timed out; trying next origin
    Trying "centos7-bind-1"
    ;; connection timed out; no servers could be reached

安装

# DNS的相关软件和文件

bind(Berkeley Internet Name Domain):DNS服务端软件
bind-chroot:DNS服务端的补充软件,提供牢笼政策(创建DNS服务的根目录防止被攻击后波及系统安全)
bind-utils:DNS客户端软件,提供nslookup命令、host命令等
	+ `nslookup或host [选项] 域名 [IP地址]`:由该IP临时提供DNS服务,如果省略则使用`/etc/resolv.conf`指定的nameserver

DNS服务:`/usr/lib/systemd/system/named.service`
DNS的配置文件:`/etc/named.conf`
DNS的默认地址库文件父目录:`/var/named`
DNS的地址库文件模板:`/var/named/named.localhost`
	+ 用户named需要对地址库文件无r权限
	+ 主从架构中slave的用户named需要对地址库文件有rwx权限

# 查看帮助模板
方式一:`man named.conf`
方式二:`vim /usr/share/doc/bind/sample/etc/named.conf``vim /usr/share/doc/bind/sample/var/named/named.localhost`

# 递归解析和迭代解析
递归查询(递归解析,默认):客户端将解析请求发送给首选DNS服务器(互联网运营商),首选DNS服务器与其他的DNS服务器交流,最终将解析结果返回给客户端。
迭代查询(迭代解析):客户端发送请求给首选DNS服务器,首选DNS服务器告知客户端下一个DNS服务器地址。
	+ 一般首选DNS服务器给客户端提供递归解析,这个过程中自身通过迭代解析查询解析结果。

# 正向解析和反向解析
正向解析:将域名解析为IP地址
反向解析:将IP地址解析为域名

正向解析

# 正向解析
DNS服务器(192.168.88.53):
[root@dns ~]# getenforce 
	Disabled
[root@dns ~]# rpm -q firewalld
	未安装软件包 firewalld 
[root@dns ~]# yum install -y bind bind-chroot
[root@dns ~]# cat -n /etc/named.conf
     1  // named.conf
     2  // See /usr/share/doc/bind*/sample/ for example named configuration files.	// 模板文件
     3
     4  options {
				// listen-on port 53 { any; }		// 监听本地任意IPv4地址的53端口,缺省配置
				// listen-on-v6 port 53 { any; }	// 监听本地任意IPv6地址的53端口,缺省配置
				// allow-query	{ any; };	// 允许访问本DNS服务的客户端,缺省配置。可以是IP地址、网段、any所有
     5          directory	"/var/named";	// 地址库文件父目录
     			// recursion  no;			// 禁用递归解析(使用迭代解析)。缺省为yes
     6  };
     7
     8  zone "example" IN {					// 解析的域名
     9          type 	master;				// master主服务器、slave从服务器、hint根域服务器、forward转发服务器
    10          file 	"example.zone";		// 地址库文件
    11  };
[root@dns ~]# ll /var/named/named.localhost		# 注意:用户named必须对地址库文件有r权限
	-rw-r----- 1 root named 194 116 23:27 /var/named/named.localhost
[root@dns ~]# cp -a /var/named/{named.localhost,example.zone}
[root@dns ~]# cat -n /var/named/example.zone
     1  $TTL 1D
     2  @       IN SOA  @ rname.invalid. (
     3                                          0       ; serial
     4                                          1D      ; refresh
     5                                          1H      ; retry
     6                                          1W      ; expire
     7                                          3H )    ; minimum
		# 以下配置注意顶格书写,NS(nameserver主站点)、A(IPv4地址)、AAAA(IPv6地址)、CNAME(解析记录的别名)、PTR(Pointer Recore反向解析)
		# 注意:地址库文件所有的域名都必须以点作为结尾,没有则默认补全域名。
     8           NS     server			# 声明主站点,完整写法:`example NS server`,server为任意取名,空则`@`
     9  server   A      192.168.88.11	# 不以点结尾,解析域名`server.example`
    10  www      A      11.11.11.11		# 不以点结尾,解析域名`www.example`
	11	xxx		 A		22.22.22.22		# 不以点结尾,解析域名`xxx.example`
	12 	yyy		 CNAME	www				# 不以点结尾,解析域名`yyy.example`,解析结果同`xxx.example`
	13 	*		 A		33.33.33.33		# 不以点结尾,解析域名`*.example`,`*`代表任意,不匹配其它域名才匹配这条
	14  example	 A		44.44.44.44		# 不以点结尾,解析域名`example.example`
	15	@		 A 		55.55.55.55		# 不以点结尾,解析域名`example`,`@`代表空
	16	example. A		7.7.7.7			# 以点结尾,解析`example`
		# 先匹配域名,如果多个匹配项,按照第一个匹配项返回解析结果
[root@dns ~]# systemctl enable named.service --now
[root@dns ~]# ss -ntulp | grep :53				# 返回DNS服务

# 客户端使用DNS服务
[root@client ~]# echo 'nameserver 192.168.88.53' > /etc/resolv.conf		# 手动指定DNS服务器地址
[root@client ~]# yum install -y bind-utils
[root@client ~]# nslookup server.example
    Server:         192.168.88.53
    Address:        192.168.88.53#53

    Name:   server.example
    Address: 192.168.88.11
[root@client ~]# host www.example
	www.example has address 11.11.11.11
[root@client ~]# host xxx.example
	xxx.example has address 22.22.22.22
[root@client ~]# host yyy.example
    yyy.example is an alias for xxx.example.
	xxx.example has address 22.22.22.22
[root@client ~]# host abcd.example
	abcd.example has address 33.33.33.33
[root@client ~]# host example.example
	example.example has address 44.44.44.44
[root@client ~]# host example
	example has address 55.55.55.55		如果`example.``@`之前,则解析结果为`example has address 7.7.7.7`

分离解析

  • DNS分离解析:区分查询请求的源IP地址,为不同类型的客户端提供不同的解析结果。
    • 例如:位于不同地方搜索地图上最近的连锁酒店,返回结果不一样。
# DNS分离解析:区分查询请求的源IP地址,为不同类型的客户端提供不同的解析结果。
DNS服务器(192.168.88.53):
[root@dns ~]# getenforce 
	Disabled
[root@dns ~]# rpm -q firewalld
	未安装软件包 firewalld 
[root@dns ~]# yum install -y bind bind-chroot
[root@dns ~]# cat -n /etc/named.conf			# 按顺序匹配,匹配即停止。最后一般使用`any;`兜底
     1  options {
     2          directory       "/var/named";
     3  };
     4
     5  view "class01" {
     6          match-clients { 192.168.88.104; 192.168.100.0/24; };
     7          zone "example" IN {
     8                  type    master;
     9                  file    "example.zone";
    10          };
    11  };
    12  view "class02" {
    13          match-clients { any; };
    14          zone "example" IN {
    15                  type    master;
    16                  file    "other.zone";
    17          };
    18  };
[root@dns ~]# ll /var/named/named.localhost		# 注意:用户named必须对地址库文件有r权限
	-rw-r----- 1 root named 194 116 23:27 /var/named/named.localhost
[root@dns ~]# cp -a /var/named/{named.localhost,example.zone}
[root@dns ~]# cat -n /var/named/example.zone
     1  $TTL 1D
     2  @       IN SOA  @ rname.invalid. (
     3                                          0       ; serial
     4                                          1D      ; refresh
     5                                          1H      ; retry
     6                                          1W      ; expire
     7                                          3H )    ; minimum
     8          NS      server
     9  server  A       192.168.88.53
    10  www     A       11.11.11.11
[root@dns ~]# cp -a /var/named/{example.zone,other.zone}
[root@dns ~]# vim /var/named/other.zone
    10  www     A       22.22.22.22
[root@dns ~]# systemctl enable named.service --now
[root@dns ~]# ss -ntulp | grep :53			# 返回DNS服务

# 验证效果
client1(192.168.88.104):
[root@client ~]# echo 'nameserver 192.168.88.53' > /etc/resolv.conf
[root@client ~]# yum install -y bind-utils
[root@client ~]# nslookup www.example
    Server:         192.168.88.53
    Address:        192.168.88.53#53

    Name:   www.example
    Address: 11.11.11.11
[root@client ~]# host www.example
	www.example has address 11.11.11.11

client1(192.168.88.102):
[root@client ~]# echo 'nameserver 192.168.88.53' > /etc/resolv.conf
[root@client ~]# yum install -y bind-utils
[root@client ~]# nslookup www.example
    Server:         192.168.88.53
    Address:        192.168.88.53#53

    Name:   www.example
    Address: 22.22.22.22
[root@client ~]# host www.example
	www.example has address 22.22.22.22

主从架构

# DNS主从架构:
主服务器:允许传输地址库文件给slave,服务器类型为master
从服务器:服务器类型为slave,从master服务器上下载(同步更新)地址库文件(默认加密)并重命名,指定下载的地址库文件为明文方式

# DNS主从架构:主(192.168.88.53),从(192.168.88.153)
主DNS服务器(192.168.88.53):
[root@dns ~]# getenforce 
	Disabled
[root@dns ~]# rpm -q firewalld
	未安装软件包 firewalld 
[root@dns ~]# yum install -y bind bind-chroot
[root@dns ~]# cat -n /etc/named.conf
     1  options {
     2          directory       "/var/named";
     3          allow-transfer  { 192.168.88.153; };	// 允许传输文件
     4  };
     5  zone "example" IN {
     6          type    master;					// master服务器
     7          file    "example.zone";
     8  };
[root@dns ~]# ll /var/named/named.localhost
	-rw-r----- 1 root named 194 116 23:27 /var/named/named.localhost
[root@dns ~]# cp -a /var/named/{named.localhost,example.zone}
[root@dns ~]# cat -n /var/named/example.zone
     1  $TTL 1D			// 地址解析记录缓存时间
     2  @       IN SOA  @ rname.invalid. (
     3                                  1949100101 		; serial	// 数据的版本号,约定年月日次数十位
     4                                          1D      ; refresh	// 每一天联系一次,健康检查
     5                                          1H      ; retry		// 健康检查失败后,每隔一小时尝试联系一次
     6                                          1W      ; expire	// 健康检查失败后,联系失败后尝试一周
     7                                          3H )    ; minimum	// 没有设置TTL时的地址解析记录缓存时间
     8          NS      server			
     9			NS		cong
    10	server  A       192.168.88.53
	11	cong	A		192.168.88.153			// 指定第二个DNS服务器
    12  www     A       11.11.11.11		
[root@dns ~]# systemctl enable named.service --now
[root@dns ~]# ss -ntulp | grep :53			# 返回DNS服务

从DNS服务器(192.168.88.153):
[root@dns2 ~]# getenforce 
	Disabled
[root@dns2 ~]# rpm -q firewalld
	未安装软件包 firewalld 
[root@dns2 ~]# yum install -y bind bind-chroot
[root@dns2 ~]# ll -d /var/named/			# 从服务器的用户named需要对地址库文件有rwx权限
	drwxrwx--T 6 root named 162 1112 00:40 /var/named/
[root@dns2 ~]# cat -n /etc/named.conf
     1  options {
     2          directory       "/var/named/";
     3  };
     4  zone "example" IN {
     5          type    slave;					// slave服务器
     6          masters { 192.168.88.53; };		// 指定其master服务器
     7          file    "example.slave";		// 从master服务器下载地址库文件(默认加密)并重命名
	 8			masterfile-format	text;		// 下载的地址库文件为明文
     9  };
[root@dns2 ~]# systemctl enable named.service --now 
[root@dns2 ~]# ss -ntulp | grep :53			# 返回DNS服务	
[root@dns2 ~]# ls /var/named/example.slave 	# 从master服务器下载的地址库文件(明文)
	/var/named/example.slave

# 验证主从架构
[root@client ~]# yum install -y bind-utils
[root@client ~]# nslookup www.example 192.168.88.53
    Server:         192.168.88.53
    Address:        192.168.88.53#53

    Name:   www.example
    Address: 11.11.11.11
[root@client ~]# host www.example
	www.example has address 11.11.11.11
[root@client ~]# nslookup www.example 192.168.88.153
    Server:         192.168.88.153
    Address:        192.168.88.153#53

    Name:   www.example
    Address: 11.11.11.11
[root@client ~]# host www.example
	www.example has address 11.11.11.11

# 主从同步:master服务器修改地址库文件后,修改数据的版本号(比原来的大),并重启服务。slave服务器无需任何操作
[root@dns ~]# sed -Ei 's,1949100101,1949100102,' /var/named/example.zone
[root@dns ~]# sed -Ei '/www/s,11.11.11.11,11.11.11.22,' /var/named/example.zone
[root@dns ~]# systemctl restart named.service 

# 验证主从同步
[root@client ~]# yum install -y bind-utils
[root@client ~]# nslookup www.example 192.168.88.53
    Server:         192.168.88.53
    Address:        192.168.88.53#53

    Name:   www.example
    Address: 11.11.11.22
[root@client ~]# nslookup www.example 192.168.88.153
    Server:         192.168.88.153
    Address:        192.168.88.153#53

    Name:   www.example
    Address: 11.11.11.22

缓存DNS

缓存DNS:转发解析请求,向权威服务器获取DNS解析,并将结果缓存(重启失效),可以加快对同一域名的解析速度

# 缓存DNS服务器
权威DNS服务器(192.168.88.53):
[root@dns ~]# getenforce 
	Disabled
[root@dns ~]# rpm -q firewalld
	未安装软件包 firewalld 
[root@dns ~]# yum install -y bind bind-chroot
[root@dns ~]# cat -n /etc/named.conf
     1  options {
     2          directory       "/var/named";
     3          allow-transfer  { 192.168.88.153; };	// 允许传输文件
     4  };
     5  zone "example" IN {
     6          type    master;					
     7          file    "example.zone";
     8  };
[root@dns ~]# ll /var/named/named.localhost
	-rw-r----- 1 root named 194 116 23:27 /var/named/named.localhost
[root@dns ~]# cp -a /var/named/{named.localhost,example.zone}
[root@dns ~]# cat -n /var/named/example.zone
     1  $TTL 1D
     2  @       IN SOA  @ rname.invalid. (
     3                                  1949100101 		; serial	
     4                                          1D      ; refresh	
     5                                          1H      ; retry		
     6                                          1W      ; expire	
     7                                          3H )    ; minimum	
     8          NS      server			
     9	server  A       192.168.88.53
    10  www     A       11.11.11.11		
[root@dns ~]# systemctl enable named.service --now
[root@dns ~]# ss -ntulp | grep :53			# 返回DNS服务

缓存DNS服务器(192.168.88.253):
[root@dns3 ~]# getenforce 
	Disabled
[root@dns3 ~]# rpm -q firewalld
	未安装软件包 firewalld 
[root@dns3 ~]# yum install -y bind bind-chroot
[root@dns3 ~]# cat -n /etc/named.conf
     1  options {
     2          directory       "/var/named/";
	 3			forwarders		{ 192.168.88.53; };
     3  };
[root@dns3 ~]# systemctl enable named.service --now 
[root@dns3 ~]# ss -ntulp | grep :53			# 返回DNS服务	

# 验证
[root@client ~]# yum install -y bind-utils
[root@client ~]# nslookup www.example 192.168.88.253
    Server:         192.168.88.253
    Address:        192.168.88.253#53

	Non-authoritative answer:
    Name:   www.example
    Address: 11.11.11.11

end

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值