目录
DNS简介
DNS主要是用于将域名解析为IP地址的协议,有时候也用于将IP地址反向解析成域名,所以DNS可以实现双向解析。
DNS可以使用TCP和UDP的53端口,基本使用UDP协议的53端口。
本地名称解析配置文件:hosts
Linux: /etc/hosts
Windows: %WINDIR%/system32/drivers/etc/hosts
DNS域名结构
根域
一级域名:Top Level Domain: tld
com, edu, mil, gov, net, org, int,arpa
三类:组织域、国家域(.cn, .ca, .hk, .tw)、反向域
二级域名:csdn.com
三级域名:www.csdn.com
最多可达到127级域名
解析类型
FQDN –> IP 正向解析
IP –> FQDN 反向解析
注意:正反向解析是两个不同的名称空间,是两棵不同的解析树
资源记录
区域解析库:由众多RR组成:
资源记录: Resource Record, RR
记录类型: A, AAAA, PTR, SOA, NS, CNAME, MX
SOA: Start Of Authority,起始授权记录;一个区域解析库有且仅能有一个
SOA记录,必须位于解析库的第一条记录
A: internet Address,作用, FQDN --> IP
AAAA: FQDN --> IPv6
PTR: PoinTeR, IP --> FQDN
NS: Name Server,专用于标明当前区域的DNS服务器
CNAME : Canonical Name,别名记录
MX: Mail eXchanger,邮件交换器
TXT:对域名进行标识和说明的一种方式,一般做验证记录时会使用此项
一、实现主DNS服务器
- 安装Bind组件
yum list all bind*
bind:服务器
bind-libs:相关库
bind-utils: 客户端
bind-chroot: 安全包,将dns相关文件放至 /var/named/chroot/BIND主程序:/usr/sbin/named
服务脚本和Unit名称:/etc/rc.d/init.d/named,/usr/lib/systemd/system/named.service
主配置文件:/etc/named.conf
zone文件: /etc/named.rfc1912.zones
/etc/rndc.key
管理工具:/usr/sbin/rndc:remote name domain controller,默认与bind安装在同一主机,且只能通过127.0.0.1连接named进程,提供辅助性的管理功能;953/tcp
解析库文件:/var/named/ZONE_NAME.ZONE注意:
(1) 一台物理服务器可同时为多个区域提供解析
(2) 必须要有根区域文件;named.ca
(3) 应该有两个(如果包括ipv6的,应该更多)实现localhost和本地回环地址的解析库 - 在主配置文件中解除限制
vim /etc/named.conf // listen-on port 53 { 127.0.0.1; }; // allow-query { localhost; };
- 配置named域名文件
cp -p /etc/named.rfc1912.zones /etc/named.rfc1912.zones.bak vim /etc/named.rfc1912.zones zone "apep.com" IN { type master; file "apep.com.zone"; allow-update { none; }; };
- 创建zone相关文件
cp -p /var/named/named.localhost /var/named/apep.com.zone vim /var/named/apep.com.zone $TTL 1D @ IN SOA master.apep.com. admin.apep.com. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS master A 127.0.0.1 AAAA ::1 master A 172.25.0.100 @ MX 10 mailsrv ftp A 1.1.1.1 db A 2.2.2.2 mailsrv A 3.3.3.3 websrv A 172.25.0.101 websrv A 172.25.0.102 www CNAME websrv
- 检查配置文件
named-checkconf
- 检查域名和对应文件是否有误
named-checkzone apep.com /var/named/apep.com.zone zone apep.com/IN: loaded serial 0 OK
- 启动named服务
systemctl start named
- 修改客户端中DNS服务器地址,指向服务器地址
vim /etc/resolv.conf nameserver 172.25.0.100
- 使用dig命令验证
dig apep.com ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.15 <<>> apep.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43060 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;apep.com. IN A ;; ANSWER SECTION: apep.com. 86400 IN A 127.0.0.1 ;; AUTHORITY SECTION: apep.com. 86400 IN NS master.apep.com. ;; ADDITIONAL SECTION: master.apep.com. 86400 IN A 172.25.0.100 ;; Query time: 0 msec ;; SERVER: 172.25.0.100#53(172.25.0.100) ;; WHEN: Fri Jan 26 15:59:07 CST 2024 ;; MSG SIZE rcvd: 90
可以看到域名由服务器解析成I
二、实现DNS反向区域
1.配置文件中添加反向区域
vim /etc/named.rfc1912.zones
zone "0.25.172.in-addr.arpa" {
type master;
file "172.25.0.zone";
};
2./var/named目录下创建反向解析域 172.25.0.zone文件
vim /var/named/172.25.0.zone
$TTL 1D
@ IN SOA ns1.apep.com. admin (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1
ns1 A 172.25.0.100
100 PTR master.apep.com.
101 PTR websrv.apep.com.
102 PTR websrv.apep.com.
3.检查配置文件
named-checkconf
4.检查反向解析文件
named-checkzone 0.25.172.in-addr.arpa 172.25.0.zone
zone 0.25.172.in-addr.arpa/IN: loaded serial 0
OK
5.重新加载配置文件
rndc reload
6.验证
dig -x 172.25.0.100 @172.25.0.100 #指定DNS服务器反向解析IP地址
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.15 <<>> -x 172.25.0.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46099
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;100.0.25.172.in-addr.arpa. IN PTR
;; ANSWER SECTION:
100.0.25.172.in-addr.arpa. 86400 IN PTR master.apep.com.
;; AUTHORITY SECTION:
0.25.172.in-addr.arpa. 86400 IN NS ns1.0.25.172.in-addr.arpa.
;; ADDITIONAL SECTION:
ns1.0.25.172.in-addr.arpa. 86400 IN A 172.25.0.100
;; Query time: 0 msec
;; SERVER: 172.25.0.100#53(172.25.0.100)
;; WHEN: Fri Jan 26 16:14:26 CST 2024
;; MSG SIZE rcvd: 117
三、实现DNS从服务器
两台主机
一台主,一台从
1 在从节点编辑配置文件,注意slaves要写对,程序检查不出来(安装过程省略...)
相关文件会自动生成。
vim /etc/named.rfc1912.zones
zone "apep.com" {
type slave;
masters { 172.25.0.100; };
file "slaves/apep.com.zone.slave";
};
2 安全加固
在主服务器上主配置文件中添加允许的从服务器地址
vim /etc/named.conf
allow-transfer { 172.25.0.200; };
在主服务器域名文件中添加从服务器IP地址
vim /var/named/apep.com.zone
@ IN SOA ns1 admin ( 3 3H 10M 12H 1H )
NS ns1
NS ns2 <---
ns1 A 172.25.0.100
ns2 A 172.25.0.200 <---
重启named服务
rndc reload
在从服务器上不允许传递
vim /etc/named.conf
allow-transfer { none; };
重启named服务
rndc reload
验证反向解析
dig -x 172.25.0.100 @172.25.0.100
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.15 <<>> -x 172.25.0.100 @172.25.0.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26392
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;100.0.25.172.in-addr.arpa. IN PTR
;; ANSWER SECTION:
100.0.25.172.in-addr.arpa. 86400 IN PTR master.apep.com.
;; AUTHORITY SECTION:
0.25.172.in-addr.arpa. 86400 IN NS ns1.0.25.172.in-addr.arpa.
;; ADDITIONAL SECTION:
ns1.0.25.172.in-addr.arpa. 86400 IN A 172.25.0.100
;; Query time: 16 msec
;; SERVER: 172.25.0.100#53(172.25.0.100)
;; WHEN: Sun Jan 28 21:50:59 CST 2024
;; MSG SIZE rcvd: 117
四、实现子域委派
两台主机,一台父域,一台子域
1 在父域服务器禁用安全相关功能
vim /etc/named.conf
dnssec-enable no;
dnssec-validation no;
在zone文件中添加子DNS服务地址
vim /var/named/apep.com.zone
chengdu NS ns3
ns3 A 172.25.0.11
rndc reload
2 在子域服务器建立主区域chengdu.apep.com
vim /etc/named.conf
// listen-on port 53 { 127.0.0.1; };
// allow-query { localhost; };
vim /etc/named.rfc1912.zones
zone "chengdu.apep.com" IN {
type master;
file "chengdu.apep.com.zone";
};
cp -p named.localhost chengdu.apep.com.zone
vim chengdu.apep.com.zone
$TTL 1D
@ IN SOA ns1 admin (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
AAAA ::1
ns1 A 172.25.0.11
websrv A 172.25.0.13
www CNAME websrv
使用子域解析域名
dig -t a www.chengdu.apep.com @172.25.0.11
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.15 <<>> -t a www.chengdu.apep.com @172.25.0.11
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52036
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.chengdu.apep.com. IN A
;; ANSWER SECTION:
www.chengdu.apep.com. 86400 IN CNAME websrv.chengdu.apep.com.
websrv.chengdu.apep.com. 86400 IN A 172.25.0.13
;; AUTHORITY SECTION:
chengdu.apep.com. 86400 IN NS chengdu.apep.com.
;; ADDITIONAL SECTION:
chengdu.apep.com. 86400 IN A 127.0.0.1
chengdu.apep.com. 86400 IN AAAA ::1
;; Query time: 1 msec
;; SERVER: 172.25.0.11#53(172.25.0.11)
;; WHEN: Mon Jan 29 11:35:50 CST 2024
;; MSG SIZE rcvd: 144
父域解析子域名(rndc flush刷新缓存)
dig -t a www.chengdu.apep.com @172.25.0.100
; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7 <<>> -t a www.chengdu.apep.com @172.25.0.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38698
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.chengdu.apep.com. IN A
;; ANSWER SECTION:
www.chengdu.apep.com. 86370 IN CNAME websrv.chengdu.apep.com.
websrv.chengdu.apep.com. 86370 IN A 172.25.0.13
;; AUTHORITY SECTION:
chengdu.apep.com. 86370 IN NS chengdu.apep.com.
;; ADDITIONAL SECTION:
chengdu.apep.com. 86370 IN A 127.0.0.1
chengdu.apep.com. 86370 IN AAAA ::1
;; Query time: 1 msec
;; SERVER: 172.25.0.100#53(172.25.0.100)
;; WHEN: Wed May 10 07:21:31 EDT 2023
;; MSG SIZE rcvd: 144
五、实现DNS forward 功能
三台主机,主DNS服务器,中间DNS缓存服务器,客户端
全局转发:
对非本机所负责解析区域的请求,全转发给指定的服务器
在全局配置块中实现:
Options {
forward first|only;
forwarders { ip;};
};
特定区域转发
仅转发对特定的区域的请求,比全局转发优先级高
zone "ZONE_NAME" IN {
type forward;
forward first|only;
forwarders { ip;};
};
first:先转发至指定DNS服务器,如果无法解析查询请求,则本服务器再去根服务器查询
only: 先转发至指定DNS服务器,如果无法解析查询请求,则本服务器将不再去根服务器查询
1 在中间转发服务器上修改配置
vim /etc/named.conf
forward first|only;
forwarders { 172.25.0.100;};
dnssec-enable no;
dnssec-validation no;
在客户端上指向中间DNS缓存服务器解析域名
dig -t a apep.com @172.25.0.220
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.15 <<>> -t a apep.com @172.25.0.220
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45759
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;apep.com. IN A
;; AUTHORITY SECTION:
apep.com. 10788 IN SOA master.apep.com. admin.apep.com. 20240130 86400 3600 604800 10800
;; Query time: 0 msec
;; SERVER: 172.25.0.220#53(172.25.0.220)
;; WHEN: Sat Jan 20 02:00:11 CST 2024
;; MSG SIZE rcvd: 86
完)