IT评测·应用市场-qidao123.com
标题:
Linux利用chrony让局域网内的服务器时间同步_chrony同步间隔,走进Linux运
[打印本页]
作者:
曂沅仴駦
时间:
2024-10-16 01:00
标题:
Linux利用chrony让局域网内的服务器时间同步_chrony同步间隔,走进Linux运
在所有节点上执行chrony服务安装下令,将主节点设置时间服务器,其他的节点都从主节点同步时间。
# 安装服务
yum -y install chrony
# 查看状态
systemctl status chronyd
# 重启chronyd
systemctl restart chronyd
复制代码
二、检查设置时区
在所有节点上设置统一的时间地区,本文中将其设置为亚洲时区,用户可自行界说。
# 查看时区
[root@x ~]# timedatectl
Local time: Sun 2022-04-17 11:15:48 CST
Universal time: Sun 2022-04-17 03:15:48 UTC
RTC time: Sun 2022-04-17 11:15:48
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
# 筛选式查看在亚洲S开的上海可用时区:
[root@x ~]# timedatectl list-timezones | grep -E "Asia/S.\*"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
# 设置当前系统为Asia/Shanghai上海时区:
[root@x ~]# timedatectl set-timezone Asia/Shanghai
# 修改日期时间(可选,主节时钟源无法同步,可以先关闭NTP同步)
timedatectl set-ntp false
timedatectl set-time "2022-04-17 15:50:20"
# 开启 NTP
timedatectl set-ntp true
#设置完时区后,强制同步下系统时钟:
[root@x ~]# chronyc -a makestep
200 OK
复制代码
三、防火墙设置
因NTP利用123/UDP端口协议,所以答应NTP服务即可。但是我一样平常都会把防火墙和selinux禁止。
# 查看防火墙状态
[root@x ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
# 启动防火墙
[root@x ~]# systemctl start firewalld.service
# 防火墙内放行NTP服务
[root@x ~]# firewall-cmd --add-service=ntp --permanent
success
# reload才能生效
[root@x ~]# firewall-cmd --reload
success
复制代码
内部访问一样平常直接关闭防火墙
[root@x ~]# systemctl stop firewalld
[root@x ~]# systemctl disalbe firewalld
复制代码
四、配置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
10.143.0.44 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.0.45 0 0 0 +0.000 2000.000 +0ns 4000ms
10.143.0.46 0 0 0 +0.000 2000.000 +0ns 4000ms
100.100.5.1 16 8 258m +0.001 0.009 +321us 44us
100.100.5.2 17 8 276m +0.001 0.007 -219us 35us
100.100.5.3 21 11 344m +0.001 0.006 +80us 48us
100.100.61.88 17 11 276m -0.001 0.009 -389us 39us
复制代码
列名寄义Name/IP address表示源的名称或IP地址,大概参考时钟的refid值NP这是当前为服务器保留的采样点数,通过这些点执行线性回归方法来估算出偏移值
9us 35us
100.100.5.3 21 11 344m +0.001 0.006 +80us 48us
100.100.61.88 17 11 276m -0.001 0.009 -389us 39us
| 列名 | 含义 |
| --- | --- |
| Name/IP address | 表示源的名称或IP地址,或者参考时钟的refid值 |
| NP | 这是当前为服务器保留的采样点数,通过这些点执行线性回归方法来估算出偏移值 |
[外链图片转存中...(img-WJNzuxmw-1727248514732)]
[外链图片转存中...(img-sozEjHiv-1727248514732)]
[外链图片转存中...(img-qBbD3lld-1727248514733)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4