设为首页 收藏本站
查看: 1770|回复: 0

[经验分享] CentOS 7.4 中时间服务器同步

[复制链接]

尚未签到

发表于 2018-4-25 10:07:57 | 显示全部楼层 |阅读模式
Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确。它由两个程序组成:chronyd和chronyc。  
chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
  
chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。
  
Chrony时间同步程序特点:
  
准确的时间同步:
  
Chrony 是网络时间协议的另一种实现,与网络时间协议后台程序(ntpd)不同,它可以更快地且准确的同步系统时钟。
  
Chrony 的优势包括:
  
更快的同步只需要数分钟而非数小时时间,从而最大程度的减少时间和频率误差,这对于并非全天运行的台式计算机或系统而言非常有用。
  
能够更好的响应时间频率的快速变化,这对于具备不稳定时钟的虚拟机或导致时钟频率反生变化的节能技术而言非常有用。
  
在初始同步后,它并不会停止时钟,以防对需要系统时间保持单调的程序造成影响。
  
在应对临时非对称延迟时,(例如,大规模下载造成链接饱和时)提供了更好的稳定性。
  
无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟。
  
一、配置时间服务器
  
     1、软件部署
  
[root@ntp ~]# cat /etc/redhat-release
  
CentOS Linux release 7.4.1708 (Core)
  
[root@ntp ~]# yum -y install chrony
  
      2、配置文件
  
[root@ntp ~]# 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).
  
//该参数可以多次用于添加时钟服务器,必须以"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
  
#server  edu.ntp.org.cn iburst
  
#server pool.ntp.org iburstserver   news.neu.edu.cn iburst     //时间同步设置(外网)
  
server   202.120.2.101 iburst     //上海交通大学网络中心NTP服务器地址,也可以写成域名:ntp.sjtu.edu.cn# Record the rate at which the system clock gains/losses time.
  
   //chronyd程序的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能的话,会从时钟服务器获得较好的估值。
  
   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.
  
  //通常,chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时步进调整系统时钟,但只有在因为chronyd启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效。
  
makestep 1.0 3
  

  
# Enable kernel synchronization of the real-time clock (RTC).
  
  //rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)
  
rtcsync
  

  
# Enable hardware timestamping on all interfaces that support it.
  
#hwtimestamp *
  

  
# Increase the minimum number of selectable sources required to adjust
  
# the system clock.
  
#minsources 2
  

  
# Allow NTP client access from local network.
  
//这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器。allow 192.168.10.0/24    //允许同步的网段# Serve time even if not synchronized to a time source.
  
local stratum 10
  

  
# Specify file containing keys for NTP authentication.
  
#keyfile /etc/chrony.keys
  

  
# Specify directory for log files.
  
logdir /var/log/chrony
  

  
# Select which information is logged.
  
#log measurements statistics tracking
  

  
     3、启动服务
  
[root@ntp ~]# systemctl start chronyd.service    //启动服务[root@ntp ~]# systemctl enable chronyd.service   //设置开机自启动Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
  
[root@ntp ~]# systemctl status chronyd.service● chronyd.service - NTP client/server         //查看当前状态
  
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
  
   Active: active (running) since Tue 2018-01-02 18:57:34 CST; 2h 1min ago
  
     Docs: man:chronyd(8)
  
           man:chrony.conf(5)
  
Main PID: 1695 (chronyd)
  
   CGroup: /system.slice/chronyd.service
  
           └─1695 /usr/sbin/chronyd
  

  
Jan 02 18:57:34 director.contoso.com systemd[1]: Starting NTP client/server...
  
Jan 02 18:57:34 director.contoso.com chronyd[1695]: chronyd version 3.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH ...+DEBUG)
  
Jan 02 18:57:34 director.contoso.com systemd[1]: Started NTP client/server.
  
Jan 02 18:57:38 director.contoso.com chronyd[1695]: Selected source 192.168.10.8
  
Hint: Some lines were ellipsized, use -l to show in full.
  
[root@ntp ~]# ss -tulp | grep chronyd
  
udp    UNCONN     0      0       *:ntp                   *:*                     users:(("chronyd",pid=1695,fd=3))
  
udp    UNCONN     0      0      127.0.0.1:323                   *:*                     users:(("chronyd",pid=1695,fd=1))
  
udp    UNCONN     0      0         ::1:323                  :::*                     users:(("chronyd",pid=1695,fd=2))
  

  
  3、验证服务
  
1 [root@ntp ~]# chronyc sources -v                //查看时间同步源: 2 210 Number of sources = 1 3  4   .-- Source mode  '^' = server, '=' = peer, '#' = local clock. 5  / .- Source state '*' = current synced, '+' = combined , '-' = not combined, 6 | /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable. 7 ||                                                 .- xxxx [ yyyy ] +/- zzzz 8 ||      Reachability register (octal) -.           |  xxxx = adjusted offset, 9 ||      Log2(Polling interval) --.      |          |  yyyy = measured offset,10 ||                                \     |          |  zzzz = estimated error.11 ||                                 |    |           \12 MS Name/IP address         Stratum Poll Reach LastRx Last sample
  
13 ===============================================================================14 ^? ns.bjnet-pku.edu.cn           0   6     0     -     +0ns[   +0ns] +/-    0ns15 [root@ntp ~]# ping -c 2 s2m.time.edu.cn16 PING s2m.time.edu.cn (202.112.7.13) 56(84) bytes of data.17 64 bytes from ns.pku.cn (202.112.7.13): icmp_seq=1 ttl=237 time=36.9 ms18 64 bytes from ns.pku.cn (202.112.7.13): icmp_seq=2 ttl=237 time=36.9 ms20 --- s2m.time.edu.cn ping statistics ---21 2 packets transmitted, 2 received, 0% packet loss, time 1001ms22 rtt min/avg/max/mdev = 36.927/36.928/36.929/0.001 ms
  
[root@ntp ~]# chronyc sourcestats -v     // 查看时间同步源状态
  
210 Number of sources = 1
  
                             .- Number of sample points in measurement set.
  
                            /    .- Number of residual runs with same sign.
  
                           |    /    .- Length of measurement set (time).
  
                           |   |    /      .- Est. clock freq error (ppm).
  
                           |   |   |      /           .- Est. error in freq.
  
                           |   |   |     |           /         .- Est. offset.
  
                           |   |   |     |          |          |   On the -.
  
                           |   |   |     |          |          |   samples. \
  
                           |   |   |     |          |          |             |
  
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
  
==============================================================================
  
202.120.2.101.dns.sjtu.e>   0   0     0     +0.000   2000.000     +0ns  4000ms
  
news.neu.edu.cn                   0   0     0     +0.000   2000.000     +0ns  4000ms
  

  
二、其它客户端配置
  
    1、软件部署
  
[root@CentOS7 ~]# yum -y install chrony
  

  
     2、配置文件
  
[root@CentOS7 ~]# egrep -v "^#|^$" /etc/chrony.conf
  
server 192.168.10.8     iburst      //同步时间服务器stratumweight 0driftfile /var/lib/chrony/drift
  
rtcsync
  
makestep 10 3bindcmdaddress 127.0.0.1bindcmdaddress ::1keyfile /etc/chrony.keys
  
commandkey 1generatecommandkey
  
noclientlog
  
logchange 0.5logdir /var/log/chrony
  

  
     3、启动服务
  
[root@CentOS7 ~]# systemctl start chronyd.service[root@CentOS7 ~]# systemctl enable chronyd.serviceCreated symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
  

  
    4、验证服务
  
[root@CentOS7 ~]# ntpdate  192.168.10.8
  
2 Jan 21:39:34 ntpdate[43563]: adjust time server 192.168.10.8 offset -0.000041 sec
  
[root@CentOS7 ~]# chronyc sourcestats -v210 Number of sources = 1
  
                             .- Number of sample points in measurement set.                            /    .- Number of residual runs with same sign.                           |    /    .- Length of measurement set (time).                           |   |    /      .- Est. clock freq error (ppm).                           |   |   |      /           .- Est. error in freq.                           |   |   |     |           /         .- Est. offset.                           |   |   |     |          |          |   On the -.                           |   |   |     |          |          |   samples. \                           |   |   |     |          |          |             |Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev==============================================================================192.168.10.8               14   8  118m     +0.002      0.023  +1305ns    48us
  
[root@CentOS7 ~]#
  

  
     4、设置任务计划
  
[root@CentOS7 ~]# crontab -e   //编辑crontab文件[root@CentOS7 ~]# crontab -l   //查看任务计划#synchronization time          //每隔五分钟进行同步一次*/5 * * * * /usr/sbin/ntpdate 192.168.10.8 > /dev/null 2>&1
  

  
三、相关概念及命令介绍
  
     1、GMT、UTC、CST、DST 时间
  
UTC  整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。
  
GMT  格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)
  
CST  中国标准时间 (China Standard Time)
  
GMT + 8 = UTC + 8 = CST
  
DST夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用)
  

  
     2、硬件时间和系统时间
  
硬件时间
  
      RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。
  
系统时间
  
      一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。
  

  
     3、timedatectl 命令
  
显示系统的当前时间和日间
  
timedatectl
  
# timedatectl status
  
# 两条命令效果等同
  
显示系统的当前时间和日间
  
timedatectl set-time "YYYY-MM-DD HH:MM:SS"timedatectl set-time "YYYY-MM-DD"timedatectl set-time "HH:MM:SS"
  
查看所有可用的时区
  
timedatectl list-timezones
  
# 亚洲
  
timedatectl list-timezones |  grep  -E "Asia/S.*"
  
设置时区
  
timedatectl set-timezone Asia/Shanghai
  
设置硬件时间
  
# 硬件时间默认为UTC
  
timedatectl set-local-rtc 1# hwclock --systohc --localtime
  
# 两条命令效果等同
  
启用时间同步
  
timedatectl set-ntp yes
  
# yes或no; 1或0也可以
  
本文出自https://www.cnblogs.com/cloudos/p/NTP.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-451729-1-1.html 上篇帖子: centos6.6_X64安装oracle10G 下篇帖子: centos6 docker1.7 存储方式修改
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表