一、简介
NTP(Network Time Protocol)时间服务器,可用于服务器之间的时间同步,使时间保持一致,对于一些对时间一致性要求高的服务(集群)很适用。
在这里,主要是针对无网络的情况下,要求服务器之间时间一致。
二、NTP
主机规划
服务端 node2:192.168.71.12
客户端 node3:192.168.71.13
2.1 服务端
安装ntp服务
[root@node2 ~]# yum install ntp ntpdate -y
安装ntp时间服务器,时间同步命令
修改默认配置文件
[root@node2 ~]# vim /etc/ntp.conf
#系统与bios时间偏差记录
driftfile /var/lib/ntp/drift
#restrict限制操作,默认限制客户端所有操作
#restrict default nomodify notrap nopeer noquery
#限制192.168.71.0网段内的服务器不能修改
restrict 192.168.71.0 mask 255.255.255.0 nomodify
#允许本地所有操作,后边没有了限制操作
restrict 127.0.0.1
restrict ::1
#注释网络时间服务,让主机以本地时间为主
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 127.127.1.0 #以本地时间为主,可以设置其他网络时间,但这里主要是针对无网络情况
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
参数了解
ignore:拒绝所有类型的ntp联机
restrict ip mask parameter:控制相应客户端IP的权限,没有parameter时,表示该IP或网段不受任何限制
nomodify:客户端不能使用ntpc与ntpq来修改服务端的时间参数,但允许客户端进行网络校时
notrap:不提供trap远程登录
nopeer:用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
noquery:不提供网络校时,客户端不能使用ntpq,ntpc等命令查询服务器
notrust:拒绝没有认证的客户端
127.0.0.1:是一个回环地址,本地IP,如果要做授时服务器时,需要用127.127.0.1
启动ntp服务
[root@node2 ~]# systemctl restart ntpd
查看端口 UDP123
[root@node2 ~]# ss -lnu
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 172.17.0.1:123 *:*
UNCONN 0 0 192.168.71.12:123 *:*
UNCONN 0 0 127.0.0.1:123 *:*
UNCONN 0 0 *:123 *:*
UNCONN 0 0 fe80::20c:29ff:fe84:bc48%ens33:123 :::*
UNCONN 0 0 ::1:123 :::*
UNCONN 0 0 :::123 :::*
2.2 客户端
安装ntp服务
[root@node3 ~]# yum install ntp ntpdate -y
事先同步一下时间
此命令与ntp服务有冲突,同时只能运行一个
[root@node3 ~]# ntpdate 192.168.71.12
20 Aug 09:46:03 ntpdate[7449]: adjust time server 192.168.71.12 offset 0.000058 sec
修改配置文件
[root@node3 ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
#添加时间服务器
server 192.168.71.12
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monito
启动ntp服务
[root@node3 ~]# systemctl restart ntpd
[root@node3 ~]# systemctl enable ntpd
查看ntp客户端与ntp服务端的状态
[root@node3 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*192.168.71.12 LOCAL(0) 6 u 62 64 177 0.811 0.092 0.077