Linux 中同步时间工具 Chrony 使用

发布时间: 更新时间: 总字数:2240 阅读时间:5m 作者: IP上海 分享 网址

ChronyLinux 系统中 NTP 的替代产品,实现时间同步功能。本文介绍如何使用 chrony 服务和常见命令,以及 Linux 系统中时间问题排查相关命令。

简介

Chrony是一个开源的自由软件,它能帮助你保持系统时钟与时钟服务器(NTP)同步,因此让你的时间保持精确。它由两个程序组成,分别是chronyd和chronyc。chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

在像CentOS7之类基于RHEL的操作系统上,已经默认安装有Chrony。

Chrony配置

当Chrony启动时,它会读取/etc/chrony.conf配置文件中的设置。CentOS 7操作系统上最重要的设置有:

server - 该参数可以多次用于添加时钟服务器,必须以server格式使用。一般而言,你想添加多少服务器,就可以添加多少服务器。

server 0.centos.pool.ntp.org
server 3.europe.pool.ntp.org

# stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多
# 少距离到同步距离。默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级。
stratumweight

# chronyd程序的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,
# 将它记录到一个文件中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能
# 的话,会从时钟服务器获得较好的估值。
driftfile

# rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
rtcsync

# 这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器。
allow / deny
allow 192.168.4.5
deny 192.168/16
# 跟上面相类似,只是你可以指定哪个IP地址或哪台主机可以通过chronyd使用控制命令
cmdallow / cmddeny

# 该指令允许你限制chronyd监听哪个网络接口的命令包(由chronyc执行)。
# 该指令通过cmddeny机制提供了一个除上述限制以外可用的额外的访问控制等级。
bindcmdaddress

bindcmdaddress 127.0.0.1
bindcmdaddress ::1
# 通常,chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。
# 在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。
# 该指令强制chronyd在调整期大于某个阀值时步进调整系统时钟,
# 但只有在因为chronyd启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效。
makestep

Server 端配置

diff /etc/chrony.conf  /etc/chrony.conf.bak
3c3,6
< server 127.0.0.1 iburst
---
> 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
24,25d26
< allow 172.20.0.0/16
28c29
< local stratum 10
---
> #local stratum 10

Client 端配置

if [ -f /etc/chrony.conf ]; then mv /etc/chrony.conf /etc/chrony.conf.$(date +%s) ; fi
rpm -qa | grep chrony && yum reinstall chrony -y
rpm -qa | grep chrony || yum install chrony -y
sed -i "s/0.centos.pool.ntp.org/<ntp ip>/g" /etc/chrony.conf
sed -i "/.centos.pool.ntp.org/d" /etc/chrony.conf
ntpdate <ntp ip>
systemctl start chronyd
systemctl enable chronyd

使用 chronyc

你也可以通过运行chronyc命令来修改设置,命令如下:

accheck - 检查NTP访问是否对特定主机可用

[root@xiexianbin_cn ~]# chronyc accheck 0.centos.pool.ntp.org
209 Access denied

activity - 该命令会显示有多少NTP源在线/离线

[root@xiexianbin_cn ~]# chronyc activity
200 OK
4 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
[root@xiexianbin_cn ~]#
  • add server - 手动添加一台新的NTP服务器。
  • clients - 在客户端报告已访问到服务器
  • delete - 手动移除NTP服务器或对等服务器
  • settime - 手动设置守护进程时间
  • tracking - 显示系统时间信息
[root@xiexianbin_cn ~]# chronyc tracking
Reference ID    : CA760182 (202.118.1.130)
Stratum         : 3
Ref time (UTC)  : Mon Feb 05 09:09:03 2018
System time     : 0.000001192 seconds slow of NTP time
Last offset     : +0.000242573 seconds
RMS offset      : 0.002258565 seconds
Frequency       : 23.707 ppm slow
Residual freq   : +6.422 ppm
Skew            : 13.612 ppm
Root delay      : 0.048384637 seconds
Root dispersion : 0.000708150 seconds
Update interval : 4.2 seconds
Leap status     : Normal
[root@xiexianbin_cn ~]#
[root@xiexianbin_cn ~]# chronyc -n tracking
Reference ID    : CA760182 (202.118.1.130)
Stratum         : 4
Ref time (UTC)  : Mon May 21 11:08:53 2018
System time     : 6.657690525 seconds fast of NTP time
Last offset     : -0.000038801 seconds
RMS offset      : 0.000038801 seconds
Frequency       : 0.000 ppm fast
Residual freq   : +41.559 ppm
Skew            : 1000000.000 ppm
Root delay      : 0.038845 seconds
Root dispersion : 3.825408 seconds
Update interval : 2.2 seconds
Leap status     : Normal
[root@xiexianbin_cn ~]#
  • 检查当前同步的时钟源
[root@xiexianbin_cn ~]# chronyc sources
210 Number of sources = 14
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 120.25.115.20                 2  10   377  1133   -305us[ -328us] +/- 7979us

ntp命令

查看与服务的时间差

ntpq -p

# timedatectl show-timesync --all

同步时间

ntpdate <ntp ip>
ntpdate -dv <ntp ip>

命令

  • hardware clock是保存在主板芯片里的时间,和操作系统没有关系,通常都是UTC的
  • system time是操作系统负责的,当OS启动后,system time就和hardware clock完全独立了,就算system time被修改,hardware clock不会跟着被改,system time也是UTC的
  • local time是针对当前时区的换算后的时间
  • hardware clock由于是在主板上,有时不准,所以需要NTP

hwclock

  • hwclock -c 显示hardware和system time之间的区别
[root@xiexianbin_cn ~]# hwclock -c
hw-time      system-time         freq-offset-ppm   tick
1517822028   1517822029.474355
1517822038   1517822039.476043               169      2
1517822048   1517822049.476921               128      1

重新启动系统,硬件时间会读取系统时间,实现同步,但是在不重新启动的时候,需要用hwclock命令实现同步。

hwclock --hctosys
hwclock --systohc

注意:

  • hc代表硬件时间
  • sys代表系统时间

timedatectl

timedatectl 可以操作时间的方方面面,是统一的时间操作入口:

[root@xiexianbin_cn ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled
[root@xiexianbin_cn ~]#
[root@xiexianbin_cn ~]# timedatectl
      Local time: Mon 2018-02-05 17:13:01 CST
  Universal time: Mon 2018-02-05 09:13:01 UTC
        RTC time: Mon 2018-02-05 17:13:00
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
[root@xiexianbin_cn ~]#

时区的设置的方法

  • tzselect
  • ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

修改系统时间

date -s "20190303 14:00:00"
date -s "2019-03-03 14:00:00"
date -s "2019/03/03 14:00:00"

修改硬件时间

hwclock
hwclock -set -date="03/03/19 14:00:00" (月/日/年 时:分:秒)

类似的工具

  • SCTP(Stream Control Transmission Protocol, 流控制传输协议) 是一个运输层协议,确保数据的可靠、按顺序传输
  • NTP
  • systemd-timesyncd 是一个用于跨网络同步系统时钟的守护服务,它实现了一个简单的 SNTP 客户端,比 NTP 简单,一般在 /etc/systemd/timesyncd.conf 中添加如下内容即可
[Time]
NTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 0.fr.pool.ntp.org
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数