Day11
背景
我们可以通过ip访问web主机上发布的web服务,即http://192.168.2.22访问其他主机上发布的80端口的服务 。ip本身无意义,需要有一个有语义的符号去标ip,方便记忆和 使用 ,于是dns出现了。
当客户端要访问其他主机提供的服务,首先获取该主机的域名,然后将这 个域名交给dns服务器解析为ip,然后拿到dns服务器返回的ip,根据ip访问 目标主机 ,最终访问主机,我们使用唯一标识,就是ip地址 。多了一个第三方,的确是减低了效率,但是网络环境更加的人性化,也让开发者更加模块haul的开发网络环境,因为域名是有语义。
反向解析 (将ip解析为域名将ip解析为域名)
(把192168.2.23逆向解析为www.tangpin.huajuan)
VMware和centos7的一些问题
A、/etc/resolv.conf文件在重启系统或者重启network服务后重置,或者 vmware关闭或者挂起之后要重新设置。
B、centos在网络配置时候,明明设置了静态ip,但是无法生效,无法修改, 原因是图形化界面NetworkManager没有关闭,我们在mini安装中不存在 以上问题 ip a 显示所有的网卡信息 查看ens33网卡,有两个ip,networkmanage服务没有关,停用 networkmanage,会优先随机ip systemctl stop NetworkManager systemctl disable NetworkManager。
环境准备
[root@client01 ~]# systemctl status NetworkManager # 优先生成ip地址,会覆盖使用设置的静态ip
[root@client01 ~]# systemctl stop NetworkManager # 停用
[root@client01 ~]# systemctl disable NetworkManager
机器:web服务器(192.168.2.22) 发布部署 web 服务
DNS服务器(192.168.2.23) 用于解析域名和ip地址
client01(192.168.2.24) 用于模拟客户机
1、安装bind-utils.x86_64
[root@client01 ~]# yum -y install bind-utils.x86_64
[root@client01 ~]# cat /etc/resolv.conf
nameserver 192.168.2.23
[root@client01 ~]# curl www.tangpin.huajuan
i am web-server marjor is static file manager
[root@client01 ~]# nslookup www.tangpin.huajuan # 正向
Server: 192.168.2.23
Address: 192.168.2.23#53
Name: www.tangpin.huajuan
Address: 192.168.2.22
2、修改zone文件控制域名和ip之间的转换
[root@DNS named]# vim /etc/named.rfc1912.zones
# 行尾增加:
zone "2.168.192.in-addr.arpa" IN { # 只写网段
type master;
file "192.168.2.zone";
allow-update { none; };
};
[root@DNS named]# ls
data named.empty slaves
dynamic named.localhost tangpin.huajuan.zone
named.ca named.loopback
3、创建zone文件
[root@DNS named]# cp -p named.loopback 192.168.2.zone
# [root@DNS named]#chgrp named 192.168.2.zone
[root@DNS named]# ls -l
总用量 24
-rw-r-----. 1 root named 168 12月 15 2009 192.168.2.zone
drwxrwx---. 2 named named 23 7月 23 15:53 data
drwxrwx---. 2 named named 60 7月 24 09:47 dynamic
-rw-r-----. 1 root named 2253 4月 5 2018 named.ca
-rw-r-----. 1 root named 152 12月 15 2009 named.empty
-rw-r-----. 1 root named 152 6月 21 2007 named.localhost
-rw-r-----. 1 root named 168 12月 15 2009 named.loopback
drwxrwx---. 2 named named 6 6月 11 22:40 slaves
-rw-r-----. 1 root named 181 7月 23 16:43 tangpin.huajuan.zone
[root@DNS named]# vim 192.168.2.zone
imum
NS @
A 127.0.0.1
AAAA ::1
PTR localhost.
22 PTR www.tangpin.huajuan # PTR ip转域名
# web的ip
[root@DNS named]# named-checkconf /etc/named.rfc1912.zones
[root@DNS named]# named-checkzone 192.168.2.zone 192.168.2.zone
zone 192.168.2.zone/IN: loaded serial 0
OK
[root@DNS named]# systemctl restart named
[root@client01 ~]# nslookup 192.168.2.22 # 反向
Server: 192.168.2.23
Address: 192.168.2.23#53
22.2.168.192.in-addr.arpa name =
www.tangpin.huajuan.2.168.192.in-addr.arpa.
[root@client01 ~]# systemctl restart network # 重置网络
[root@client01 ~]# nslookup www.baidu.com
Server: 114.114.114.114
Address: 114.114.114.114#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 180.101.50.242
Name: www.a.shifen.com
Address: 180.101.50.188
[root@client01 ~]# cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 114.114.114.114
[root@client01 ~]# nslookup 180.101.50.188
Server: 114.114.114.114
Address: 114.114.114.114#53
** server can't find 188.50.101.180.in-addr.arpa.: NXDOMAIN
[root@client01 ~]# nslookup 192.168.2.22 # 此时解析不到
;; connection timed out; no servers could be reached
[root@client01 ~]# echo "nameserver 192.168.2.23" > /etc/resolv.conf
[root@client01 ~]# nslookup 192.168.2.22
Server: 192.168.2.23
Address: 192.168.2.23#53
22.2.168.192.in-addr.arpa name = www.tangpin.huajuan.2.168.192.in-addr.arpa.
多域DNS服务器的搭建
说明:搭建DNS服务器,实现同时解析多个域名,即bbs.tangpin.ks co.tangpin.bc
1、修改zone文件控制域名和ip之间的转换
[root@DNS named]# vim /etc/named.rfc1912.zones
zone "tangpin.ks" IN {
type master;
file "tangpin.ks.zone";
allow-update { none; };
};
zone "tangpin.bc" IN {
type master;
file "tangpin.bc.zone";
allow-update { none; };
};
2、创建zone文件
[root@DNS ~]# cd /var/named/
[root@DNS named]# pwd
/var/named
[root@DNS named]# ls
192.168.2.zone named.localhost
data named.loopback
dynamic slaves
named.ca tangpin.huajuan.zone
named.empty
[root@DNS named]# cp -p named.localhost tangpin.ks.zone
[root@DNS named]# vim tangpin.ks.zone
imum
NS @
A 127.0.0.1
AAAA ::1
bbs A 192.168.2.22
[root@DNS named]# cp -p named.localhost tangpin.bc.zone
[root@DNS named]# vim tangpin.bc.zone
imum
NS @
A 127.0.0.1
AAAA ::1
co A 192.168.2.22
3、检查配置并重启服务
[root@DNS named]# named-checkconf /etc/named.conf
[root@DNS named]# named-checkzone tangpin.ks.zone tangpin.ks.zone
zone tangpin.ks.zone/IN: loaded serial 0
OK
[root@DNS named]# named-checkzone tangpin.bc.zone tangpin.bc.zone
zone tangpin.bc.zone/IN: loaded serial 0
OK
[root@DNS named]#systemctl restart named
4、客户端测试
[root@client01 ~]# cat /etc/resolv.conf
nameserver 192.168.2.23
[root@client01 ~]# nslookup bbs.tangpin.ks
Server: 192.168.2.23
Address: 192.168.2.23#53
Name: bbs.tangpin.ks
Address: 192.168.2.22
[root@client01 ~]# nslookup co.tangpin.bc
Server: 192.168.2.23
Address: 192.168.2.23#53
Name: co.tangpin.bc
Address: 192.168.2.22
DNS主从配置
一、基础要求:
1、master和slave的系统时间保持一致
2、slave服务器上安装相应的软件(系统版本,软件版本)保持一致
3、根据需求修改相应的配置文件 master和slave都应修改
4、主从同步的核心是slave同步master上的区域文件
二、具体配置:
环境说明:确保集群时间服务器主机的时间同步
# 不可能要求每一台主机都访问一次(cn.ntp.org.cn),这样访问流量大,效率低
# 解决:只放一台主机做内网的ntp服务,这台主机定时访问外网,集群中内网
的机器访问这台ntp服务器即可,减少访问外网来同步时间。
机器:主DNS(192.168.2.23)
从DNS(192.168.2.26)
ntp-server(192.168.2.25)
client01(192.168.2.24)
[root@client01 ~]# date -s "2009-7-20 12:34:56"
2009年 07月 20日 星期一 12:34:56 CST
[root@client01 ~]# date
2009年 07月 20日 星期一 12:35:03 CST
[root@client01 ~]# netdate
-bash: netdate: 未找到命令
[root@client01 ~]# yum search ntpdate
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
=============== N/S matched: ntpdate ===============
ntpdate.x86_64 : Utility to set the date and time
: via NTP
1、配置ntp服务
# client01主机上下载ntpdate
[root@client01 ~]# yum -y install ntpdate.x86_64
[root@client01 ~]# ntpdate cn.ntp.org.cn # 同步时间
[root@client01 ~]# date
2024年 07月 24日 星期三 14:28:49 CST
# ntp-server服务器
[root@ntp-server ~]# systemctl stop firewalld.service
[root@ntp-server ~]# setenforce 0
[root@ntp-server ~]# vim /etc/selinux/config
[root@ntp-server ~]# systemctl disable firewalld.service
[root@ntp-server ~]# systemctl stop NetworkManager
[root@ntp-server ~]# systemctl disable NetworkManager
[root@ntp-server ~]# systemctl restart network
[root@ntp-server ~]# yum search ntp
已加载插件:fastestmirror
Repository 'samba': Error parsing config: Error parsing "baseurl = '/root/soft'": URL must be http, ftp, file or https not ""
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
================= N/S matched: ntp =================
ntp.x86_64 : The NTP daemon and utilities
ntp-doc.noarch : NTP documentation
ntp-perl.noarch : NTP utilities written in Perl
ntpdate.x86_64 : Utility to set the date and time
: via NTP
[root@ntp-server ~]# yum -y install ntp
[root@ntp-server ~]# vim /etc/ntp.conf
# 15 restrict 192.168.2.0 mask 255.255.255.0 # 共享时间(允许其他主机进行访
问)
[root@ntp-server ~]# ntpdate cn.ntp.org.cn
24 Jul 15:06:14 ntpdate[2095]: adjust time server 203.107.6.88 offset 0.016204 sec
[root@ntp-server ~]# systemctl start ntpd
[root@ntp-server ~]# which ntpdate
/usr/sbin/ntpdate
[root@ntp-server ~]# crontab -e # 作服务器一般需要指定ip,设置计划任务
* 4 * * * /usr/sbin/ntpdate cn.ntp.org.cn
# 客户端测试
[root@client01 ~]# ntpdate 192.168.2.25
24 Jul 15:10:55 ntpdate[4776]: adjust time server 192.168.2.25 offset 0.017569 sec
# 再次修改客户端时间(即不需要每次访问外网同步时间)
[root@client01 ~]# date -s "2009-7-30 12:34:56"
2009年 07月 30日 星期四 12:34:56 CST
[root@client01 ~]# date
2009年 07月 30日 星期四 12:34:58 CST
[root@client01 ~]# ntpdate 192.168.2.25
24 Jul 15:12:46 ntpdate[4781]: step time server 192.168.2.25 offset 472876643.736138 sec
2、主从架构的配置
(1)主DNS
# 主DNS同步时间
[root@DNS ~]# yum -y install ntpdate
[root@DNS ~]# ntpdate 192.168.2.25
24 Jul 15:37:37 ntpdate[3169]: adjust time server 192.168.2.25 offset 0.004380 sec
# 修改主配置文件,允许其他主机下载同步资源
options {
listen-on port 53 { 127.0.0.1;any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
# 添加行(ntp-server的ip地址)
allow-transfer {192.168.2.25;};
[root@DNS ~]# named-checkconf /etc/named.conf
[root@DNS ~]# systemctl restart named
(2)从服务器
# 从服务器同步时间
[root@slave ~]# ntpdate 192.168.2.25
24 Jul 15:36:02 ntpdate[2873]: adjust time server 192.168.2.25 offset 0.006140 sec
[root@slave ~]# yum -y install bind # 安装bind
[root@slave ~]# yum list installed|grep bind
# 修改主配置文件,确保允许访问
[root@slave ~]# vim /etc/named.conf
listen-on port 53 { 127.0.0.1;any; };
allow-query { localhost;any; };
# 修改区域文件
[root@slave ~]# vim /etc/named.rfc1912.zones
zone "tangpin.huajuan" IN {
45 type slave;
46 file "slaves/tangpin.huajuan.zone"; # 同步主DNS中的
/etc/var/named下的slaves文件,一旦主DNS出现问题及时接替任务
47 masters {192.168.2.23;};
48 };
[root@slave ~]# named-checkconf /etc/named.rfc1912.zones
[root@slave ~]# systemctl restart named
[root@slave ~]# ls /var/named/
data named.ca named.localhost slaves
dynamic named.empty named.loopback
[root@slave ~]# ls -ld /var/named/slaves/ # 查看同步过来的文件
drwxrwx---. 2 named named 6 6月 11 22:40 /var/named/slaves/
(3)客户端测试
[root@client01 ~]# cat /etc/resolv.conf
nameserver 192.168.2.23
[root@client01 ~]# nslookup www.tangpin.huajuan # 主提供服务
Server: 192.168.2.23
Address: 192.168.2.23#53
Name: www.tangpin.huajuan
Address: 192.168.2.22
[root@client01 ~]# echo "nameserver 192.168.2.26" > /etc/resolv.conf # 覆盖
[root@client01 ~]# cat /etc/resolv.conf
nameserver 192.168.2.26
[root@client01 ~]# nslookup www.tangpin.huajuan # 从提供服务
Server: 192.168.2.26
Address: 192.168.2.26#53
Name: www.tangpin.huajuan
Address: 192.168.2.22