NTP介绍

  • NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC(Universal Time Coordinated,世界协调时),UTC 是由原子钟报时的国际标准时间,而 NTP 获得 UTC 的时间来源可以是原子钟、天文台、卫星,也可从 Internet 上面获取。其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。 补充知识:抢票软件等就是提前发送已知的数据包,时间一到服务器开始接受就会第一时间抢到。 NTP 服务器就是利用 NTP 协议提供时间同步服务的。

NTP服务器顾名思义就是时间同步服务器(Network Time Protocol),Linux下的ntp服务器配置相对来说都比较容易,但在Linux下有一个弊端,不同时区或者说是时间相差太大的无法同步,所以在配置ntp服务器之前需要把时间配置成相同的。

NTP时钟同步C/S模式

Linux集群环境下NTP服务器时间同步-LMLPHPLinux集群环境下NTP服务器时间同步-LMLPHP

NTP服务器作用

  • 产生与处理系统是各种计算设备集群的,计算设备将统一、同步的标准时间用于记录各种事件发生时序,如E-MAIL信息、文件创建和访问时间、数据库处理时间等。
  • 系统内不同计算设备之间控制、计算、处理、应用等数据或操作都具有时序性,若计算机时间不同步,这些应用或操作或将无法正常进行。
  • 系统是对时间敏感的计算处理系统,时间同步是大数据能够得到正确处理的基础保障,是大数据得以发挥作用的技术支撑。
  • 大数据时代,整个处理计算系统内的大数据通信都是通过网络进行,时间同步也是如此,利用大数据的互联网络传送标准时间信息,实现大数据系统内时间同步。

NTP时钟同步方式说明
NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:

  • 直接同步

  使用ntpdate命令进行同步,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。

  • 平滑同步

  使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。

ntpd、ntpdate的区别

  • 使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,他还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。
  • 时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:
  • 第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
  • 第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
  • 第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。
  • 因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
  • NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

注意:NTP 也存在一定局限性,如果系统时间比正确的时间要快的话,NTP 是不会帮你做调整的,而且当你的时间设置和正确的时间相差很大的时候,NTP 会花上很长一段时间进行同步调整。此外,当本地时间与标准时间相差30分钟以上时, ntpd 会停止工作

环境准备

NTP服务器操作步骤

  • 1.确认wencheng-1节点是否安装ntp,并修改时区。
[root@wencheng-1 ~]# rpm -qa |  grep ntp
[root@wencheng-1 ~]# yum install -y ntp
[root@wencheng-1 ~]# rpm -qa | grep ntp
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
ntp-4.2.6p5-29.el7.centos.2.x86_64
[root@wencheng-1 ~]# systemctl start ntpd
[root@wencheng-1 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-03-25 01:09:13 EDT; 1 months 16 days ago
  Process: 8716 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 8717 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─8717 /usr/sbin/ntpd -u ntp:ntp -g

Mar 25 01:09:13 wencheng-1 ntpd[8717]: Listen normally on 2 lo 127.0.0.1 UDP 123
Mar 25 01:09:13 wencheng-1 ntpd[8717]: Listen normally on 3 ens33 172.16.70.185 UDP 123
Mar 25 01:09:13 wencheng-1 ntpd[8717]: Listen normally on 4 lo ::1 UDP 123
Mar 25 01:09:13 wencheng-1 ntpd[8717]: Listen normally on 5 ens33 fe80::20c:29ff:fe7f:222b UDP 123
Mar 25 01:09:13 wencheng-1 ntpd[8717]: Listening on routing socket on fd #22 for interface updates
Mar 25 01:09:13 wencheng-1 ntpd[8717]: 0.0.0.0 c016 06 restart
Mar 25 01:09:13 wencheng-1 ntpd[8717]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Mar 25 01:09:13 wencheng-1 ntpd[8717]: 0.0.0.0 c011 01 freq_not_set
Mar 25 01:09:20 wencheng-1 ntpd[8717]: 0.0.0.0 c61c 0c clock_step +4075317.470688 s
May 11 05:11:18 wencheng-1 ntpd[8717]: 0.0.0.0 c614 04 freq_mode

[root@wencheng-1 ~]# netstat -ntplu | grep ntp
udp        0      0 172.16.70.185:123       0.0.0.0:*                           11655/ntpd
udp        0      0 127.0.0.1:123           0.0.0.0:*                           11655/ntpd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           11655/ntpd
udp6       0      0 fe80::20c:29ff:fe7f:123 :::*                                11655/ntpd
udp6       0      0 ::1:123                 :::*                                11655/ntpd
udp6       0      0 :::123                  :::*                                11655/ntpd

[root@wencheng-1 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

[root@wencheng-1 ~]# timedatectl set-timezone Asia/Shanghai
[root@wencheng-1 ~]# timedatectl
      Local time: Tue 2021-05-11 17:16:10 CST
  Universal time: Tue 2021-05-11 09:16:10 UTC
        RTC time: Thu 2021-03-25 05:04:14
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
  •  2.wencheng-1修改文件 /etc/ntp.conf。
[root@wencheng-1 ~]# mv /etc/ntp.conf /etc/ntp.conf_bak
[root@wencheng-1 ~]# vim /etc/ntp.conf
# 新增一下内容
# --------------------------------------
##记录和上级时间服务器的时间差异.
driftfile /var/lib/ntp/drift

##新增:日志目录.
logfile /var/log/ntpd.log

restrict default nomodify
##参数解释
## restrict可以限制客户端权限,可以使用的 parameter说明:
## kod            kod技术可以阻止“Kiss of Death “包对服务器的破坏
## nomodity       client可通过ntp进行时间同步,但不能通过 ntpq, ntpc 等更改server参数
## notrap         不提供trap远程登陆 (remote event logging) 功能
## nopeer         不与其它同一层的ntp server进行时间同步
## noquery        客户端不能够使用 ntpq, ntpc 等指令来查询时间服务器,即拒绝ntp时间同步;
## notrust        拒绝无认证的client
## ignore         拒绝所有连接到ntp server的请求
## 如果出现问题,可能是ipv6配置出错,可以尝试添加restrict -6 default kod nomodify notrap nopeer noquery  #针对ipv6设置
## 或者修改默认为restrict -4 default nomodify notrap nopeer noquery

##打开允许本地所有操作
restrict 127.0.0.1
restrict ::1

restrict 172.16.0.0 mask 255.255.255.0 nomodify notrap
## restrict 用来分配指定网段权限,格式如下:
## restrict [授权同步的网段] mask [netmask] [parameter]
##这一行的含义是授权172.16.0.0网段上的所有机器可以从这台机器上查询和同步时间.

##新增:时间服务器列表.
server time1.aliyun.com
server time2.aliyun.com
server time3.aliyun.com prefer
##参数解释
## server 用来设置上一级ntp服务器,这里使用阿里云提供的ntp服务器,parameter说明:
## prefer     最高优先级
## burst      当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
## iburst     当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。

# 如果无法与上层ntp server通信以本地时间为标准时间
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
# ------------------------------------------
  • 3.ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件
[root@wencheng-1 ~]# mv /etc/sysconfig/ntpd /etc/sysconfig/ntpd_bak
[root@wencheng-1 ~]# vim /etc/sysconfig/ntpd
# 添加一下内容
# -------------------------------------------
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"
# 设置是否同步到硬件时间
SYNC_HWCLOCK=yes
# Additional options for ntpdate 默认选项为-g
NTPDATE_OPTIONS="-g"
# -------------------------------------------
  •  4.验证与状态检查
[root@wencheng-1 ~]# systemctl restart ntpd
[root@wencheng-1 ~]# netstat -ntplu | grep ntp
udp        0      0 172.16.70.185:123       0.0.0.0:*                           11655/ntpd
udp        0      0 127.0.0.1:123           0.0.0.0:*                           11655/ntpd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           11655/ntpd
udp6       0      0 fe80::20c:29ff:fe7f:123 :::*                                11655/ntpd
udp6       0      0 ::1:123                 :::*                                11655/ntpd
udp6       0      0 :::123                  :::*                                11655/ntpd

[root@wencheng-1 ~]# ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3
   time correct to within 479 ms
   polling server every 64 s
注意:此命令出现上述synchronised结果比较慢,都等待几分钟。

[C:\~]$ ping time3.aliyun.com    # 验证文件/etc/ntp.conf的server time3.aliyun.com prefer对应IP地址
正在 Ping ntp.aliyun.com [203.107.6.88] 具有 32 字节的数据:
来自 203.107.6.88 的回复: 字节=32 时间=40ms TTL=51
来自 203.107.6.88 的回复: 字节=32 时间=40ms TTL=51
来自 203.107.6.88 的回复: 字节=32 时间=44ms TTL=51
来自 203.107.6.88 的回复: 字节=32 时间=41ms TTL=51
203.107.6.88 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 40ms,最长 = 44ms,平均 = 41ms

[root@wencheng-1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*203.107.6.88    10.137.38.86     2 u   66   64  377   39.198    5.282   2.988
 LOCAL(0)        .LOCL.          10 l  531   64    0    0.000    0.000   0.000

ntpq -p 参数详解
    remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
    如果有[*] 代表目前正在使用当中的上层NTP服务器。
    如果有[+] 代表也有连上上层NTP服务器,可以作为提高时间更新的候选NTP服务器
    如果有[-] 代表同步的该NTP服务器被认为是不合格的NTP Server
    如果有[x] 代表同步的外网NTP服务器不可用
    refid :指的是给上层NTP服务器提供时间校对的服务器。
    St:上层NTP服务器的级别。
    When: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
    Poll :本地主机与上层NTP服务器进行时间校对的周期(单位:s)
    reach:八进制数,表示最近8次时钟同步包接收情况(1表示接收成功,0表示接收失败。每接收一个包左移一位。对于一个运行较长时间的NTP client而言,这个值应该是377->11,111,111,即最近8次包接收均成功;否则表示有丢包情况发生).
    delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
    offset:时间补偿的结果,单位为10^(-6) 秒
    jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

 NTP客户端操作步骤。

  • 1.确认是否安装ntp,并修改时区。
[root@wencheng-2 ~]# yum install -y ntp
[root@wencheng-1 ~]# rpm -qa | grep ntp
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
ntp-4.2.6p5-29.el7.centos.2.x86_64

[root@wencheng-2 ~]# timedatectl set-timezone Asia/Shanghai
[root@wencheng-2 ~]# date -s "2020-10-10 10:10"    #预设置错误的时间
[root@wencheng-2 ~]# timedatectl
      Local time: Sat 2020-10-10 10:16:35 CST
  Universal time: Sat 2020-10-10 02:16:35 UTC
        RTC time: Wed 2021-05-12 04:18:08
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
  •  2.修改文件/etc/ntp.conf,主要修改Server选择部分。
[root@wencheng-2 ~]# cp /etc/ntp.conf /etc/ntp.conf_bak
[root@wencheng-2 ~]# vim /etc/ntp.conf
#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

#注释所有centos.pool.ntp,再新增
server 172.16.70.185 frefer
fudge 172.16.70.185 stratum 10
  •  3.验证与状态检查
[root@wencheng-2 ~]# systemctl start ntpd
[root@wencheng-2 ~]# systemctl enable ntpd

[root@wencheng-2 ~]# netstat -nutpl | grep ntp
udp        0      0 172.16.70.186:123       0.0.0.0:*                           9088/ntpd
udp        0      0 127.0.0.1:123           0.0.0.0:*                           9088/ntpd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           9088/ntpd
udp6       0      0 fe80::20c:29ff:fef6:123 :::*                                9088/ntpd
udp6       0      0 ::1:123                 :::*                                9088/ntpd
udp6       0      0 :::123                  :::*                                9088/ntpd

[root@wencheng-2 ~]# ntpstat
synchronised to NTP server (172.16.70.185) at stratum 4
   time correct to within 76 ms
   polling server every 256 s

[root@wencheng-2 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*172.16.70.185   203.107.6.88     3 u  253  256  377    0.312    1.557   0.997

[root@wencheng-2 ~]# timedatectl
      Local time: Wed 2021-05-12 14:24:30 CST
  Universal time: Wed 2021-05-12 06:24:30 UTC
        RTC time: Wed 2021-05-12 06:24:30
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
  •  4.添加时间同步计划任务。
[root@wencheng-2 ~]# vim /etc/cron.d/ntp
# 每两个小时同步一次,并修改硬件时间
0 */2 * * * /usr/sbin/ntpdate 172.16.70.185 && /usr/sbin/hwclock -w &>/dev/null

至此,NTP服务器构建,并完成客户端时间同步!

 
 
 
 
 
 
06-22 12:33