美文网首页
chrony时间同步服务

chrony时间同步服务

作者: 木头_95b3 | 来源:发表于2020-11-02 21:32 被阅读0次

    本想简单的找点chrony的讲解视频看看,但没找到;我想,可能是因为服务这个服务太简单了吧,觉得没什么讲的;所以都忽略了;现在只有自己烧烧脑,手动研究一下下

    chrony是 rhel7 上的时间同步,一般安装完系统就有了;rhel6 不带有,rhel8也不带有,都需要另外安装

    公司里 虚拟机加物理机 2000+ 台 机器,一直使用的是 ntpdate 直接强行同步三台NTP服务器,就这么暴力的用了几年,到现在,也没出啥问题;但是身为一名运维屌丝,预感到了强烈安全隐患。

    是时候展现真正的技术了 ~~~

    一啪啦的扯淡,进入正题。

    我的系统版本
    [root@localhost ~]# cat /etc/redhat-release 
    CentOS Linux release 7.8.2003 (Core)
    

    我的 rhel8上面没有,我使用过yum安装的

    安装chrony:
    yum install chrony -y
    
    • 1.安装之前可以先检查一下,系统是否已经带有:rpm -qa|grep chrony*
      1. 一般yum源都带有,没有yum源的可以自己去下载 rpm包安装

    安装完成之后,系统会多出一个 chronyd的服务

    启动chrony服务,,并设为开机启动
    systemctl  start chronyd --now 
    

    查看服务状态:systemctl status chronyd
    停止服务:systemctl stop chronyd
    重启服务:systemctl restart chronyd

    我们可以用 rpm -ql chrony 看一下安装 chrony 后产生的安装文件,帮助了解这个服务的大体结构,与复杂度,不想看也可以不看;

    [root@node5 ~]# rpm -ql chrony
    /etc/NetworkManager/dispatcher.d/20-chrony
    /etc/chrony.conf            # 服务的配置文件
    /etc/chrony.keys
    /etc/dhcp/dhclient.d/chrony.sh
    /etc/logrotate.d/chrony
    /etc/sysconfig/chronyd
    /usr/bin/chronyc     # 安装包产生的命令
    /usr/lib/.build-id
    /usr/lib/.build-id/17
    /usr/lib/.build-id/17/2a35db575ce982518cbb4750b3ba30e4e792fe
    /usr/lib/.build-id/77
    /usr/lib/.build-id/77/cc7ed196a76d590a3ef44b920dbbfeecc64e82
    /usr/lib/systemd/ntp-units.d/50-chronyd.list
    /usr/lib/systemd/system/chrony-dnssrv@.service
    /usr/lib/systemd/system/chrony-dnssrv@.timer      # 安装包安装的 system 服务
    /usr/lib/systemd/system/chrony-wait.service
    /usr/lib/systemd/system/chronyd.service
    /usr/libexec/chrony-helper
    /usr/sbin/chronyd     # 安装包产生的命令
    /usr/share/doc/chrony  
    /usr/share/doc/chrony/FAQ
    /usr/share/doc/chrony/NEWS     # DOC使用文档
    /usr/share/doc/chrony/README
    /usr/share/doc/chrony/ntp2chrony.py
    /usr/share/licenses/chrony
    /usr/share/licenses/chrony/COPYING
    /usr/share/man/man1/chronyc.1.gz
    /usr/share/man/man5/chrony.conf.5.gz   # man帮助文件
    /usr/share/man/man8/chronyd.8.gz
    /var/lib/chrony
    /var/lib/chrony/drift
    /var/lib/chrony/rtc
    /var/log/chrony    #日志文件
    

    大体上就是这些东西了
     
     
     

    配置NTP:

    下面开始介绍,将chrony配置为 NTP 服务端,用来供NTP 客户端同步使用的,后面再讲客户端

    配置前先讲一下 配置文件件:

    [root@node5 ~]# cat /etc/chrony.conf
    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    pool 2.rhel.pool.ntp.org iburst   
    # 1.指的是与某域名(rhel.pool.ntp.org)做时间同步
    # 2.pool 用于域名的同步,与 server 类似 ,但是 server是用于 IP的同步;
    #   pool域名同步可能会解析出多个IP地址,默认与会同步前4个,最大能设置为 16个,设置方法:
           pool pool.ntp.org iburst maxsources 3 (数量)
    # 3.iburst 加快同步速度,就是在服务启动的时候,前4个时间同步请求的发送时间间隔两秒;
    #   如果不加,系统就直接 以 minpoll 的规则来运行,默认为 2^6(64秒) 同步一次
    
    # Record the rate at which the system clock gains/losses time.
    driftfile /var/lib/chrony/drift
    # 把同步的时间误差保存到该文件中
    
    # Allow the system clock to be stepped in the first three updates
    # if its offset is larger than 1 second.
    makestep 1.0 3
    # 这个参数,开始看man帮助没看明白,然后又搜索一下网上别人的文档,不讲大白话,
    #  还是有点蒙,而且基本都是抄来抄去的;
    #  自己理解了一下:
    #  正常情况下,时间同步服务会根据时间差来加快,或减慢系统时间的流动,
    #  但是呢,如果时间差太多了;时间同步就会很长时间才能跟上;这个参数就是设置了一个阀值
    #  那拿makestep 1.0 3 来说,如果,系统时间与NTP 时间误差超过 1.0 秒 ,就触发强制同步,
    #  直接调整 系统时间与 与NTP源时间一致
    
    # Enable kernel synchronization of the real-time clock (RTC).
    rtcsync
    # 系统时间同步到RTC(硬件时钟)
    
    # Enable hardware timestamping on all interfaces that support it.
    #hwtimestamp *
    # 是用来提高时间的精确度的;时间从ntp传输过来到本机,
    #  系统运行的速度,CPU的轮询切换等,都会影响到时间同步的精度。
    #  所以,可以在数据包到网卡时,网卡会记录一个时间点,
    #  用来在系统执行时间同步时,用作参考(服务端与客户端都会产生时间戳)
    
    # Increase the minimum number of selectable sources required to adjust
    # the system clock.
    #minsources 2
    #
    # Allow NTP client access from local network.
    #allow 192.168.0.0/16
    # 局域网中允许指定网段可以同步时间
    
    # Serve time even if not synchronized to a time source.
    #local stratum 10
    # 不与其它主机时间同步
    
    # Specify file containing keys for NTP authentication.
    keyfile /etc/chrony.keys
    # NTP密钥文件
    
    # Get TAI-UTC offset and leap seconds from the system tz database.
    leapsectz right/UTC
    # 这个指令指定了系统 tz 数据库中的一个时区,chronyd 可以使用这个时区来确定下一个闰秒何时发生(也是提高时间精度用的)
    
    # Specify directory for log files.
    logdir /var/log/chrony
    # 日志文件
    
    # Select which information is logged.
    #log measurements statistics tracking
    # 选择要记录的日志信息
    

    经过一番烧脑,大致的理解了一下主配置文件里的参数,并做了一下注释,方便以后查阅

     
     
     

    chrony服务端的配置

    配置都很简单

    就更改一下域名服务器,与允许同步的网段就可以了,其余的基本都是默认;更改完后重启一下服务

    [root@localhost ~]# cat /etc/chrony.conf|grep -Ev "^#|^$"
    pool ntp1.aliyun.com iburst
    pool ntp.ntsc.ac.cn iburst
    pool ntp2.aliyun.com iburst
    driftfile /var/lib/chrony/drift
    makestep 1.0 3
    rtcsync
    allow 10.0.0.0/16
    local stratum 10
    logdir /var/log/chrony
    

    阿里云的时间服务器:
        ntp1.aliyun.com
        ntp2.aliyun.com
    中科院的时间服务器:
        ntp.ntsc.ac.cn

    查看源的IP、与活跃的源
    [root@localhost ~]# chronyc sources
    210 Number of sources = 4
    MS Name/IP address         Stratum Poll Reach LastRx Last sample               
    ===============================================================================
    ^- 120.25.115.20                 2   6     7     2  -1474us[  -71ms] +/- 5404us
    ^+ 114.118.7.161                 2   6     7     1  -1167us[  -70ms] +/-   59ms
    ^* 114.118.7.163                 2   6     7     1   +653us[  -68ms] +/-   40ms
    ^- 203.107.6.88                  2   6     7     2  -5938us[  -75ms] +/-   37ms
    
    [root@localhost ~]# chronyc activity
    200 OK
    4 sources online
    0 sources offline
    0 sources doing burst (return to online)
    0 sources doing burst (return to offline)
    

    显示目前同步的状态:

    [root@localhost ~]# chronyc tracking
    Reference ID    : 78197314 (120.25.115.20)
    Stratum         : 3
    Ref time (UTC)  : Fri Nov 06 18:03:47 2020
    System time     : 0.000141801 seconds slow of NTP time
    Last offset     : -0.000179653 seconds
    RMS offset      : 0.001172805 seconds
    Frequency       : 12.521 ppm fast
    Residual freq   : -1.504 ppm
    Skew            : 5.339 ppm
    Root delay      : 0.006741996 seconds
    Root dispersion : 0.002041594 seconds
    Update interval : 64.4 seconds
    Leap status     : Normal
    

    与外部互联网同步的时间间隔不能设置为小于64秒,否则会因为过于平凡与NTP服务器同步,而视为垃圾数据报被丢弃,这个需要注意一下
     
     

    客户端的配置

     
    就是添加一个ntp服务IP就行了,其余的基本不用动;重启服务器,并加入开机启动

    [root@centos-mini ~]# cat /etc/chrony.conf |grep -Ev "^#|^$"
    server 10.0.0.14 iburst
    stratumweight 0
    driftfile /var/lib/chrony/drift
    makestep 10 3
    rtcsync
    keyfile /etc/chrony.keys
    commandkey 1
    generatecommandkey
    noclientlog
    logchange 0.5
    logdir /var/log/chrony
    log measurements statistics tracking
    
    • 同样可以用上面的检测方法检测 自己配置ntp服务端是否可用;
    • 也可以更改客户端的时间,等待一段时间查看客户端时间是否恢复正常;chrony 不是像ntpdate那样瞬间跳跃式同步的,而是通过 加快/减慢 系统时钟,来慢慢与NTP时间靠拢,对于依赖时间类的服务器是非常有帮助的;

    相关文章

      网友评论

          本文标题:chrony时间同步服务

          本文链接:https://www.haomeiwen.com/subject/glfvvktx.html