CentOS7搭建NTP时间服务

一、简介

 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

real向往

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值