服务器时间同步架构与实现chrony

实验背景

模拟企业局域服务器时间同步,保障各服务器系统准确性和时间一致性。

时间服务器系统搭建

实验架构图

image-20220822145222999

环境设备

设备IP规划

  • 国内互联网NTP服务器

    ntp.aliyun.com #阿里云NTP授时服务器地址
    ntp.tencent.com #腾讯云NTP授时服务器地址
    ntp.ntsc.ac.cn #中国科学院国家授时中心NTP授时服务器地址
    time.edu.cn #教育网内的授时服务器
    
  • 企业外网NTP服务器

    192.168.100.30  #NTPserver1,os:centos8
    192.168.100.31  #NTPserver2,os:rocklinux8
    
  • 企业内网局域网NTP客户端

    192.168.100.10 #NTPClient1
    192.168.100.11 #NTPClient2
    192.168.100.12 #NTPClient3
    

时间同步系统实现

  1. 对NTPserver1、NTPserver2、NTPClient1、NTPClient2、NTPClient3安装chronyd同步数据软件。

    yum install chrony -y  #centos,yum安装chronyd软件
    #apt install chrony -y  #Ubuntu,yum安装chronyd软件
    
  2. 配置NTPserver1、NTPserver2同步互联网NTP服务器,并授权允许同步时钟的客户端网段。

    编辑配置文件:/etc/chrony.conf

    #1、禁用默认的国外时间服务器地址
    #pool 2.centos.pool.ntp.org iburst 
    
    #2、添加国内互联网时间服务器域名地址
    server ntp.aliyun.com iburst 
    server ntp.tencent.com iburst
    server ntp.ntsc.ac.cn iburst
    server time.edu.cn iburst
    
    #3、添加允许客户端同步时钟的网段
    allow 0.0.0.0/0
    
    #4、设置即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端
    local stratum 10
    
  3. 允许NTPserver1、NTPserver2的chrony服务,

    systemctl start chronyd
    
  4. 验证配置文件是否配置成功,并查看服务状态是否运行,查看udp端口123是否开启,是否运行任何IP访问(0.0.0.0)

    systemctl status chronyd
    ss -anu
    

    image-20220822211031703

    image-20220822211119246

  5. 查看NTPserver1、NTPserver2是否已正常同步互联网时间服务器

    chronyc sources -V
    

    image-20220822210733373

    image-20220822210859457

  6. 配置客户端:NTPClient1、NTPClient2、NTPClient3同步NTPserver1和NTPserver2

    #1、centos7系统配置,禁用系统默认时间服务器地址
    #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
    
    #或centos8禁用默认的国外时间服务器地址
    #pool 2.centos.pool.ntp.org iburst
    
    #2、添加企业外网时间同步服务器地址
    server 192.168.100.30 iburst 
    server 192.168.100.31 iburst
    
  7. 重启客户端chrony服务

    systemctl restart 
    
  8. 验证客户端NTPClient1、NTPClient2、NTPClient3是否同步NTPserver1和NTPserver2时间服务器

    chronyc sources -V
    

    image-20220822210609330

    image-20220822210623724

    image-20220822210545142

  9. 可在NTPserver1和NTPserver2验证接入哪些客户端连接

    chronyc clients
    

    image-20220822212547044

    image-20220822212615913

命令和配置文件及详解

  • chrony介绍

    • 两个主要程序:chronyd和chronyc

      chronyd:后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿
      chronyc:命令行用户工具,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可在一台不同的远程计算机上工作
      
    • 服务unit 文件: /usr/lib/systemd/system/chronyd.service

    • 监听端口: 服务端: 123/udp,客户端: 323/udp

    • 配置文件: /etc/chrony.conf

  • 配置文件chrony.conf

    server #可用于时钟服务器,iburst 选项当服务器可达时,发送一个八个数据包而不是通常的一个数据
    包。 包间隔通常为2秒,可加快初始同步速度
    pool #该指令的语法与server 指令的语法相似,不同之处在于它用于指定NTP服务器池而不是单个
    NTP服务器。池名称应解析为随时间可能会变化的多个地址
    driftfile #根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中,会在重启后为系统时钟作
    出补偿
    rtcsync #启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)
    allow / deny #指定一台主机、子网,或者网络以允许或拒绝访问本服务器
    cmdallow / cmddeny #可以指定哪台主机可以通过chronyd使用控制命令
    bindcmdaddress #允许chronyd监听哪个接口来接收由chronyc执行的命令
    makestep # 通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定
    情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在
    调整期大于某个阀值时调整系统时钟
    local stratum 10 #即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它
    客户端
    
  • chronyc 可以运行在交互式和非交互式两种方式

    help 命令可以查看更多chronyc的交互命令
    accheck 检查是否对特定主机可访问当前服务器
    activity 显示有多少NTP源在线/离线
    sources [-v] 显示当前时间源的同步信息
    sourcestats [-v]显示当前时间源的同步统计信息
    add server 手动添加一台新的NTP服务器
    clients 报告已访问本服务器的客户端列表
    delete 手动移除NTP服务器或对等服务器
    settime 手动设置守护进程时间
    tracking 显示系统时间信息
    
  • 判断时间客户端是否连接上时间服务器:chronyc sources -v

    [21:28:46 root@rocky8 ~]#chronyc sources -v
    
      .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
     / .- Source state '*' = current best, '+' = combined, '-' = not combined,
    | /             'x' = may be in error, '~' = too variable, '?' = unusable.
    ||                                                 .- xxxx [ yyyy ] +/- zzzz
    ||      Reachability register (octal) -.           |  xxxx = adjusted offset,
    ||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
    ||                                \     |          |  zzzz = estimated error.
    ||                                 |    |           \
    MS Name/IP address         Stratum Poll Reach LastRx Last sample               
    ===============================================================================
    ^+ 203.107.6.88                  2   9   377   451  +2493us[+2375us] +/-   39ms
    ^* 139.199.215.251               2   9   337   206   +205us[ +145us] +/-   49ms
    ^+ 114.118.7.161                 1   9   231   259    +22ms[  +22ms] +/-   50ms
    ^? 202.112.0.7                   0   9     0     -     +0ns[   +0ns] +/-    0ns
    
    
    
    #--------------------------------------------
    ^+ 203.107.6.88 #表示可用时间服务器地址
    ^* 139.199.215.251 #表示客户端正在连接同步时间的服务器地址
    ^? 202.112.0.7 #表示不可用的服务器地址
    
posted @ 2022-08-22 21:54  天梯的脚印  阅读(766)  评论(2编辑  收藏  举报