Linux使用chrony让局域网内的服务器时间同步_chrony同步间隔


### 四、配置Chrony


#### 1.服务器端配置


​ 将主节点设置为内部NTP Server,编辑“/etc/chrony.conf”文件,如有公网的情况,可以配置阿里云的ntp服务地址作为源 。



> 
> ntp1.aliyun.com
> 
> 
> 授时中心参考
> 
> 
> * 210.72.145.44 国家授时中心
> * ntp.aliyun.com 阿里云
> * s1a.time.edu.cn 北京邮电大学
> * s1b.time.edu.cn 清华大学
> * s1c.time.edu.cn 北京大学
> * s1d.time.edu.cn 东南大学
> * s1e.time.edu.cn 清华大学
> * s2a.time.edu.cn 清华大学
> * s2b.time.edu.cn 清华大学
> * s2c.time.edu.cn 北京邮电大学
> * s2d.time.edu.cn 西南地区网络中心
> * s2e.time.edu.cn 西北地区网络中心
> * s2f.time.edu.cn 东北地区网络中心
> * s2g.time.edu.cn 华东南地区网络中心
> * s2h.time.edu.cn 四川大学网络管理中心
> * s2j.time.edu.cn 大连理工大学网络中心
> * s2k.time.edu.cn CERNET桂林主节点
> * s2m.time.edu.cn 北京大学
> * ntp.sjtu.edu.cn 202.120.2.101 上海交通大学
> 
> 
> 


​ **master01**作为服务端,当前节点IP地址为172.16.24.65,网段是172.16.24.0/24,配置详情如下



> 
> server ntp1.aliyun.com iburst
> 
> 
> allow 172.16.24.0/24
> 
> 
> local stratum 10
> 
> 
> 



[root@master01 ~]# 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 ntp1.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 172.16.24.0/24

Serve time even if not synchronized to a time source.

即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端chronyc命令

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




---


#### 2.客户端节点配置



> 
> 其他**work**节点使用主节点(master01)作为时钟源,增加内网时钟源
> 
> 
> server master01 iburst
> 
> 
> 



[root@worker01 ~]# 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 master01 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

Specify directory for log files.

logdir /var/log/chrony

Select which information is logged.

#log measurements statistics tracking




---


#### 3.配置文件说明:



> 
> /etc/chrony.conf
> 
> 
> 


* **server**  
 可用于时钟服务器,iburst 选项当服务器可达时,发送一个八个数据包而不是通常的一个数据包。 包间隔通常为2秒,可加快初始同步速度,这个可以添加外网服务器或者内网服务器,域名或者IP都可以。
* **driftfile**  
 根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中,会在重启后为系统时钟作出补偿
* **stratumweight**  
 stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级。
* **rtcsync**  
 启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)
* **allow / deny**  
 指定一台主机、子网,或者网络以允许或拒绝访问本服务器
* **cmdallow / cmddeny**  
 可以指定哪台主机可以通过chronyd使用控制命令
* **bindcmdaddress**  
 允许chronyd监听哪个接口来接收由chronyc执行的命令
* **makestep**  
 通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时调整系统时钟
* **local stratum 10**  
 即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端chronyc命令




---


### 四、启动chrony服务


在所有节点上将其配置为在系统引导时启动。



设置开机启动

[root@x~]# systemctl enable chronyd.service

重启服务

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

查看时间同步状态

[root@x~]# timedatectl status

手动强制同步下系统时钟

[root@x~]# chronyc -a makestep

查看时间同步源

[root@x~]# chronyc sources -v

查看时间同步源状态

[root@x~]# chronyc sourcestats -v

硬件时间默认为UTC

[root@x~]# timedatectl set-local-rtc 1

启用NTP时间同步

[root@x~]# timedatectl set-ntp yes


### 五、chronyc相关命令


* **help** 命令可以查看更多chronyc的交互命令
* **accheck** 检查是否对特定主机可访问当前服务器
* **activity** 显示有多少NTP源在线/离线
* **sources [-v]** 显示当前时间源的同步信息
* **sourcestats [-v]** 显示当前时间源的同步统计信息
* **add server** 手动添加一台新的NTP服务器
* **clients** 报告已访问本服务器的客户端列表
* **delete** 手动移除NTP服务器或对等服务器
* **settime** 手动设置守护进程时间
* **sracking** 显示系统时间信息
* **chronyc sources** 主要用于查看时钟源授时时间偏差值。



[root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc sources
210 Number of sources = 15
MS Name/IP address Stratum Poll Reach LastRx Last sample

^+ 120.25.115.20 2 10 255 987 -982us[ -985us] +/- 15ms
^? 10.143.33.49 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.3.1 2 10 377 112 +126us[ +126us] +/- 1601us
^+ 100.100.3.2 2 10 377 258 -75us[ -75us] +/- 1738us
^+ 100.100.3.3 2 10 375 720 -191us[ -191us] +/- 1708us
^+ 203.107.6.88 2 10 377 344 +2988us[+2988us] +/- 18ms
^? 10.143.33.50 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.33.51 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.44 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.45 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.46 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.5.1 2 10 377 537 +324us[ +324us] +/- 1975us
^+ 100.100.5.2 2 10 377 269 -209us[ -209us] +/- 1946us
^+ 100.100.5.3 2 10 377 281 +126us[ +126us] +/- 2078us
^* 100.100.61.88 1 10 377 913 -465us[ -468us] +/- 11ms


这里总共输出8列信息,分别对应含义如下:




| 列名 | 含义 | 具体说明 |
| --- | --- | --- |
| M | 表示授时时钟源 | ^表示服务器,= 表示二级时钟源 ,#表示本地连接的参考时钟 |
| S | 指示源的状态 | \*当前同步的源,+表示其他可接受的源,?表示连接丢失的源,x表示一个认为是falseticker 的时钟(即它的时间与大多数其他来源不一致),~表示其时间似乎具有太多可变性的来源 |
| Name/IP address | 表示源的名称或IP地址,或者参考时钟的refid值 | 无 |
| Stratum | 表示源的层级 | 层级1表示本地连接的参考时钟,第2层表示通过第1层级计算机的时钟实现同步,依此类推。 |
| Poll | 表示源轮询的频率 | 以秒为单位,值是基数2的对数,例如值6表示每64秒进行一次测量,chronyd会根据当时的情况自动改变轮询频率 |
| Reach | 表示源的可达性的锁存值(八进制数值) | 该锁存值有8位,并在当接收或丢失一次时进行一次更新,值377表示最后八次传输都收到了有效的回复 |
| LastRx | 表示从源收到最近的一次的时间 | 通常是几秒钟,字母m,h,d或y分别表示分钟,小时,天或年,值10年表示从未从该来源收到时间同步信息 |
| Last sample | 表示本地时钟与上次测量时源的偏移量 | 方括号中的数字表示实际测量的偏移值,这可以以ns(表示纳秒),us(表示微秒),ms(表示毫秒)或s(表示秒)为后缀;方括号左侧的数字表示原始测量值,这个值是经过调整以允许应用于本地时钟的任何偏差;方括号右侧表示偏差值,+/-指示器后面的数字表示测量中的误差范围,+偏移表示本地时钟快速来源 |


* **chronyc sourcestats** 主要偏移率及每个时钟源的偏移评估值:



[root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc sourcestats
210 Number of sources = 15
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev

120.25.115.20 37 15 12h -0.001 0.024 -216us 516us
10.143.33.49 0 0 0 +0.000 2000.000 +0ns 4000ms
100.100.3.1 26 18 431m -0.001 0.004 +94us 44us
100.100.3.2 22 12 362m +0.001 0.005 +5134ns 44us
100.100.3.3 17 9 362m -0.003 0.006 -236us 38us
203.107.6.88 64 32 18h +0.013 0.039 +553us 1730us
10.143.33.50 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.33.51 0 0 0 +0.000 2000.000 +0ns 4000ms
先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

)]
[外链图片转存中…(img-6Ms7fvGJ-1714280215857)]
[外链图片转存中…(img-hA4RuZHt-1714280215858)]
[外链图片转存中…(img-rZ309NiT-1714280215859)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

  • 26
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤来配置 Linux-1 为其他 Linux 主机提供时间同步服务: 1. 在 Linux-1 上安装 chrony: ``` sudo apt-get update sudo apt-get install chrony ``` 2. 修改 chrony 配置文件 `/etc/chrony/chrony.conf`,将以下行添加到文件末尾: ``` allow 192.168.1.0/24 ``` 其中 `192.168.1.0/24` 是你的局域网地址段,允许这个地址段的主机与 Linux-1 同步时间。 3. 启动 chrony 服务: ``` sudo systemctl start chrony ``` 4. 验证 chrony 是否正常工作: ``` chronyc sources ``` 如果输出类似以下内容,则表示 chrony 已经正常工作: ``` 210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ time.nist.gov 1 6 37 30 -294us[ -625us] +/- 161ms ``` 5. 在其他 Linux 主机上安装 chrony: ``` sudo apt-get update sudo apt-get install chrony ``` 6. 修改 chrony 配置文件 `/etc/chrony/chrony.conf`,将以下行添加到文件末尾: ``` server linux-1 ``` 其中 `linux-1` 是 Linux-1 的主机名或 IP 地址。 7. 启动 chrony 服务: ``` sudo systemctl start chrony ``` 8. 验证时间同步是否正常: ``` chronyc tracking ``` 如果输出类似以下内容,则表示时间同步已经正常: ``` Reference ID : 192.168.1.100 (linux-1) Stratum : 2 Ref time (UTC) : Tue Sep 07 07:52:26 2021 System time : 0.000000000 seconds slow of NTP time Last offset : -0.000000000 seconds RMS offset : 0.000000000 seconds Frequency : 1.814 ppm slow Residual freq : -0.004 ppm Skew : 0.000 ppm Root delay : 0.002045309 seconds Root dispersion : 0.000534147 seconds Update interval : 64.0 seconds Leap status : Normal ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值