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这是当前为服务器保留的采样点数,通过这些点执行线性回归方法来估算出偏移值

img
img
img

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%以上运维知识点,真正体系化!**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值