chrony时间同步软件介绍

本文分享自天翼云开发者社区《chrony时间同步软件介绍》,作者:刘****苏

chrony是网络时间协议NTP的通用实现,它可以将系统时钟和`NTP服务器同步。它支持在各种条件下包括间歇性的网络连接、严重阻塞的网络、不断变化的温度以及支持不连续的运行并且可以运行于虚机上。

本文介绍chrony工具的安装和使用,包括两个节点:一个作为时间服务器,另一个作为客户端。

执行如下命令来安装chrony:

1[root@kvm ~]# rpm -ql chrony 2/etc/NetworkManager/dispatcher.d/20-chrony-dhcp 3/etc/NetworkManager/dispatcher.d/20-chrony-onoffline 4/etc/chrony.conf 5/etc/chrony.keys 6/etc/dhcp/dhclient.d/chrony.sh 7/etc/logrotate.d/chrony 8/etc/sysconfig/chronyd 9/usr/bin/chronyc 10/usr/lib/.build-id 11/usr/lib/.build-id/38 12/usr/lib/.build-id/38/1d3a324ce66865ba879dfa43d49e0e0c1e2c41 13/usr/lib/.build-id/43 14/usr/lib/.build-id/43/444c816fcf0802c43011f282afaf9feded4c56 15/usr/lib/systemd/ntp-units.d/50-chronyd.list 16/usr/lib/systemd/system/chrony-dnssrv@.service 17/usr/lib/systemd/system/chrony-dnssrv@.timer 18/usr/lib/systemd/system/chrony-wait.service 19/usr/lib/systemd/system/chronyd.service 20/usr/libexec/chrony-helper 21/usr/sbin/chronyd 22/usr/share/doc/chrony 23/usr/share/doc/chrony/FAQ 24/usr/share/doc/chrony/NEWS 25/usr/share/doc/chrony/README 26/usr/share/doc/chrony/ntp2chrony.py 27/usr/share/licenses/chrony 28/usr/share/licenses/chrony/COPYING 29/usr/share/man/man1/chronyc.1.gz 30/usr/share/man/man5/chrony.conf.5.gz 31/usr/share/man/man8/chronyd.8.gz 32/var/lib/chrony 33/var/lib/chrony/drift 34/var/lib/chrony/rtc 35/var/log/chrony

如下所示为配置文件:

1[root@kvm ~]# cat /etc/chrony.conf 2pool 2.centos.pool.ntp.org iburst 34# Record the rate at which the system clock gains/losses time. 5driftfile /var/lib/chrony/drift 67# Allow the system clock to be stepped in the first three updates 8# if its offset is larger than 1 second. 9makestep 1.0 3 1011# Enable kernel synchronization of the real-time clock (RTC). 12rtcsync 1314# Enable hardware timestamping on all interfaces that support it. 15#hwtimestamp * 1617# Increase the minimum number of selectable sources required to adjust 18# the system clock. 19#minsources 2 2021# Allow NTP client access from local network. 22allow 0/0 2324# Serve time even if not synchronized to a time source. 25local stratum 10 2627# Specify file containing keys for NTP authentication. 28keyfile /etc/chrony.keys 2930# Get TAI-UTC offset and leap seconds from the system tz database. 31leapsectz right/UTC 3233# Specify directory for log files. 34logdir /var/log/chrony 3536# Select which information is logged. 37#log measurements statistics tracking

其中: pool:为外部时钟源地址 rtcsync:开启硬件时钟的同步机制,linux内核每11分钟同步一次系统时钟到硬件时钟 local stratum 10:当外部时钟源不可用的时候,以本机时钟为准继续提供服务 allow 0/0:允许的客户端的cidr 启动服务后查看状态:

1[root@kvm ~]# systemctl start chronyd 2[root@kvm ~]# 3[root@kvm ~]# 4[root@kvm ~]# systemctl status chronyd 5● chronyd.service - NTP client/server 6 Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) 7 Active: active (running) since Fri 2023-09-08 19:02:50 CST; 2min 43s left 8 Docs: man:chronyd(8) 9 man:chrony.conf(5) 10 Process: 11542 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited, status=0/SUCCESS) 11 Process: 11552 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) 12 Process: 11547 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) 13 Main PID: 11550 (chronyd) 14 Tasks: 1 (limit: 52427) 15 Memory: 924.0K 16 CGroup: /system.slice/chronyd.service 17 └─11550 /usr/sbin/chronyd 1819Sep 08 19:02:50 kvm chronyd[11550]: chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG) 20Sep 08 19:02:50 kvm chronyd[11550]: Frequency 1.633 +/- 0.046 ppm read from /var/lib/chrony/drift 21Sep 08 19:02:50 kvm chronyd[11550]: Using right/UTC timezone to obtain leap second data 22Sep 08 19:02:50 kvm systemd[1]: Started NTP client/server. 23Sep 08 19:02:55 kvm chronyd[11550]: Selected source 36.110.233.85 (2.centos.pool.ntp.org) 24Sep 08 19:02:55 kvm chronyd[11550]: System clock wrong by -791.549636 seconds 25Sep 08 18:49:44 kvm chronyd[11550]: System clock was stepped by -791.549636 seconds 26Sep 08 18:49:44 kvm chronyd[11550]: System clock TAI offset set to 37 seconds 27Sep 08 18:50:49 kvm chronyd[11550]: Selected source 119.28.206.193 (2.centos.pool.ntp.org) 28Sep 08 18:54:07 kvm chronyd[11550]: Source 81.16.177.123 replaced with 78.46.102.180 (2.centos.pool.ntp.org) 29[root@kvm ~]# 30[root@kvm ~]# netstat -tunlp |grep chrony 31udp 0 0 0.0.0.0:123 0.0.0.0:* 11550/chronyd 32udp 0 0 127.0.0.1:323 0.0.0.0:* 11550/chronyd 33udp6 0 0 ::1:323 :::* 11550/chronyd 34[root@kvm ~]# 35[root@kvm ~]# chronyc sources -v 3637 .-- Source mode '^' = server, '=' = peer, '#' = local clock. 38 / .- Source state '*' = current best, '+' = combined, '-' = not combined, 39| / 'x' = may be in error, '~' = too variable, '?' = unusable. 40|| .- xxxx [ yyyy ] +/- zzzz 41|| Reachability register (octal) -. | xxxx = adjusted offset, 42|| Log2(Polling interval) --. | | yyyy = measured offset, 43|| \ | | zzzz = estimated error. 44|| | | \ 45MS Name/IP address Stratum Poll Reach LastRx Last sample 46=============================================================================== 47^- 79.133.44.136 1 6 377 56 -10ms[ -10ms] +/- 138ms 48^- electrode.felixc.at 2 6 33 55 +17ms[ +17ms] +/- 119ms 49^* 119.28.206.193 2 6 377 60 +1706us[+2327us] +/- 40ms 50^+ 36.110.233.85 2 6 377 58 -1324us[-1324us] +/- 68ms

chrony守护进程监听在udp协议的123和323端口。 客户端节点上安装好chrony后,修改配置文件如下所示:

1[root@node1 ~]# cat /etc/chrony.conf 2server 172.24.21.221 iburst 34# Record the rate at which the system clock gains/losses time. 5driftfile /var/lib/chrony/drift 67# Allow the system clock to be stepped in the first three updates 8# if its offset is larger than 1 second. 9makestep 3600 3 1011# Enable kernel synchronization of the real-time clock (RTC). 12rtcsync 1314# Enable hardware timestamping on all interfaces that support it. 15#hwtimestamp * 1617# Increase the minimum number of selectable sources required to adjust 18# the system clock. 19#minsources 2 2021# Allow NTP client access from local network. 22#allow 192.168.0.0/16 2324# Serve time even if not synchronized to a time source. 25#local stratum 10 2627# Specify file containing keys for NTP authentication. 28#keyfile /etc/chrony.keys 2930# Specify directory for log files. 31logdir /var/log/chrony 3233# Select which information is logged. 34#log measurements statistics tracking

查看状态:

1[root@node1 ~]# chronyc sources -v 2210 Number of sources = 1 34 .-- 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|| | | \ 12MS Name/IP address Stratum Poll Reach LastRx Last sample 13=============================================================================== 14^* 172.24.21.221 3 6 377 37 -7753ns[ +140us] +/- 29ms

chrony默认是采用连续同步的方式来同步时钟的,并非采用跳变的方式。

点赞
收藏

评论区

加载中...

相关推荐

NTP时间同步

如果服务器的时间出现混乱,将导致很多意想不到的问题。使用NTP,可以使服务器获取正确的时间,从而避免出现问题。NTP是NetworkTimeProtocol的简写,意为“网络时间协议”,是一个用TCP/IP协议实现的时间同步协议。Ubuntu在每次启动都会自动到ntp.ubuntu.com去做时间同步。但是,由于服务器一般不会频繁启动,所以还是不

Linux内网时钟同步问题(ntp和chrony)

我们都知道时钟同步可以使用外网服务器,在内网内不能连接外网的时候也需要时钟同步,那怎么进行呢?选择内网的一台稳定的服务器作为时钟源,然后让其他机器都来同步这台机器即可。注:其实ntp服务和chrony服务本质上差不多,他们之间都可以互相进行同步。不过为了避免不必要的麻烦,一般服务器都只选择同一时钟服务来讲进行同步。一、ntp服务安装:yum

简单概述Serverless

本文分享自天翼云开发者社区@《​​》,作者:我是小朋友Serverless是计算的趋势,它使诸如Web应用程序之类的代码执行与维护服务器以运行该代码的需求脱钩。创建,供应和维护服务器上运行的应用程序的操作开销(传统意义上)非常重要。减少或消除这项工作可以帮

聊聊Docker镜像

本文分享自天翼云开发者社区@《​​​​​​​​​》,作者:AE86上山了。前言回顾前面:为什么需要Docker?Docker入门为什么可以这么简单?在上篇也同样留下一个问题:我们知道Tomcat运行起来需要Java的支持,那么我们在DockerHub拉取下

如何将物理机Windows系统迁移到VMware虚拟机?

本文分享自天翼云开发者社区@《》,作者:我是小朋友如何将物理机上的Windows系统迁移到VMware虚拟机?本文详细介绍如何使用DiskGenius免费版将物理电脑上的Windows操作系统转移至VMware虚拟机。关于迁移物理系统到VMware虚拟机V