Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确。

它由两个程序组成:chronydchronyc

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 iburst
server  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、验证服务

 [root@ntp ~]# chronyc sources -v                //查看时间同步源:
Number of sources = .-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- 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
===============================================================================
^? ns.bjnet-pku.edu.cn - +0ns[ +0ns] +/- 0ns
[root@ntp ~]# ping -c s2m.time.edu.cn
PING s2m.time.edu.cn (202.112.7.13) () bytes of data.
bytes from ns.pku.cn (202.112.7.13): icmp_seq= ttl= time=36.9 ms
bytes from ns.pku.cn (202.112.7.13): icmp_seq= ttl= time=36.9 ms
--- s2m.time.edu.cn ping statistics ---
packets transmitted, received, % packet loss, time 1001ms
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
driftfile /var/lib/chrony/drift
rtcsync
makestep
bindcmdaddress 127.0.0.1
bindcmdaddress ::
keyfile /etc/chrony.keys
commandkey
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

  3、启动服务

[root@CentOS7 ~]# systemctl start chronyd.service
[root@CentOS7 ~]# systemctl enable chronyd.service
Created 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
Jan :: ntpdate[]: adjust time server 192.168.10.8 offset -0.000041 sec
[root@CentOS7 ~]# chronyc sourcestats -v
Number of sources =
.- 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 118m +0.002 0.023 +1305ns 48us
[root@CentOS7 ~]#

 

     4、设置任务计划

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

 

三、相关概念及命令介绍

     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
    # hwclock --systohc --localtime
    # 两条命令效果等同
  • 启用时间同步
    timedatectl set-ntp yes
    # yes或no; 1或0也可以

CentOS7.4 chrony时间同步服务器部署(替代NTPD)的更多相关文章

  1. centos7搭建ntp时间同步服务器chrony服务

    centos7搭建ntp时间同步服务器chrony服务 前言: 在centos6的时候我们基本使用的是ntp服务用来做时间同步,但是在centos7后推荐是chrony作为时间同步器的服务端使用, ...

  2. CentOS7.x搭建时间同步服务器

    关于chrony Chrony是一个开源的自由软件,像CentOS7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP) ...

  3. suse 12 部署chrony时间同步服务器

    文章目录 1.ntp和chrony的区别 1.1.关于chrony 1.2.chronyd的优势 2.环境介绍 3.部署chrony 4.配置chrony 4.1.配置文件解析 4.2.查看chron ...

  4. centos7下配置时间同步服务器

    同网段20几台服务器: 其中有一组mysql 集群中 互为主从 选一台mysql master 作为时间同步的服务器,这样做的好处以便于这台down了 另一个与他互为主从的master 继续提供时间同 ...

  5. 分布式集群中,设定时间同步服务器,以及ntpd与ntpdate的区别

    什么时候配置时间同步? 当分布式集群配置好了以后,马上配置的是SSH无密钥配置,然后就是配置时间同步. 时间同步在集群中特别重要. 一:时间同步 1.时间同步 集群中必须有一个统一的时间 如果是内网, ...

  6. chrony 时间同步服务器

    这篇文章也不错: https://www.zfl9.com/chrony.html 简介 chrony是网络时间协议(NTP)的多用途实现.它可以使系统时钟与NTP服务器.参考时钟(例如GPS接收器) ...

  7. 030 分布式集群中,设定时间同步服务器,以及ntpd与ntpdate的区别

    什么时候配置时间同步? 当分布式集群配置好了以后,马上配置的是SSH无密钥配置,然后就是配置时间同步. 时间同步在集群中特别重要. 一:时间同步 1.时间同步 集群中必须有一个统一的时间 如果是内网, ...

  8. Linux集群部署自定义时间同步服务器(ntpd)

    Linux集群部署自定义时间同步服务器(ntpd) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 身为一名运维人员,在搭建集群的时候,第一步需要做的就是同步每个机器的时间,尤其是在 ...

  9. CentOS7 使用chrony搭建集群中的时间同步服务

    一.集群环境: 系统:CentOS7-minimal 集群中的两台主机ip:10.132.226.103/24  10.132.226.104/24 二.CentOS7中时间相关命令timedatec ...

随机推荐

  1. 十、Hadoop学习笔记————Hive与Hbase以及RDBMS(关系型数据库)的关系

    Hive目的是为了简化MapReduce编程 实际应用中,Hive与Hbase不经常链接

  2. JAVA 中LinkedHashMap要点记录

    JAVA 中LinkedHashMap要点记录 构造函数中可能出现的几个参数说明如下: 1.initialCapacity 初始容量大小,使用无参构造方法时,此值默认是16 2.loadFactor ...

  3. 关于苹果APP的上架整理

    由于苹果的机制,在非越狱机器上安装应用必须通过官方的App Store,开发者开发好应用后上传App Store,也需要通过审核等环节.AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传A ...

  4. 【原创】用python写的一个监测本地进程CPU占用的程序

    #coding=utf-8import psutilimport sysimport timetry:#输入需要监测的进程PID PID = raw_input('ProcessPID: ') def ...

  5. springMVC+commons-fileupload上传文件大小限制异常

    异常信息: 严重: Servlet.service() for servlet [suibian] in context with path [/SpringMvcDemo3] threw excep ...

  6. 实践作业1:测试管理工具实践 Day4

    由小组吴辉同学和王俊杰同学负责撰写使用手册,详细记录了环境配置完整过程,以及从软件登陆开始,创建一个测试计划,创建新版本,创建测试用例集,分配测试用例到测试计划,关联到测试用例这一系列完整过程. 刘思 ...

  7. codeforces #447 894A QAQ 894B Ralph And His Magic Field 894C Marco and GCD Sequence

    A.QAQ 题目大意:从给定的字符串中找出QAQ的个数,三个字母的位置可以不连续 思路:暴力求解,先找到A的位置,往前扫,往后扫寻找Q的个数q1,q2,然 后相乘得到q1*q2,这就是这个A能够找到的 ...

  8. PHP能引起安全的函数

    php中需要禁用以下函数来提高安全性 打开php.ini  找到 disable_functions .然后禁用以下函数 disable_functions = pcntl_alarm, pcntl_ ...

  9. HTTP协议之URL

    1.什么是URL URL的全称是Uniform Resoure Locator,统一资源定位器.URL是浏览器寻找信息时所需的资源位置.当一个人将浏览器指向一个URL,浏览器就会在幕后发送适当的协议报 ...

  10. 写出优雅又地道的pythonic代码(转自网络)

    本文是Raymond Hettinger在2013年美国PyCon演讲的笔记(视频, 幻灯片). 示例代码和引用的语录都来自Raymond的演讲.这是我按我的理解整理出来的,希望你们理解起来跟我一样顺 ...