NTP网络授时服务器部署及网络时钟同步设置说明

NTP网络授时服务器部署及网络时钟同步设置说明

NTP网络授时服务器部署及网络时钟同步设置说明

 本文由安徽京准科技提供@请勿转载。

一、前言

1、NTP简介

NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

2、为什么需要NTP服务器?

时间保持同步对于服务器集群来说尤为重要,比如说电商的秒杀,以及火车票的抢购等等,如果服务器时间不同步,那么不同的用户可能不是在同一时间点进行抢购的,就会出现不公平的问题。 对于集群化部署的应用,例如数据库集群,只有时间同步了,同一时间到达不同数据库节点的数据才会有相同的时间戳。集群时间的一致性影响了分布式系统的一致性。

二、准备工作

1、服务器规划

机器名

IP

节点应用

ntp01

192.168.88.88

NTP时间服务器

2、软件环境说明

说明

Linux Server

CentOS 7

NTP

4.2.6

三、部署过程

1、安装NTP

yum install -y ntp

2、修改NTP配置

#修改配置文件
vi /etc/ntp.conf

#在配置中增加以下配置:

#允许上层时间服务器主动修改本机时间
restrict 0.centos.pool.ntp.org nomodify notrap noquery
restrict 1.centos.pool.ntp.org nomodify notrap noquery
restrict 2.centos.pool.ntp.org nomodify notrap noquery
restrict 3.centos.pool.ntp.org nomodify notrap noquery

#外部时间服务器不可用时,以本地时间作为时间服务
server 127.0.0.1 fudge 127.0.0.1 stratum 10

3、与公共NTP服务器预先同步

为了保证本地NTP服务器能正常与公共NTP服务器进行同步,先跟公共NTP服务器同步一次。 再启动NTP服务

[root@ntp01 ~]# ntpdate cn.pool.ntp.org 27 Feb 19:27:02 ntpdate[2632]: adjust time server 85.199.214.100 offset -0.007263 sec

4、启动NTP服务&开机启动设置

#启动NTP服务
systemctl start ntpd

#将NTP服务设置为开机启动
systemctl enable ntpd

5、查看NTP服务信息

[root@ntp01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter ============================================================================== +118.122.35.10 223.255.185.2 2 u 30 64 3 48.062 -17.829  2 u 29 64 3 34.870 2.672 2.617 -ntp1.ams1.nl.le 130.133.1.10 2 u 27 64 3 238.254 21.513 1.854 +ntp5.flashdance 194.58.202.148 2 u 26 64 3 312.940 4.263 1.237

NTP服务信息说明:

说明

remote

当前远程NTP服务器,+表示可用,-表示不可用,*表示推荐

refid

用于和本地时钟同步的远程服务器的 IP 地址

st

Stratum(阶层),表示经过n=2次NTP同步到当前服务器

t

类型,u表示单播(unicast)。其它值包括本地(local)、多播(multicast)、广播(broadcast)。

when

自从上次和服务器交互后经过的时间(以秒数计)。

poll

和服务器的轮询间隔,以秒数计。

reach

表示和服务器交互是否有任何错误的八进制数。值 337 表示 * *(即十进制的255)。

delay

服务器和远程服务器来回的时间。

offset

我们服务器和远程服务器的时间差异,以毫秒数计。

jitter

两次取样之间平均时差,以毫秒数计。

6、防火墙配置

#开放NTP端口
firewall-cmd --add-port=123/udp --permanent

#重载防火墙规则
firewall-cmd --reload

四、客户端同步设置

1、Linux客户端

Linux主机需要 ntpdate 软件包来和NTP服务器同步时间 这里ken.io 以CentOS客户端为例说明,由于CentOS7已经内置了ntpdate,所以无需安装

1.1、方式1:单次同步

ntpdate 192.168.88.88

1.2、方式2:安装ntp服务进行自动同步(推荐)

主要思路就是讲客户端作为NTP服务的一个层级自动从已经配置好的本地NTP服务器同步时间

相当于通过NTP服务以以下顺序传播时间: 公共NTP服务器->本地NTP服务器->客户端(NTP服务)

  • 安装NTP
yum install -y ntp
  • 修改配置
#修改配置文件
vi /etc/ntp.conf


#修改server节点

server 192.168.88.88 #在配置中增加以下配置:

#允许上层时间服务器主动修改本机时间
restrict 192.168.88.88 nomodify notrap noquery


#外部时间服务器不可用时,以本地时间作为时间服务
server 127.0.0.1 fudge 127.0.0.1 stratum 10
  • 启动NTP服务&开机启动设置
#启动NTP服务
systemctl start ntpd

#将NTP服务设置为开机启动
systemctl enable ntpd

2、Windows客户端

控制面板->日期和时间->设置日期和时间->Internet时间->更改设置

输入对应的NTP服务器IP,然后确定即可,如下图:

原文地址:https://www.cnblogs.com/yfcs999/p/99poik123.html