dns服务器部署
一、关于dns的名词解释
dns:
domain name service(域名解析服务)
#关于客户端:#(172.25.254.201)
/etc/resolv.conf ##dns指向文件
nameserver 172.25.254.101
#测试:
host www.baidu.com ##地址解析命令
dig www.baidu.com ##地址详细解析信息命令
A记录 ##ip地址叫做域名的Address 记录
SOA ##授权起始主机
dns顶级
. 13
次级
.com .net .edu .org ....
baidu.com
#关于服务端#(172.25.254.101)
bind ##安装包
named ##服务名称
/etc/named.conf ##主配置文件
/var/named ##数据目录
端口 ##53
关于报错信息:
1.no servers could be reached ##服务无法访问(服务开启?火墙?网络?端口?)
2.服务启动失败 ##配置文件写错 journalctl -xe查询错误
3.dig 查询状态
NOERROR | ##表示查询成功 |
REFUSED | ##服务拒绝访问 |
SERVFAIL | ##查询记录失败,(dns服务器无法到达上级,拒绝缓存) |
NXDOMAIN | ##此域名A记录在dns中不存在 |
二、dns服务的安装与启用
#安装#
dnf install bind.x86_64 -y
#启用#
systemctl enable --now named
firewall-cmd --permanent --add-service=dns
firewall-cmd --reload
vim /etc/named.conf
11 listen-on port 53 { any; }; ##在本地所有网络接口上开启53端口
19 allow-query { any; }; ##允许查询A记录的客户端列表
34 dnssec-validation no; ##禁用dns检测使dns能够缓存外部信息到本机
systemctl restart named
netstat -antlupe | grep named 查询端口
三、高速缓存dns
作用:在企业中的直连网络下,每台主机都去向外网获取dns解析,会比较慢,可以设置内网的一台能上网的主机作为dns服务器,给直连的主机提供dns解析服务。
20 forwarders { 114.114.114.114; };
四、dns的正向解析 (做此实验的时候将刚才高速缓存还原)
vim /etc/named.rfc1912.zone
(为了出错之后好排错所以此时复制一份以下内容进行编写)
zone "westos.com" IN { ##维护的域名
type master; ##当前服务器位主dns
file "westos.com.zone"; ##域名A记录文件
allow-update { none; }; ##允许更新主机列表
};
cd /var/named/
cp -p named.localhost westos.com.zone
$TTL 1D #TIME-TO-LIVE(dns地址保存时间长度)
@ IN SOA dns.westos.com. root.westos.com. ( #SOA授权起始(Start of Authority)
0 ; serial #域名版本序列号
1D ; refresh #刷新时间(辅助dns)
1H ; retry #重试时间(辅助dns)
1W ; expire #过期时间(辅助dns,查询失败过期停止对辅助域名的应答)
3H ) ; minimum #A记录最短有效期
NS dns.westos.com.
dns A 172.25.254.101
bbs A 172.25.254.111
www CNAME lee1.westos.com. ##规范域名
lee1 A 172.25.254.111 ##正向解析记录
lee1 A 172.25.254..222
westos.com. MX 1 172.25.254.101. ##邮件解析记录
systemctl restart named
dig www.westos.com #查询正向解析
dns 的邮件解析
dnf install mailx postfix -y
systemctl start postfix
dig -t mx westos.com
五、dns的反向解析
vim /etc/named.rfc1912.zones
注意: 同样为了好排错,所以此部分内容也是复制之后在编辑
zone "254.25.172.in-addr.arpa" IN {+----------------+
type master;
file "172.25.254.ptr";
allow-update { none; };
};
cd /var/named/
cp -p named.loopback 172.25.254.ptr
vim 172.25.254.ptr
$TTL 1D
@ IN SOA dns.westos.com. root.westos.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.101
11 PTR www.westos.com.
12 PTR bbs.westos.com.
13 PTR news.westos.com.
测试:
systemctl restart named
dig -x 172.25.254.111
六、dns的双向解析
实验环境:
客户端2台
1.1.1网段
172.25.254网段 ##ifconfig enp1s0 172.25.254.201 netmask 255.255.255.0
服务端1台2个网段的ip
1.1.1.101
172.25.254.101 ##ifconfig enp1s0 172.25.254.101 netmask 255.255.255.0
在1.1.1网段的客户主机中
vim /etc/resolv.conf
nameserver 172.25.254.101
在172.25.254网段的客户主机中
vim /etc/resolv.conf
nameserver 172.25.254.101
配置方式:
cd /var/named/
cp -p westos.com.zone westos.com.inter
vim westos.com.inter
$TTL 1D
@ IN SOA westos.com. root.westos.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 1.1.1.101
bbs A 1.1.1.111
www CNAME lee1.westos.com.
lee1 A 1.1.1.111
lee1 A 1.1.1.222
westos.com. MX 1 1.1.1.101. #mail exchanger
cp -p /etc/named.rfc1912.zones /etc/named.rfc1912.zones.inters
vim /etc/named.rfc1912.inters
zone "westos.com" IN {
type master;
file "westos.com.inter";
allow-update { none; };
};
vim /etc/named.conf
systemctl restart named
/*
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
*/
view localnet {
match-clients { 172.25.254.0/24; };
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
};
view internet {
match-clients { any; };
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.inters";
};
include "/etc/named.root.key";
测试:
分别在2个网段的主机中作同样域名的地址解析
得到的A记录不同
七、dns集群
###主dns:####
vim /etc/named.rfc1912.zones
zone "westos.com" IN {
type master;
file "westos.com.zone";
allow-update { none; };
also-notify { 172.25.254.201; }; ##主动通知的辅助dns主机
};
vim /var/named/westos.com.zone
$TTL 1D
@ IN SOA westos.com. root.westos.com. (
2020112201 ; serial ##每次修改A记录文件需要变更此参数的值
1D ; refresh ##
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.101
bbs A 172.25.254.111
www CNAME lee1.westos.com.
lee1 A 172.25.254.111
lee1 A 172.25.254.222
westos.com. MX 1 172.25.254.101.
###slave dns####:
dnf install bind -y
firewall-cmd --add-service=dns
vim /etc/named.conf
vim /etc/resolv.conf
172.25.254.201 (改成自己的dns)
listen-on port 53 { any; };
allow-query { any; };
dnssec-validation no;
vim /etc/named.rfc1912.zone
systemctl restart named
注意:为了好排错,同样是复制之后再进行编辑
zone "westos.com" IN {
type slave; ##dns状态位辅助dns
masters { 172.25.254.101; }; ##主dns
file "slaves/westos.com.zone"; ##同步数据文件
};
验证:
八、dns的更新
dns基于ip地址的更新:
在dns中设定:
vim /etc/named.rfc1912.zones
zone "westos.com" IN {
type master;
file "westos.com.zone";
allow-update { 172.25.254.201; }; ##允许指定客户端更新westos域
also-notify { 172.25.254.201; };
};
测试:
在172.25.254.201
[root@node2 ~]# nsupdate
> server 172.25.254.101
> update add hello.westos.com 86400 A 172.25.254.111 ##新曾A记录
> send
> update delete hello.westos.com ##删除A记录
> send
测试:
dns基于key更新的方式:
[root@node1 mnt]# dnssec-keygen -a HMAC-SHA256 -b 128 -n HOST westos
Kwestos.+163+11625
[root@node1 mnt]# ls
Kwestos.+163+11625.key Kwestos.+163+11625.private
[root@node1 mnt]# cp -p /etc/rndc.key /etc/westos.key
[root@node1 mnt]# cat Kwestos.+163+11625.key
westos. IN KEY 512 3 163 do5PjldBXK6WIohfhtIIZQ==
[root@node1 mnt]# vim /etc/westos.key
key "westos" {
algorithm hmac-sha256;
secret "do5PjldBXK6WIohfhtIIZQ==";
};
将刚才生成的公钥和私钥传给测试的客户机
[root@node1 mnt]# scp Kwestos.+163+11667.* root@172.25.254.201:/mnt
The authenticity of host '172.25.254.201 (172.25.254.201)' can't be established.
ECDSA key fingerprint is SHA256:Z7nIjVS0zBFK8xGDwjAegodMOk0lyUIF0+GBN13Mrv0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.201' (ECDSA) to the list of known hosts.
root@172.25.254.201's password:
Kwestos.+163+11667.key 100% 50 70.8KB/s 00:00
Kwestos.+163+11667.private 100% 168 259.2KB/s 00:00
vim /etc/named.conf
44 include "/etc/wesots.key";
vim /etc/named.rfc1912.zones
systemctl restart named
zone "westos.com" IN {
type master;
file "westos.com.zone";
allow-update { key westos; };
also-notify { 172.25.254.201; };
};
验证:
nsupdate -k /mnt/Kwestos.+163+26695.private
> server 172.25.254.101
> update add hello.westos.com 86400 A 192.168.0.111
> send
> quit
九、ddns(dhcp+dns)
DDNS是动态域名服务的缩写,是指域名系统中的一种自动更新名称服务器内容的技术,DDNS是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序就会通过信息传递把该主机的动态ip地址传送给位于服务上主机上的服务器程序,服务器程序负责提供DNS服务并实现动态域名解析。
主机名固定,IP不固定
解析www,域名对应的IP是死的,不适用于动态网络,
如何让解析随IP变:
因为dhcp每次分配的IP都不同
IP dhcp服务知道是哪个IP
分配IP的时候告诉dns,把解析指向他
本实验的环境是基于上步实验(key 更新)
服务端主机:
dnf instsall dhcp-server -y
cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
vim /etc/dhcpd/dhcpd.conf
systemctl restart dhcpd
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "westos.com";
option domain-name-servers 172.25.254.101;
default-lease-time 600;
max-lease-time 7200;
# Use this to enble / disable dynamic dns updates globally.
ddns-update-style interim;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
# This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 {
range 172.25.254.46 172.25.254.50;
option routers 172.25.254.101;
}
key westos {
algorithm hmac-sha256;
secret B/16D8XGtviAKrYPB9zanw==;
};
zone westos.com. {
primary 172.25.254.101;
key westos;
}
这部分内容可以通过man 5 dhcp.conf 查看
vim /var/named/westos.com.zone
systemctl restart named
$TTL 1D
@ IN SOA westos.com. root.westos.com. (
2020112303 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.101
客户端测试主机:
设定测试主机网络工作方式为dhcp
设定主机名称jjj.westos.com
重启网络
nmcli connection reload
nmcli connection up enp1s0
设置客户端 主机名:
hostnamectl set-hostname jjj.westos.com
nmcli connection reload
nmcli connection up enp1s0
dig jjj.westos.com
测试:
dig jjj.westos.com
可以得到正确解析
为了保证实验的准确性:进行二次测试
修改dhcp的地址池,使得客户端ip改变
服务端:
vim /etc/dhcp/dhcpd.conf
systemctl restart dhcpd
客户端:
hostname set-hostname yyy.westos.com 设置客户端主机名
nmcli connection reload 重启网络
nmcli connection up enp1s0
dig yyy.westos.com
ddsn实验总结:
(1)本实验基于key 更新实验环境;
(2)为保证dhcp服务有效,要关掉其他的dhcp服务;
(3)编辑完配置文件一定要重启服务!!!
(4)服务端和客户端火墙都要关闭 或者将服务添加到火墙里;
(5)两边的selinux也都要关闭。