chrony

CentOS8 时间同步服务

CentOS7之前的版本用的是ntpdate服务,之后用的是chrony服务
默认是安装的了

查看版本

[root@centos8 ~]#rpm -qi chrony
Name        : chrony
Version     : 3.5
Release     : 2.el8
Architecture: x86_64
Install Date: Sun 13 Jun 2021 12:40:02 PM CST
Group       : System Environment/Daemons
Size        : 537759
License     : GPLv2
Signature   : RSA/SHA256, Wed 03 Mar 2021 12:26:24 AM CST, Key ID 05b555b38483c65d
Source RPM  : chrony-3.5-2.el8.src.rpm
Build Date  : Tue 02 Mar 2021 03:04:32 PM CST
Build Host  : x86-01.mbox.centos.org
Relocations : (not relocatable)
Packager    : CentOS Buildsys <bugs@centos.org>
Vendor      : CentOS
URL         : https://chrony.tuxfamily.org
Summary     : An NTP client/server
Description :
chrony is a versatile implementation of the Network Time Protocol (NTP).
It can synchronise the system clock with NTP servers, reference clocks
(e.g. GPS receiver), and manual input using wristwatch and keyboard. It
can also operate as an NTPv4 (RFC 5905) server and peer to provide a time
service to other computers in the network.

#CentOS7查看chrony版本
[root@centos7 ~]#rpm -qi chrony
Name        : chrony
Version     : 2.1.1
Release     : 1.el7.centos
Architecture: x86_64
Install Date: 2019年11月25日 星期一 20时17分23秒
Group       : System Environment/Daemons
Size        : 479430
License     : GPLv2
Signature   : RSA/SHA256, 2015年11月25日 星期三 22时19分35秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : chrony-2.1.1-1.el7.centos.src.rpm
Build Date  : 2015年11月24日 星期二 05时36分12秒
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://chrony.tuxfamily.org
Summary     : An NTP client/server
Description :
A client/server for the Network Time Protocol, this program keeps your
computer's clock accurate. It was specially designed to support
systems with intermittent internet connections, but it also works well
in permanently connected environments. It can use also hardware reference
clocks, system real-time clock or manual input as time references.

服务端配置

主配置文件/etc/chrony
重点是:
allow 0.0.0.0/0
local stratum 10

简单解释:
允许NTP客户端从本地网络访问。
即使不能和一个远程服务器时间源同步,也能继续为其他客户端提供时间同步服务.
10 代表当前服务器设置为第10层, 按照 原子钟为第一层,互联网上的若干时间服务器为第N层,第10层足够表示当前主机所在的局域网
iburst:并行同步,加快同步速度

[root@centos8 ~]#vim /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.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst iburst
server ntp.ecslb.sjtu.edu.cn iburst
server time1.cloud.tencent.com iburst

# 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

# Enable kernel synchronization of the real-time clock (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.
#allow 192.168.0.0/16
allow 0.0.0.0/0

# Serve time even if not synchronized to a time source.

#local stratum 10
local stratum 10


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

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

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

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

重启服务

systemctl restart chronyd

查看chrony服务默认监听的UDP端口123是否打开

[root@centos8 ~]#ss -ntlu
Netid          State           Recv-Q          Send-Q                   Local Address:Port                      Peer Address:Port          Process
udp            UNCONN          0               0                              0.0.0.0:111                            0.0.0.0:*
udp            UNCONN          0               0                              0.0.0.0:123                            0.0.0.0:*
udp            UNCONN          0               0                              0.0.0.0:39087                          0.0.0.0:*
udp            UNCONN          0               0                              0.0.0.0:5353                           0.0.0.0:*
udp            UNCONN          0               0                            127.0.0.1:323                            0.0.0.0:*
udp            UNCONN          0               0                                 [::]:111                               [::]:*
udp            UNCONN          0               0                                 [::]:50298                             [::]:*
udp            UNCONN          0               0                                 [::]:5353                              [::]:*
udp            UNCONN          0               0                                [::1]:323                               [::]:*
tcp            LISTEN          0               128                          127.0.0.1:6010                           0.0.0.0:*
tcp            LISTEN          0               128                          127.0.0.1:6011                           0.0.0.0:*
tcp            LISTEN          0               128                          127.0.0.1:6012                           0.0.0.0:*
tcp            LISTEN          0               128                          127.0.0.1:6013                           0.0.0.0:*
tcp            LISTEN          0               128                            0.0.0.0:111                            0.0.0.0:*
tcp            LISTEN          0               128                            0.0.0.0:22                             0.0.0.0:*
tcp            LISTEN          0               5                            127.0.0.1:631                            0.0.0.0:*
tcp            LISTEN          0               128                              [::1]:6010                              [::]:*
tcp            LISTEN          0               128                              [::1]:6011                              [::]:*
tcp            LISTEN          0               128                              [::1]:6012                              [::]:*
tcp            LISTEN          0               128                              [::1]:6013                              [::]:*
tcp            LISTEN          0               128                               [::]:111                               [::]:*
tcp            LISTEN          0               128                                  *:80                                   *:*
tcp            LISTEN          0               128                               [::]:22                                [::]:*
tcp            LISTEN          0               5                                [::1]:631                               [::]:*

客户端测试

[root@centos7 ~]#date -s "-1 year"
2020年 08月 27日 星期四 20:09:11 CST
[root@centos7 ~]#date
2020年 08月 27日 星期四 20:09:17 CST
[root@centos7 ~]#ntpdate 10.0.0.5
27 Aug 16:09:37 ntpdate[4935]: step time server 10.0.0.5 offset 31521600.000748 sec
[root@centos7 ~]#date
2021年 08月 27日 星期五 16:09:40 CST

#更改配置文件使客户端时间服务默认指向10.0.0.5
[root@centos7 ~]#vim /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 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 10.0.0.5 iburst

重启服务

systemctl restart chronyd

测试

[root@centos7 ~]#date -s "-1 year"
2020年 08月 27日 星期四 16:17:28 CST

#查看同步详细信息
[root@centos7 ~]#chronyc sources -v
210 Number of sources = 1

  .-- 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
===============================================================================
^? 10.0.0.5                      3   6   377    17   -8760h[ -8760h] +/-   17ms

#同步成功
[root@centos7 ~]#chronyc sources -v
210 Number of sources = 1

  .-- 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
===============================================================================
^* 10.0.0.5                      3   6   377     1    +76us[ -8760h] +/-   16ms

注:使用默认同步需要稍微等待几分钟,立即同步可以用ntpdate 10.0.0.5 或者重启chrony服务 systemctl restart chronyd

国内常见的NTP时间服务器地址

清华大学
https://tuna.moe/help/ntp/

ntp.tuna.tsinghua.edu.cn

上海交通大学

ntp.sjtu.edu.cn
ntp.ecslb.sjtu.edu.cn

腾讯

time1.cloud.tencent.com
time2.cloud.tencent.com
time3.cloud.tencent.com
time4.cloud.tencent.com
time5.cloud.tencent.com

微软
windows自带

time.windows.com

配置了chrony服务时间还是无法同步

2022年2月8日
问题:配置了chrony服务后,时间不对

[root@localhost yum.repos.d]#date
Tue Feb  8 07:22:24 GMT 2022
[root@localhost yum.repos.d]#systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) (thawing) since Tue 2022-02-08 07:22:19 GMT; 10s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 18051 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited, status=0/SUCCESS)
  Process: 18060 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 18056 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 18058 (chronyd)
    Tasks: 1 (limit: 11220)
   Memory: 2.9M
   CGroup: /system.slice/chronyd.service
           └─18058 /usr/sbin/chronyd

Feb 08 07:22:19 localhost.localdomain systemd[1]: Starting NTP client/server...
Feb 08 07:22:19 localhost.localdomain chronyd[18058]: chronyd version 4.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +>
Feb 08 07:22:19 localhost.localdomain chronyd[18058]: Frequency 6.525 +/- 1.253 ppm read from /var/lib/chrony/drift
Feb 08 07:22:19 localhost.localdomain chronyd[18058]: Using right/UTC timezone to obtain leap second data
Feb 08 07:22:19 localhost.localdomain systemd[1]: Started NTP client/server.
Feb 08 07:22:25 localhost.localdomain chronyd[18058]: Selected source 139.199.215.251 (ntp.ecslb.sjtu.edu.cn)
Feb 08 07:22:25 localhost.localdomain chronyd[18058]: System clock TAI offset set to 37 seconds

排查思路:

  1. 查看网络是否通畅
  2. 查看时区配置是否正确
#查看时区配置
[root@localhost yum.repos.d]#timedatectl
               Local time: Tue 2022-02-08 07:42:06 GMT
           Universal time: Tue 2022-02-08 07:42:06 UTC
                 RTC time: Tue 2022-02-08 07:42:05
                Time zone: Africa/Abidjan (GMT, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: yes

#发现并不是常用的亚洲上海时区
#查看所有的时区列表中是否有亚洲上海时区
[root@localhost yum.repos.d]#timedatectl list-timezones | grep -E "Asia/S.*"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

#配置时区为亚洲上海时区
[root@localhost yum.repos.d]#timedatectl set-timezone Asia/Shanghai

#强制同步系统时钟
[root@localhost yum.repos.d]#chronyc -a makestep
200 OK

#再次查看时间 正常
[root@localhost yum.repos.d]#date
Tue Feb  8 15:44:55 CST 2022

使用timedatectl命令查看时区时候的提示

开机时,读取主板时间即RTC,
系统作业时间Local Time,即本地中央标准时间(Centtral Standard Time CST)有同步ntp,就正常同步ntp,如果没有,CST时间会一直跑,可能会和RTC有时间差。
开机时,作业系统将本地时间CST写入硬件时间RTC中
为了跨时区作用通常RTC时间要等于UTC(Coordinated Universal Time,即格林威治时间)

时钟有两种,一种是本地机器的硬件时钟RTC(Read Time Clock)和内核时钟

内核时钟由操作系统维护,启动时,读取硬件时钟并用于设置系统时钟。从那时起,系统时钟用于跟踪时间

#timedatectl显示当前时间,包括系统系统时钟和硬件时钟,还显示系统当前配置的时区
[root@localhost ~]#timedatectl
               Local time: Tue 2022-02-08 15:49:02 CST
           Universal time: Tue 2022-02-08 07:49:02 UTC
                 RTC time: Tue 2022-02-08 15:49:02
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: yes

Warning: The system is configured to read the RTC time in the local time zone.
         This mode cannot 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'.

#大概意思是本地的RTC时钟和系统时钟有冲突了,只能开启一个。

#按照提示输入命令
[root@localhost ~]#timedatectl set-local-rtc 0

#不显示警告了
[root@localhost ~]#timedatectl
               Local time: Tue 2022-02-08 15:49:38 CST
           Universal time: Tue 2022-02-08 07:49:38 UTC
                 RTC time: Tue 2022-02-08 07:49:37
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

关于systemctl程序状态命令显示(thawing)问题
描述:

systemctl程序状态命令的输出结果显示为解冻

当重新加载服务时,命令的输出结果显示状态为active(thawing)而不是active(running)
此问题知识显示问题,可以忽略

[root@localhost ~]#systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) (thawing) since Tue 2022-02-08 15:22:19 CST; 4h 0min ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 18051 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited, status=0/SUCCESS)
  Process: 18060 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 18056 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 18058 (chronyd)
    Tasks: 1 (limit: 11220)
   Memory: 2.6M
   CGroup: /system.slice/chronyd.service
           └─18058 /usr/sbin/chronyd

Feb 08 15:22:19 localhost.localdomain systemd[1]: Starting NTP client/server...
Feb 08 15:22:19 localhost.localdomain chronyd[18058]: chronyd version 4.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS>
Feb 08 15:22:19 localhost.localdomain chronyd[18058]: Frequency 6.525 +/- 1.253 ppm read from /var/lib/chrony/drift
Feb 08 15:22:19 localhost.localdomain chronyd[18058]: Using right/UTC timezone to obtain leap second data
Feb 08 15:22:19 localhost.localdomain systemd[1]: Started NTP client/server.
Feb 08 15:22:25 localhost.localdomain chronyd[18058]: Selected source 139.199.215.251 (ntp.ecslb.sjtu.edu.cn)
Feb 08 15:22:25 localhost.localdomain chronyd[18058]: System clock TAI offset set to 37 seconds
Feb 08 15:27:51 localhost.localdomain chronyd[18058]: Selected source 111.230.189.174 (2.centos.pool.ntp.org)
Feb 08 15:44:52 localhost.localdomain chronyd[18058]: System clock was stepped by -0.000106 seconds

查找到dell的售后有一个说明

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值