linux中chrony服务器的使用

1. chrony的优势

Chrony是NTP的另一种实现,与ntpd不同,它可以更快且更准确地同步系统时钟,最大程度的减少时间和频率误差。

chrony相比ntp的优势:

  • 更快的同步,只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,这对于并非全天 24 小时运行的台式计算机或系统而言非常有用。
  • 能够更好地响应时钟频率的快速变化,这对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用。
  • 在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响。
  • 在应对临时非对称延迟时(例如,在大规模下载造成链接饱和时)提供了更好的稳定性。
  • 无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟。

2. chrony的实现

Chrony运行于UDP323端口,ntp运行于UDP123端口,使用chrony服务器可以同时为chrony客户端和ntp客户端提供服务。

Chrony包括两个核心组件:

1、chronyd:一个后台运行的守护进程,用于调整内核中运行的系统时钟与NTP服务器同步。它确定服务器增减时间的比率,并对此进行调整补偿,即是服务器端进程也可以是客户端进程;

2、chronyc:提供用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的服务器上工作,也可以在一台不同的远程服务器上工作。

3.安装配置 

#rpm -qa | grep chrony  //查看是否安装
#yum -y install chrony  //安装
#systemctl enable chronyd //开机自启动
#systemctl start chronyd  

 4.文件位置

/etc/chrony.conf                  <==chrony的主配置文件
/usr/lib/systemd/system/chronyd.service      <==服务启动文件
/usr/bin/chronyc               <==chronyc工具
/usr/sbin/chronyd             <==主程序文件
/var/log/chrony   <==日志文件

5.chrony命令


查看 ntp_servers
chronyc sources -v
查看 ntp_servers 状态
chronyc sourcestats -v
查看 ntp_servers 是否在线
chronyc activity -v
查看 ntp 详细信息
chronyc tracking -v
强制同步下系统时钟
chronyc -a makestep

6.详解字段

M
 这表示信号源的模式。^表示服务器,=表示对等方,#表示本地连接的参考时钟。
S
 此列指示源的状态。
•	* 表示chronyd当前同步到的源。
•	+ 表示可接受的信号源,与选定的信号源组合在一起。
•	- 表示被合并算法排除的可接受源。
•	? 指示已失去连接性或其数据包未通过所有测试的源。它也显示在启动时,直到从中至少收集了3个样本为止。
•	x 表示chronyd认为是虚假行情的时钟(即,其时间与大多数其他来源不一致)。
•	〜 表示时间似乎具有太多可变性的来源。
Name/IP address
这显示了源的名称或IP地址,或参考时钟的参考ID。
Stratum
这显示了来源的层,如其最近收到的样本中所报告的那样。层1表示一台具有本地连接的参考时钟的计算机。与第1层计算机同步的计算机位于第2层。与第2层计算机同步的计算机位于第3层,依此类推。
Poll
这显示轮询源的速率,以秒为单位的时间间隔的以2为底的对数。因此,值为6表示每64秒进行一次测量。chronyd会根据当前情况自动更改轮询速率。
Reach
这显示了源的可达性寄存器以八进制数字打印。寄存器有8位,并在每个从源接收或丢失的数据包上更新。值377表示从最后八次传输中收到了对所有用户的有效答复。
LastRx
此列显示多长时间前从来源接收到了最后一个好的样本(在下一列中显示)。未通过某些测试的测量将被忽略。通常以秒为单位。字母m,h,d或y表示分钟,小时,天或年。
Last sample
此列显示上次测量时本地时钟与源之间的偏移。方括号中的数字表示实际测得的偏移量。可以用ns(表示纳秒),us (表示微秒),ms(表示毫秒)或s(表示秒)作为后缀。方括号左侧的数字表示原始测量值,已调整为允许此后施加于本地时钟的任何摆度。
+/-指示器后面的数字表示测量中的误差范围。正偏移表示本地时钟位于源时钟之前。

 7.例题
chrony服务部署:两台机器
       a:    第一台机器从阿里云同步时间
             第二台机器从第一台机器同步时间
       b:   第一台服务器使用系统时间作为第二台服务器的时钟源, 第一台服务器层级设置为6,            

温馨提示:保证两台机器的网络通畅,防火墙关闭,selinux为Permissive状态,允许的网段正确

a题:

第一台机器的配置文件 

[root@xiezhehuiIS ~]# 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.rhel.pool.ntp.org iburst
server ntp.aliyun.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  //允许的网段

# Serve time even if not synchronized to a time source.
local stratum 10     //设置层数

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys
"/etc/chrony.conf" 38L, 1110C

第二台机器的配置文件

[root@xiezhehui ~]# 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.rhel.pool.ntp.org iburst
server 192.168.31.142 iburst  //设第一台机器的ip为服务端
# 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  

# Serve time even if not synchronized to a time source.
local stratum 10

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys
"/etc/chrony.conf" 38L, 1110C

重启服务

[root@xiezhehui ~]# systemctl restart chronyd.service

查看

[root@xiezhehui ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.31.142                3   6   377    45   +660us[ +725us] +/-   32ms
[root@xiezhehui ~]# timedatectl 
               Local time: 二 2023-01-03 20:04:39 CST
           Universal time: 二 2023-01-03 12:04:39 UTC
                 RTC time: 二 2023-01-03 12:04:39
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@xiezhehui ~]# chronyc tracking 
Reference ID    : C0A81F8E (192.168.31.142)  //可以看到授时服务器为第一台机器
Stratum         : 4
Ref time (UTC)  : Tue Jan 03 12:04:11 2023
System time     : 0.000234063 seconds fast of NTP time
Last offset     : +0.000199592 seconds
RMS offset      : 0.001441084 seconds
Frequency       : 1.294 ppm fast
Residual freq   : +0.063 ppm
Skew            : 1.751 ppm
Root delay      : 0.052681766 seconds
Root dispersion : 0.003614831 seconds
Update interval : 64.5 seconds
Leap status     : Normal

b题:

第一台机器配置文件

[root@xiezhehuiIS ~]# vim /etc/chrony.conf 

# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#pool 2.rhel.pool.ntp.org iburst
#server ntp.aliyun.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

# Serve time even if not synchronized to a time source.
local stratum 6

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

第二台机器配置文件同上

查看

[root@xiezhehui ~]# chronyc tracking 
Reference ID    : C0A81F8E (192.168.31.142)  //此时第一台机器将自己的系统时间授予第二台机器
Stratum         : 7      //第一台机器的层数为6,则来到第二台就+1
Ref time (UTC)  : Tue Jan 03 12:11:22 2023
System time     : 0.000000261 seconds slow of NTP time
Last offset     : -0.000020905 seconds
RMS offset      : 0.000020905 seconds
Frequency       : 4.751 ppm fast
Residual freq   : -3.062 ppm
Skew            : 0.990 ppm
Root delay      : 0.000228690 seconds
Root dispersion : 0.000025113 seconds
Update interval : 2.0 seconds
Leap status     : Normal

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤来配置ChronyLinux系统上: 1. 安装Chrony使用适合你的Linux发行版的包管理器安装Chrony。例如,在Debian或Ubuntu上可以使用以下命令进行安装: ``` sudo apt-get install chrony ``` 2. 配置Chrony:编辑Chrony的配置文件`/etc/chrony/chrony.conf`,使用你喜欢的文本编辑器打开该文件: ``` sudo nano /etc/chrony/chrony.conf ``` 3. 配置NTP服务器:在配置文件,你需要指定一个或多个NTP服务器。可以在文件找到一行以`server`开头的注释行,将其取消注释并将所需的NTP服务器添加到其。例如: ``` server time1.example.com server time2.example.com ``` 你可以使用任何可用的公共NTP服务器,或者使用你组织内部的NTP服务器。 4. 配置访问控制:如果你希望限制谁可以访问Chrony服务器,你可以在配置文件添加适当的访问控制。例如,要允许特定IP地址范围的客户端访问,可以添加以下行: ``` allow 192.168.0.0/24 ``` 这将允许192.168.0.0/24网段内的客户端访问。 5. 保存并关闭配置文件。 6. 启动Chrony:启动Chrony服务并将其设置为在系统启动时自动启动。使用以下命令启动Chrony服务: ``` sudo systemctl start chrony ``` 使用以下命令将Chrony设置为在系统启动时自动启动: ``` sudo systemctl enable chrony ``` 这样,你就完成了在Linux系统上配置Chrony的过程。你的系统现在应该使用配置的NTP服务器来同步时间。你可以使用`chronyc`命令来检查Chrony的状态和同步情况。例如,可以运行以下命令来查看Chrony的状态: ``` chronyc tracking ``` 希望这对你有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值