时钟源服务器解决:chronyd
安装服务端
说明
时钟服务器:192.168.19.69
此处选择chrony作为服务端,都是ntp协议的实现,包括chronyd和chronyc
端口:默认使用udp 123服务端之间通讯,323端口客户端与服务端通讯
- centos7.4+、almalinux8.6+默认已安装的服务
- 既可以是服务端也可以是客户端
- 更精确的时间控制
linux环境(免安装)
确定版本:使用3.4
chrony-3.4-1.el7.x86_64.rpm#保持版本一致,能减少同步问题
chronyd -v
确定安装环境(此次教程栽在这里)
#因centos早期版本可能ntpd和chrony共存,或其他人安装了ntpd,需要关闭ntpd
netstat -anlp|grep 123 #端口是否有占用
netstat -anlp|grep 323 #端口是否有占用
netstat -anlp|grep ntp #ntp服务是否开启
确定状态
#当前服务器状态,上图刚安装系统后的初始状态,注意时区
timedatectl
说明:
Local time: 二 2022-03-01 00:47:50 CST #当地时间
Universal time: 一 2022-02-28 16:47:50 UTC #伦敦时间
RTC time: 一 2022-02-28 16:47:50 #硬件时间
Time zone: Asia/Shanghai (CST, +0800) #NTP服务
NTP synchronized: no #NTP时间是否启用
RTC in local TZ: no #是否启用RTC硬件时间
【定系统时间计算方式,0表示使用utc时间计算方式,yes为0,no为1】
【timedatectl set-local-rtc 1这个命令控制】
DST active: n/a
不关闭防火墙、但允许123、323端口
#如果没开启防火墙,则不需要配置
firewall-cmd --permanent --add-port=123/udp
firewall-cmd --permanent --add-port=323/udp
firewall-cmd --reload
关闭selinux
setenforce 0 #设置关闭
getenroce #查看状态
修改配置:服务器同步的上游
#因为是内网,则同步机器自身的时间即可
vi /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 192.168.19.69 #localhost
server 127.127.1.0 #localhost,注:内网环境必须这么做,否则其他会无法同步成功。可以使用公网的服务器时间
修改配置:允许下游的IP
#因为是内网,所以也不需要配置
# Allow NTP client access from local network.
allow 0.0.0.0/0 #如果需要则配置,不需要可配置为所有
修改配置:允许自己权限
#即使不能和一个远程服务器时间源同步,也能继续为其他客户端提供时间同步服务.
10 代表当前服务器设置为第10层, 按照 原子钟为第一层,互联网上的若干时间服务器为第N层,第10层足够表示当前主机所在的局域网.
#chronyd重启后,需1小时左右才会生效
local stratum 10
启动服务端
systemctl restart chronyd #启动chrony服务
systemctl enable chronyd #设置开机同步时间
systemctl status chronyd #查看服务状态
最终服务端配置内容demo
# 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 192.168.8.10 iburst
#server 192.168.19.69 iburst
#server 127.127.1.0
server 192.168.19.69 #localhost
server 127.127.1.0 #localhost
# 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
#allow 0.0.0.0/24
allow 0.0.0.0/0
# Serve time even if not synchronized to a time source.
#local stratum 10
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
测试安装状态
Poll=6表示等待2^6秒后再次发起同步;LastRx=6表示上一次同步后的计时
#重启
systemctl restart chronyd
#查看同步状态,^* 表示服务端部署成功
chronyc sources -v
#查看监听端口,现chrony服务已经在监听123和323的udp端口,服务端配置成功。
netstat -anlp | grep chrony
#时钟状态
timedatectl
安装客户端
linux环境(免安装)
确定版本使用3.4
chronyd -v
确定状态
#当前服务器状态,上图刚安装系统后的初始状态,注意时区
timedatectl
说明:
Local time: 二 2022-03-01 00:47:50 CST #当地时间
Universal time: 一 2022-02-28 16:47:50 UTC #伦敦时间
RTC time: 一 2022-02-28 16:47:50 #硬件时间
Time zone: Asia/Shanghai (CST, +0800) #NTP服务
NTP synchronized: no #NTP时间是否启用
RTC in local TZ: no #是否启用RTC硬件时间
【定系统时间计算方式,0表示使用utc时间计算方式,yes为0,no为1】
【timedatectl set-local-rtc 1这个命令控制】
DST active: n/a
配置服务地址
#一样打开此文件
vi /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
#上游服务器IP,iburst:并行同步,加快同步速度
server 192.168.19.69 iburst
启动客户端
systemctl restart chronyd #启动chrony服务
systemctl enable chronyd #设置开机同步时间
systemctl status chronyd #查看服务状态
定时强制同步(不能同步则配置)
crontab -e #打开定时任务器
*/30 * * * * root /usr/sbin/ntpdate -u 192.168.19.69 #30分钟定时强制更新
最终客户端配置内容demo
# 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 192.168.19.69 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
测试安装状态
指向服务端的IP192.168.19.69
#查看时间同步源连接:
//^?同步失败
//^*同步成功
chronyc sources -v #有时候没这么快
服务端测试
#1.修改服务器时间
#修改时间后执行:clock -w【写入硬件时间,一般2个时间同步修改】
timedatectl set-ntp no #允许本地修改,chronyd会停止
timedatectl set-time "2022-12-16 14:50:00"
clock -w
timedatectl set-ntp yes #重新启动chronyd
chronyc -a makestep #立即手工同步
systemctl restart chronyd
#查看所有时钟状态
#硬件时间不一致,原因???
timedatectl
2、在客户端执行同步
#查看客户端时间
date
chronyc sources -v #查看时间同步源
chronyc -a makestep #立即手工同步
systemctl restart chronyd #重启
chronyc tracking #校准时间服务器
#参数说明 :makestep 1.0 3
##在启动 chronyd 时,如果本机和 server 的时间差超过 1.0 秒,就通过三次修改,将本机时间修改到 server 的时间。
## 注意,只有在启动 chronyd 时,该操作才会执行。但同时也说明,每次启动 chronyd 都会执行一次 chronyc makestep 命令
#客户端强制同步2
#注意:在NTP Server重新启动NTP服务后,NTP Server自身或与其他NTPServer的同步大概需要5分钟左右,因此NTP客户端在这个时间段运行ntpdate命令时会产生no server suitable for synchronization found错误。
ntpdate -u 192.168.19.69
nc -vuz 192.168.19.69 323
客户端测试二
#开启了ntp同步后,本地修改时间,过段时间会被改回ntp服务端时间
date -s "2022-12-16 14:50:00"
clock -w
timedatectl参数说明
列名 | 含义 | 具体说明 |
---|---|---|
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(表示秒)为后缀;方括号左侧的数字表示原始测量值,这个值是经过调整以允许应用于本地时钟的任何偏差;方括号右侧表示偏差值,+/-指示器后面的数字表示测量中的误差范围,+偏移表示本地时钟快速来源 |
问题
这段时间存在不能同步问题,在确认用本地时间作为时间源,要等15分钟左右,但过一这段时间正常运行,在服务端查询发现已经Normal
操作
#查看时间同步源连接:
//^?同步失败
//^*同步成功
chronyc sources -v
#立即手工同步
chronyc -a makestep
#查看时间同步源状态:
chronyc sourcestats -v
#校准时间服务器(use):
//命令返回结果为Leap status: Normal,则代表同步过程正常。
//命令返回结果为Leap status: Not synchronised,则表示同步过程出错
chronyc tracking
#查看时间同步状态
timedatectl status
#启用NTP时间同步
#如果把NTP enabled 修改为yes时,表示开启自动同步时间,此时,是不能手动修改时间的。
#如果为No时,表示关闭自动同步时间。
timedatectl set-ntp yes
#设置硬件时钟为本地时间
硬件时间默认为UTC: timedatectl set-local-rtc 0
设置硬件时钟为本地时间:timedatectl set-local-rtc 1
设置硬件时钟为UTC时间:timedatectl set-local-rtc 0
#设置时区
timedatectl set-timezone Asia/Shanghai
参考
https://blog.csdn.net/cuichongxin/article/details/123178318
https://blog.csdn.net/xinle0320/article/details/124232910https://www.osyunwei.com/archives/12126.html