域名系统(英文:Domain Name System,缩写:DNS)是互联网的一项服务。它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。
虽然因特网上的节点都可以用IP地址唯一标识,并且可以通过IP地址被访问,但即使是将32位的二进制IP地址写成4个0~255的十位数形式,也依然太长、太难记。因此,人们发明了域名(Domain Name),域名可将一个IP地址关联到一组有意义的字符上去。用户访问一个网站的时候,既可以输入该网站的IP地址,也可以输入其域名,对访问而言,两者是等价的。例如:微软公司的Web服务器的IP地址是207.46.230.229,其对应的域名是www.microsoft.com,不管用户在浏览器中输入的是207.46.230.229还是www.microsoft.com,都可以访问其Web网站。
实验环境配置
配置双网卡主机westosa
两个ip 分别为172.25.254.100 、172.25.26.100
开启真实主机防火墙的地址伪装功能,将真实主机变为路由器(真实主机联网),使得虚拟主机westosa(双网卡:172.25.254.100 和 172.25.26.100)、westosb(172.25.254.200)能够连接外网
firewall-cmd --permanent --add-masquerade ##不用reload
firewall-cmd --list-all
为westosa、westosb设置网关为真实主机ip,此时westosa、westosb可以ping通www.baidu.com
[root@westoslinux ~]# cd /etc/sysconfig/network-scripts/
[root@westoslinux network-scripts]# ls
ifcfg-westos
[root@westoslinux network-scripts]# vim ifcfg-westos ##编写网卡配置文件
//
TYPE=Ethernet
BOOTPROTO=none
IPADDR=172.25.254.100
PREFIX=24
NAME=westos
DEVICE=ens3
ONBOOT=yes
GATEWAY=172.25.254.77 ##添加网关
//
route -n ##查询网络,网关设置完成
[root@westoslinux ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.77 0.0.0.0 UG 100 0 0 ens3
172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 ens3
**将westosa作为客户端主机进行域名解析测试,编辑westosa的dns指向文件/etc/resolv.conf,在其中设定向其提供域名解析服务的服务器192.168.43.169,与真实机的nameserver一致。
vim /etc/resolv.conf
//编写内容
search westos.org
nameserver 192.168.43.169
//
host www.qq.com对www.qq.com进行地址解析得到对应IP, dig www.qq.com得到域名www.qq.com的地址详细解析信息,NOERROR表示解析成功
[root@westoslinux ~]# host www.qq.com
www.qq.com is an alias for ins-r23tsuuf.ias.tencent-cloud.net.
ins-r23tsuuf.ias.tencent-cloud.net has address 183.194.238.117
ins-r23tsuuf.ias.tencent-cloud.net has address 183.194.238.19
ins-r23tsuuf.ias.tencent-cloud.net has IPv6 address 2402:4e00:1430:1301:0:9227:79cc:76f2
ins-r23tsuuf.ias.tencent-cloud.net has IPv6 address 2402:4e00:1430:1301:0:9227:79d3:ffd1
[root@westoslinux ~]# dig www.qq.com
; <<>> DiG 9.11.13-RedHat-9.11.13-3.el8 <<>> www.qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55114
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.qq.com. IN A
;; ANSWER SECTION:
www.qq.com. 172 IN CNAME ins-r23tsuuf.ias.tencent-cloud.net.
ins-r23tsuuf.ias.tencent-cloud.net. 38 IN A 183.194.238.19
ins-r23tsuuf.ias.tencent-cloud.net. 38 IN A 183.194.238.117
;; Query time: 39 msec
;; SERVER: 192.168.43.169#53(192.168.43.169)
;; WHEN: Thu Aug 12 08:48:16 CST 2021
;; MSG SIZE rcvd: 119
服务器端名词解释
服务器端名词 解释
bind dns服务安装包
named dns服务名称
/etc/named.conf dns服务主配置文件
/var/named 数据目录
53 默认服务端口
/etc/resolv.conf dns指向文件
安装启用dns服务
在双网卡虚拟主机westosa中安装dns服务软件包bind,安装完成后启动dns服务named,在防火墙中添加dns服务,刷新火墙使设定生效
dnf install bind -y 下载dns服务
systemctl enable --now named ##启动dns服务named
firewall-cmd --permanent --add-service=dns ##在防火墙中添加dns服务
firewall-cmd --reload ##刷新火墙使设定生效
编辑修改dns服务的配置文件 /etc/named.conf,内容如下:
options {
11 listen-on port 53 { any; }; ##在本地所有网络接口上开启53端口
19 allow-query { any; }; ##允许查询A记录的客户端列表
20 forwarders { 114.114.114.114; }; ##使用国内域名解析服务器源114.114.114.114进行数据缓存(而不是根dns服务器,节省时间)
34 dnssec-validation no; ##禁用dns合法检测功能使dns服务器能够缓存外部信息到本机
编写完成后重启dns服务named
systemctl restart named
dns正向解析
编辑dns服务的配置文件
vim /etc/named.conf
//编辑内容
# forwarders { 114.114.114.114; }; ##注释掉
//
编辑记录维护域名的语句块的文件
vim /etc/named.rfc1912.zones
//编辑内容
29 zone "westos.org" IN { ##编写要维护的域名,设置当前服务器类型为主dns
30 type master;
31 file "westos.org.zone"; ##指定进行解析动作时读取的域名A记录文件
32 allow-update { none; }; ##允许更新主机列表
33 };
//
在dns服务的数据目录/var/named下,复制域名A记录文件模板
33 cd /var/named/
35 cp -p named.localhost westos.org.zone
编辑复制的域名A记录文件westos.org.zone
37 vim /var/named/westos.org.zone
//
$TTL 1D
@ IN SOA dns.westos.org. root.westos.org. ( ##修改@符号的值等于文件中书写的维护域名
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.org.
dns A 172.25.254.100 ##添加正向解析A记录
www A 172.25.254.200
www A 172.25.254.77
//
此时可以dig书写的域名,当同一个域名对应两个ip时,dig时两个ip会轮换
[root@westosb named]# dig www.westos.org
; <<>> DiG 9.11.13-RedHat-9.11.13-3.el8 <<>> www.westos.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12315
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: bf312063896e2484b41d08c0611484b485ff28d9836a33d8 (good)
;; QUESTION SECTION:
;www.westos.org. IN A
;; ANSWER SECTION:
www.westos.org. 86400 IN A 172.25.254.77 ##这样轮换,别误会 ^-^
www.westos.org. 86400 IN A 172.25.254.200
;; AUTHORITY SECTION:
westos.org. 86400 IN NS dns.westos.org.
;; ADDITIONAL SECTION:
dns.westos.org. 86400 IN A 172.25.254.100
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Aug 12 10:17:24 CST 2021
;; MSG SIZE rcvd: 137
[root@westosb named]# dig www.westos.org
; <<>> DiG 9.11.13-RedHat-9.11.13-3.el8 <<>> www.westos.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47674
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: f56768c1f8a841a9f87f2373611484f7ed1c4a9affd41529 (good)
;; QUESTION SECTION:
;www.westos.org. IN A
;; ANSWER SECTION:
www.westos.org. 86400 IN A 172.25.254.200 ##这样轮换,别误会 ^-^
www.westos.org. 86400 IN A 172.25.254.77
;; AUTHORITY SECTION:
westos.org. 86400 IN NS dns.westos.org.
;; ADDITIONAL SECTION:
dns.westos.org. 86400 IN A 172.25.254.100
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Aug 12 10:18:31 CST 2021
;; MSG SIZE rcvd: 137
dns规范域名转换CNAME
编辑dns服务器westosa的域名A记录文件
vim /var/named/westos.org.zone
//
NS dns.westos.org.
dns A 172.25.254.100
www CNAME www.a.westos.org. ####添加规范域名转换及对应的正向解析A记录
www.a A 172.25.254.200
www.a A 172.25.254.77
//
重启dns服务named
systemctl restart named
此时在westosb中dig www.westos.org,dns服务器westosa会先对www.westos.org进行规范域名转换
[root@westosb ~]# vim /etc/resolv.conf
//修改DNS服务主机ip为DNS主服务机172.25.254.100的ip
nameserver 172.25.254.100
//
[root@westosb ~]# dig www.westos.org
; <<>> DiG 9.11.13-RedHat-9.11.13-3.el8 <<>> www.westos.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40358
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: dbde288f30205c9f0ac8706a61149bcc96437ead5e207ed3 (good)
;; QUESTION SECTION:
;www.westos.org. IN A
;; ANSWER SECTION:
www.westos.org. 86400 IN CNAME www.a.westos.org.
www.a.westos.org. 86400 IN A 172.25.254.200
www.a.westos.org. 86400 IN A 172.25.254.77
;; AUTHORITY SECTION:
westos.org. 86400 IN NS dns.westos.org.
;; ADDITIONAL SECTION:
dns.westos.org. 86400 IN A 172.25.254.100
;; Query time: 0 msec
;; SERVER: 172.25.254.100#53(172.25.254.100)
;; WHEN: Thu Aug 12 11:55:56 CST 2021
;; MSG SIZE rcvd: 157
dns邮件记录的正向解析
在邮件发westosb送方中安装postfix、mailx
dnf install postfix mailx -y
开启postfix服务
systemctl start postfix.service
编辑dns服务器westosa的域名A记录文件,添加邮件解析记录MX(1表示优先级为1)
vim /var/named/westos.org.zone
//添加内容
westos.org. MX 1 172.25.254.100.
//
重启dns服务named
systemctl restart named
此时可以在westosb中发送邮件给westos.org并查看邮件的日志文件
[root@westosb ~]# mail root@westos.org
Subject: dtd
wrgah
.
EOT
You have mail in /var/spool/mail/root
[root@westosb ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
EC75295995 430 Thu Aug 12 12:14:11 root@westosb.westos.org
(connect to 172.25.254.100[172.25.254.100]:25: No route to host)
root@westos.org
-- 0 Kbytes in 1 Request.
dns反向解析
dns反向解析ptr与正向解析是完全独立的,在dns服务器westosa中编写/etc/named.rfc1912.zones指定反向解析ptr的维护网段和反向解析记录文件
vim /etc/named.rfc1912.zones
//编写内容
53 zone "254.25.172.in-addr.arpa" IN {
54 type master;
55 file "172.25.254.ptr";
56 allow-update { none; };
57 };
//
重启dns服务named
chibohandong