【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置

一、实验说明

ntp服务既可以作为ntpServer,也可以作为ntpClient,只是配置稍有不同。

一台作为ntp服务与外部公共ntp服务同步时间,同时作为内网的ntpServer,其它机器的ntpClient与这台服务做时间同步。

NTP时间同步方式选择

NTP同步方式在Linux下一般两种:使用ntpdate命令直接同步和使用ntpd服务平滑同步。

假设现有一台设备,系统时间是 13:00 , 真实的当前时间(在空中,或许卫星上,这里如果是在准备同步的上级目标ntpServer)是: 12:30  。

如果我们使用ntpdate同步(ntpdate -u xx.xx.xx.xx),操作系统的时间马上更新为12:30。

假如我们的系统有一个定时应用是在每天12:40执行,那么实际今天这个的任务已经执行过了(当前时间是13:00嘛),如今被ntpdate改动为12:30,那么意味作10分钟后。又会执行一次任务。这就糟糕了,这个任务仅仅能执行一次的嘛!

我想你(事实上是我)已经懂了ntpdate时间同步的隐患,当然这个样例有些极端,但的确是有风险的,生产环境我不打算这么干,还是稳妥点好。

解决该问题的办法就是时间平滑更改,不会让一个时间点在一天内经历两次。这就是ntpd服务方式平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的,一次一点的同步,全然同步好须要较长时间,所以一般开启ntpd服务同步前先用ntpdate先手动同步一次


二、实验环境

操作系统: CentOS 7.5 Minimal

ntpServer:192.168.1.104

ntpClient:192.168.1.105


三、软件安装

在ntpServer和ntpClient服务器

# yum info ntp

# yum -y install  ntp

# rpm -ql ntp

# ntpq -c version


# systemctl enable ntpd


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第1张图片


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第2张图片


四、配置内网ntpServer


在配置前,先使用ntpdate手动同步外网上游ntpServer的时间,免得本机与外部时间server时间差距太大,让ntpd不能正常同步。

# ntpdate  -u  202.112.10.36


# vim  /etc/ntp.conf

改动部分配置如下,其它的是默认

###########################################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1


# 同意内网其它机器同步时间

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap


# 中国取最活跃的ntpServer http://www.pool.ntp.org/zone/cn

# prefer 表示优先级,优先选择的ntpServer服务器

server 210.72.145.44 iburst  prefer 

server 202.112.10.36  iburst           

server 59.124.196.83  iburst     


# 允许上层ntpServer主动改动本机时间

restrict 210.72.145.44 nomodify notrap noquery

restrict 202.112.10.36 nomodify notrap noquery

restrict 59.124.196.83 nomodify notrap noquery


# 外部ntpServer不可用时,以本地时间作为时间服务

server  127.127.1.0   

fudge  127.127.1.0  stratum 10


# 绑定服务的监听IP,默认是0.0.0.0

interface ignore  wildcard

interface listen  192.168.1.104

interface listen  127.0.0.1


includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

################################################


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第3张图片

# systemctl  start ntpd

# systemctl  status  ntpd

# ss -uan


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第4张图片

ntpd服务启动后,默认监听端口为 123/udp

# firewall-cmd --zone=public  --add-port=123/udp  --permanent

# firewall-cmd --reload


ntpq -p 查看网络中的btpServer,同一时候显示client和每一个server的关系

#  ntpq  -p


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第5张图片



【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第6张图片

ntpstat 命令查看时间同步状态,这个一般须要5-10分钟后才干成功连接和同步,所以server启动后须要稍等下。

# ntpstat


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第7张图片

如果上游ntpServer不可用,那么以本服务器时间作为时间服务。


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第8张图片


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第9张图片

五、配置内网ntpClient

内网其它设备作为ntpClient配置,相对就比较简单,并且全部设备的配置都同样。

首先须要安装ntpd服务。然后配置为自启动(与ntpServer全然一样),然后找当中一台配置/etc/ntp.conf文件,配置完毕验证通过后,复制到其它ntpClient机器,直接使用就可以。


配置ntpClient之前,使用ntpdate跟内网ntpServer强制同步一次,避免时差过大。

# ntpdate -u  192.168.1.104



# vim  /etc/ntp.conf

##################################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1


# 设置内网ntpServer地址

server 192.168.1.104  iburst


# 允许上层ntpServer主动改动本机时间

restrict 192.168.1.104 nomodify notrap noquery


# 外部ntpServer不可用时,以本地时间作为时间服务

server  127.127.1.0 

fudge  127.127.1.0  stratum 10


# 绑定服务的监听IP,默认是0.0.0.0

interface ignore  wildcard

interface listen  192.168.1.105

interface listen  127.0.0.1


includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor


#########################################


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第10张图片

# systemctl  start ntpd

# systemctl  status  ntpd

# ss -uan

【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第11张图片


ntpd服务启动后,默认监听端口为 123/udp

# firewall-cmd --zone=public  --add-port=123/udp  --permanent

# firewall-cmd --reload

注意:ntpd作为client也要开放 123/udp ,否则ntpServe无法调整ntpClient的时间,也就无法同步了!


ntpq -p 查看网络中的btpServer,同一时候显示client和每一个server的关系

# ntpq  -p

# ntpstat


【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置_第12张图片

七、参考


ntp服务器同步时间详细配置

https://www.cnblogs.com/shanhua-fu/p/9281040.html


ntp服务及时间同步问题

https://www.cnblogs.com/yjbjingcha/p/6918917.html


解决ntp的错误 no server suitable for synchronization found

https://blog.csdn.net/weidan1121/article/details/3953021


时间服务器: NTP 服务器

http://cn.linux.vbird.org/linux_server/0440ntp.php

你可能感兴趣的:(【ntp】CentOS7.x ntpd作为ntpServer ntpClient 的安装配置)