> 文档中心 > 【Linux部署】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)

【Linux部署】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)


1.什么是NTP

百度百科:网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。应用场景:

  • 局域内网:由于不能访问互联网时间,需要建立ntp服务器用于时间的同步。
  • 系统集群:对时间敏感的计算处理系统内不同设备之间控制、计算、处理、应用等数据或操作都具有时序性,需要同步的标准时间用于记录各种事件发生的时序,这对后续的数据分析

注意:在CentOS-8中默认不再支持ntp软件包,时间同步将由chrony来实现,以下内容系统环境为CentOS Linux release 7.9.2009 (Core)

2.NTP时间服务器端搭建

2.1 安装

CentOS Linux release 7.9.2009 (Core)自带ntp软件包的,首先要确认是否已安装ntp和ntpdate服务:

# 查看是否安装[root@aliyun ~]# yum list | grep ntpntp.x86_64   4.2.6p5-29.el7.centos.2ntpdate.x86_64      4.2.6p5-29.el7.centos.2# 未安装则使用命令进行安装yum install ntp# 启动服务systemctl start ntpd# 开机自启动systemctl enable ntpd# 查看ntp服务器状态[root@aliyun ~]# ntpstatsynchronised to NTP server (100.100.61.88) at stratum 2   time correct to within 14 ms   polling server every 1024 s

2.2 服务器端配置

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

ntp.conf修改前(去掉注释信息):

driftfile /var/lib/ntp/driftrestrict default nomodify notrap nopeer noqueryrestrict 127.0.0.1 restrict ::1server 0.centos.pool.ntp.org iburstserver 1.centos.pool.ntp.org iburstserver 2.centos.pool.ntp.org iburstserver 3.centos.pool.ntp.org iburstincludefile /etc/ntp/crypto/pwkeys /etc/ntp/keysdisable monitor

ntp.conf修改后:

driftfile /var/lib/ntp/drift# 1.default表示对所有的计算机进行控制restrict default nomodify notrap nopeer noquery# 局域网可放行可以指定网段或列出单独IP# restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify # restrict xxx.xxx.xxx.x nomodify # 1.endrestrict 127.0.0.1 restrict ::1# 2.注释掉公网NTP服务器# 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# 2.end# 3.开启自身同步server 127.127.1.0Fudge 127.127.1.0 stratum 8# 3.endincludefile /etc/ntp/crypto/pwkeys /etc/ntp/keysdisable monitor

2.3 服务器端测试

# 重启服务systemctl restart ntpd# 测试[root@aliyun ~]# ntpstatsynchronised to local net (127.127.1.0) at stratum 6   time correct to within 949 ms   polling server every 64 s

3.Linux客户端配置

3.1 安装

安装过程跟服务器端一致,这里不再赘述。

3.2 客户端配置

ntp.conf修改前不再赘述,修改后:

driftfile /var/lib/ntp/driftrestrict default nomodify notrap nopeer noqueryrestrict 127.0.0.1 restrict ::1# 1.注释掉公网NTP服务器# 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# 1.end# 2.开启服务器同步server aliyunFudge aliyun stratum 8# 2.endincludefile /etc/ntp/crypto/pwkeys /etc/ntp/keysdisable monitor

3.3 客户端测试

# 查看NTP服务是否开启[root@tcloud ~]# timedatectl      Local time: Fri 2022-03-25 15:29:25 CST  Universal time: Fri 2022-03-25 07:29:25 UTC RTC time: Fri 2022-03-25 07:29:24Time zone: Asia/Shanghai (CST, +0800)     NTP enabled: noNTP synchronized: no RTC in local TZ: no      DST active: n/a# 开启NTP服务[root@tcloud ~]# timedatectl set-ntp yes[root@tcloud ~]# timedatectl      Local time: Fri 2022-03-25 15:52:30 CST  Universal time: Fri 2022-03-25 07:52:30 UTC RTC time: Fri 2022-03-25 07:52:29Time zone: Asia/Shanghai (CST, +0800)     NTP enabled: yesNTP synchronized: yes RTC in local TZ: no      DST active: n/a
# 重启服务systemctl restart ntpd# 测试[root@tcloud ~]# ntpstatunsynchronised  time server re-starting   polling server every 8 s# 配置后一般要等待几分钟才能与/etc/ntp.conf中的服务器端完成同步[root@tcloud ~]# ntpstatsynchronised to NTP server (101.201.69.99) at stratum 7   time correct to within 968 ms   polling server every 64 s

3.4 另一种方式

使用ntpdate命令结合定时任务实现时间同步。此时要停止ntpd服务器,测试ntpdate aliyun报错,查看报错信息:

[root@tcloud ~]# ntpdate -d aliyun28 Mar 15:33:22 ntpdate[17145]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)Looking for host aliyun and service ntphost found : aliyuntransmit(101.201.69.99)101.201.69.99: Server dropped: no dataserver 101.201.69.99, port 12328 Mar 15:33:30 ntpdate[17145]: no server suitable for synchronization found

解决:关闭防火墙,配置aliyun服务器的安全组放开端口123

# 端口开放后[root@tcloud ~]# ntpdate aliyun28 Mar 16:06:57 ntpdate[2330]: adjust time server 101.201.69.99 offset 0.000112 sec
# 添加定时任务vim /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# /etc/crontab 内容如下# For details see man 4 crontabs# Example of job definition:# .---------------- minute (0 - 59)# |  .------------- hour (0 - 23)# |  |  .---------- day of month (1 - 31)# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# |  |  |  |  |# *  *  *  *  * user-name  command to be executed# 每分钟同步一次*/1  *  *  *  * root  ntpdate aliyun

4.Windows客户端配置

4.1 配置并启用客户端

Win键+r输入gpedit.msc打开本地组策略编辑器,找到配置 Windows NTP 客户端(计算间配置-管理模板-系统-Windows 时间服务-时间提供程序)进行客户端配置:

在这里插入图片描述
配置页面:

在这里插入图片描述
启用客户端配置:

在这里插入图片描述

4.2 使用客户端配置

在这里插入图片描述

5.ntp服务其他参数配置

5.1 查看ntp服务器有无和上层ntp连通

[root@aliyun ~]# ntpstatsynchronised to NTP server (139.199.214.202) at stratum 3   time correct to within 1010 ms   polling server every 64 s 

5.2 查看ntp服务器与上层ntp的状态

[root@aliyun ~]# ntpq -p     remote    refid      st t when poll reach   delay   offset  jitter==============================================================================+skitty.itu.ch   84.16.67.12      2 u   49   64   17  174.891   20.582   5.712+139.199.214.202 100.122.36.196   2 u   47   64   37   46.355   43.764   9.636-time.cloudflare 10.28.12.207     3 u   49   64   37  216.628   31.101  12.642*tick.ntp.infoma .GPS.     1 u   49   64   37  156.665   12.054  11.269# 或者使用以下命令查看实时状态[root@aliyun ~]# watch "ntpq -p"
  • remote:列出源的 IP 地址或主机名。第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。
  • refid:参考上一层ntp主机地址。
  • st:stratum阶层。
  • when:多少秒前曾经同步过时间。指出从轮询源开始已过去的时间(秒)。
  • poll:下次更新在多少秒后。指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
  • reach:已经向上层ntp服务器要求更新的次数。是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
  • delay:网络延迟。
  • offset:时间补偿。是源时钟与本地时钟的时间差(毫秒)。
  • jitter:系统时间与bios时间差。

5.3 从其他博客收集的信息

5.3.1 /etc/ntp.conf 配置内容

driftfile /var/lib/ntp/drift# 1. 权限设置 包括放行上层服务器以及开放局域网用户来源 restrict default nomodify notrap nopeer noqueryrestrict 127.0.0.1restrict ::1# 放行局域网用户来源 或者列出单独IPrestrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify # 2. 设定主机来源 请先将原本的 [0|1|2|3].centos.pool.ntp.org 注掉#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# 以这部主机为最优先的serverserver xxx.xxx.xxx.xx prefer  # 3. 默认的一个内部时钟数据 用在没有外部NTP服务器时 使用它为局域网用户提供服务server 127.127.1.0Fudge 127.127.1.0 stratum 8includefile /etc/ntp/crypto/pwkeys /etc/ntp/keysdisable monitor

5.3.2 restrict选项格式

restrict [ 客户端IP ] mask [ IP掩码 ] [参数]

客户端IPIP掩码指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:

  • ignore:拒绝连接到NTP服务器。
  • nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
  • noquery: 不提供客户端的时间查询。
  • notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
  • notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
  • nopeer: 提供时间服务,但不作为对等体。
  • kod: 向不安全的访问者发送Kiss-Of-Death报文。

5.3.3 server选项格式

server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:

  • key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
  • version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
  • prefer: 如果有多个server选项,具有该参数的服务器有限使用。
  • mode: 指定数据报文mode字段的值。
  • minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
  • maxpoll: 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
  • iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。

5.3.4 层次(stratum)

stratum根据上层server的层次而设定(+1)。对于提供network time service provider的主机来说,stratum的设定要尽可能准确。而作为局域网的time service provider,通常将stratum设置为10。

0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互。ntpd对下层client来说是service server,对于上层server来说它是client。ntpd根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。所有的配置都在/etc/ntp.conf文件中。

5.3.5 同步硬件时钟

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加【SYNC_HWCLOCK=yes】这样,就可以让硬件时间与系统时间一起同步。

允许BIOS与系统时间同步,也可以通过hwclock -w 命令。

5.3.6 ntpd、ntpdate的区别

下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。时钟的跃变,对于某些程序会导致很严重的问题。

许多应用程序依赖连续的时钟,毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题。

不安全:ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

不精确:一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

不优雅:由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。

因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD在和时间服务器的同步过程中,会把BIOS计时器的振荡频率偏差——或者说Local Clock的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

5.3.7 常用NTP服务器地址及IP

210.72.145.44 国家授时中心服务器IP地址133.100.11.8 日本 福冈大学  time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland   time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland   time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado   time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado   time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado   utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder   time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado   time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington   nist1.symmetricom.com 69.25.96.13 Symmetricom, San Jose, California   nist1-dc.glassey.com 216.200.93.8 Abovenet, Virginia   nist1-ny.glassey.com 208.184.49.9 Abovenet, New York City   nist1-sj.glassey.com 207.126.98.204 Abovenet, San Jose, California   nist1.aol-ca.truetime.com 207.200.81.113 TrueTime, AOL facility, Sunnyvale, California   nist1.aol-va.truetime.com 64.236.96.53 TrueTime, AOL facility, Virginia  ntp.sjtu.edu.cn 202.120.2.101 上海交通大学网络中心NTP服务器地址s1a.time.edu.cn 北京邮电大学  s1c.time.edu.cn 北京大学  s2m.time.edu.cn 北京大学s1d.time.edu.cn 东南大学  s1e.time.edu.cn 清华大学  s2a.time.edu.cn 清华大学  s2b.time.edu.cn 清华大学  s1b.time.edu.cn 清华大学  s2c.time.edu.cn 北京邮电大学  s2d.time.edu.cn 西南地区网络中心  s2e.time.edu.cn 西北地区网络中心  s2f.time.edu.cn 东北地区网络中心  s2g.time.edu.cn 华东南地区网络中心  s2h.time.edu.cn 四川大学网络管理中心  s2j.time.edu.cn 大连理工大学网络中心  s2k.time.edu.cn CERNET桂林主节点  

5.3.8 查看网关方法

[root@aliyun ~]# route -nKernel IP routing tableDestination     Gateway  Genmask  Flags Metric Ref    Use Iface0.0.0.0  172.20.111.253  0.0.0.0  UG    0      0 0 eth0169.254.0.0     0.0.0.0  255.255.0.0     U     1002   0 0 eth0172.20.96.0     0.0.0.0  255.255.240.0   U     0      0 0 eth0[root@aliyun ~]# ip route showdefault via 172.20.111.253 dev eth0169.254.0.0/16 dev eth0 scope link metric 1002172.20.96.0/20 dev eth0 proto kernel scope link src 172.20.111.49[root@aliyun ~]# netstat -rKernel IP routing tableDestination     Gateway  Genmask  Flags   MSS Window  irtt Ifacedefault  gateway  0.0.0.0  UG 0 0   0 eth0link-local      0.0.0.0  255.255.0.0     U  0 0   0 eth0172.20.96.0     0.0.0.0  255.255.240.0   U  0 0   0 eth0