• 主机列表
      | 主机IP | 主机名 | 配置 | 操作系统 | | —- | —- | —- | —- | | 192.168.26.100 | hd100 | 2C 4G | CentOS 7.4.1708 | | 192.168.26.110 | hd110 | 2C 4G | CentOS 7.4.1708 | | 192.168.26.120 | hd120 | 2C 4G | CentOS 7.4.1708 |

    • 所有节点设置时区,中国所用时区

      1. ~]# timedatectl #读取当前时间,查看是否为Time zone: Asia/Shanghai (CST, +0800),否则用以下命令修改
      2. ~]# timedatectl set-timezone Asia/Shanghai #设置时区为亚洲/上海
    • 所有节点安装(以hd100为例)

      1. [root@hd100 ~]# rpm -q ntp
      2. 未安装软件包 ntp
      3. [root@hd100 ~]# yum -y install ntp
      4. ...
      5. [root@hd100 ~]# systemctl list-unit-files | grep chronyd
      6. chronyd.service enabled
      7. [root@hd100 ~]# systemctl disable chronyd.service #关闭 chronyd 服务的开机自启行为
      8. Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.
      9. [root@hd100 ~]# systemctl list-unit-files | grep chronyd
      10. chronyd.service disabled
      11. [root@hd100 ~]# service ntpd start
      12. Redirecting to /bin/systemctl start ntpd.service
      13. [root@hd100 ~]# service ntpd status
      14. ...
      15. [root@hd100 ~]# systemctl enable ntpd.service
      16. ...
      17. [root@hd100 ~]# systemctl list-unit-files | grep ntpd #查看自启动项ntpd状态
      18. ntpd.service enable # 开机自启(ntpd服务-平滑调整时间)
      19. ntpdate.service disable # 开机不自启(ntpdate服务-瞬间调整时间)
    • hd100上执行:配置内网NTP-Server

      1. [root@hd100 ~]# vi /etc/ntp.conf
      2. ...
      3. # Permit all access over the loopback interface. This could
      4. # be tightened as well, but to do so would effect some of
      5. # the administrative functions.
      6. restrict 203.107.6.88
      7. restrict 127.0.0.1
      8. restrict ::1
      9. # Hosts on local network are less restricted.
      10. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
      11. restrict 192.168.26.0 mask 255.255.255.0 nomodify
      12. # Use public servers from the pool.ntp.org project.
      13. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
      14. server 203.107.6.88 # aliyun clock
      15. #server 0.centos.pool.ntp.org iburst
      16. #server 1.centos.pool.ntp.org iburst
      17. #server 2.centos.pool.ntp.org iburst
      18. #server 3.centos.pool.ntp.org iburst
      19. server 127.127.1.0 # local clock
      20. fudge 127.127.1.0 stratum 10
      21. ...

      说明:

    1.注释掉原有的四个server,如下:
    #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.在其上增加aliyun的server
    server 203.107.6.88 # aliyun clock
    3.增加aliyun server放行
    restrict 203.107.6.88
    4.增加内网server放行,不允许其修改服务器时间参数
    (单个IP放行)restrict 192.168.26.110 nomodify
    (网段放行)restrict 192.168.26.0 mask 255.255.255.0 nomodify
    5.增加外部服务时间不可获取时,使用本地时间
    server 127.127.1.0 # local clock
    fudge 127.127.1.0 stratum 10

    :配置文件中,写在靠前的 server 优先级高于 靠后的 server
    所以把 server 203.107.6.88 写在 server 127.127.1.0 之前,意为:本机优先与 203.107.6.88 同步
    启动NTPD服务,等待同步

    1. [root@hd100 ~]# service ntpd stop #修改完配置文件后,停止NTPD服务
    2. [root@hd100 ~]# ntpdate 203.107.6.88 #ntpd服务启动后不能执行ntpdate服务命令
    3. [root@hd100 ~]# service ntpd start #启动并查看NTP服务是否开始同步,通常需要在ntpd启动后15分钟内才能开始同步
    4. [root@hd100 ~]# ntpstat
    5. synchronised to NTP server (203.107.6.88) at stratum 3
    6. time correct to within 52 ms
    7. polling server every 128 s
    8. [root@hd100 ~]# ntpq -p
    9. remote refid st t when poll reach delay offset jitter
    10. ==============================================================================
    11. *203.107.6.88 10.137.38.86 2 u 47 128 377 41.523 17.724 12.166
    12. LOCAL(0) .LOCL. 10 l 40m 64 0 0.000 0.000 0.000

    参数解释:

    remote:是上层 NTP 服务器的 IP 或主主机名
    注意最左邊的符号:
    1.如果有『 * 』代表目前正在作用当中的上层 NTP
    2.如果是『 + 』代表也有连上线,而且可作为下一个提供时间更新的候选者。
    refid:是 remote 服务器参考的上一层 NTP 服务器的地址
    st:是 stratum 层级
    when:几秒钟前曾经做过时间同步化更新的动作;
    poll:下一次更新在几秒钟之后;
    reach:已经向上层 NTP 服务器要求更新的次数
    delay:网络传输过程中延迟的时间,单位为 10^(-3) 秒
    offset:时间补偿的结果,单位为 10^(-3) 秒
    jitter:Linux 系统时间与 BIOS 硬件时间的差距时间,单位为 10^(-3) 秒。
    • hd110、hd120上执行:配置内网NTP-Clients

    修改 /etc/ntp.conf 文件,主要的几个修改地方

    1.注释掉原有的四个server,如下:
    #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.在其上增加主节点IP
    server 192.168.26.100 # master lock
    3.增加master server放行
    restrict 192.168.26.100
    4.增加外部服务时间不可获取时,使用本地时间
    server 127.127.1.0 # local clock
    fudge 127.127.1.0 stratum 10 # 不可以超过15层

    启动NTPD服务,等待同步(15分钟以内)

    1.修改完配置文件后,停止NTPD服务
    # service ntpd stop
    2.同步本机与主节点时间服务器 192.168.26.100 (master clock)时间
    (以免时间差距太大,使ntp不能正常同步校准)
    # ntpdate 192.168.26.100
    3.启动并查看NTP服务是否开始同步,通常需要在ntpd启动后15分钟内才能开始同步
    # service ntpd start
    # ntpstat
    synchronised to NTP server (192.168.26.100) at stratum 4 — 已经开始同步,层数4
    —-比上层master的NTPD服务(为3)低一层,最高层为1
    time correct to within 508 ms — 时间校准到508ms以内
    polling server every 128 s — 每隔128s轮询一次服务器
    4.查看当前NTP服务器与上层NTP服务器的状态 (hd100就是192.168.26.100)
    1. # [root@hd110 ~]# ntpq -p
    2. remote refid st t when poll reach delay offset jitter
    3. ==============================================================================
    4. hd100 LOCAL(0) 11 u 40 64 17 0.843 40.812 0.659
    5. *LOCAL(0) .LOCL. 10 l 48 64 17 0.000 0.000 0.000
    6. # [root@hd110 ~]# ntpq -p #等一会后与NTP服务器hd100同步
    7. remote refid st t when poll reach delay offset jitter
    8. ==============================================================================
    9. *hd100 203.107.6.88 3 u 98 128 377 0.649 32.619 7.959
    10. LOCAL(0) .LOCL. 10 l 299 64 360 0.000 0.000 0.000

    —-210205 广州—-