目录
一、NTP - - Neatwork Time Protocol
一、NTP - - Neatwork Time Protocol
网络时间协议(NTP)是一种计算机网络中同步时间的协议。它是互联网标准协议之一,旨在使计算机在网络中相互同步,以确保它们具有相同的时间基准。通过udp 123端口进行网络时钟同步。
关于NTP:
-
NTP使用客户端/服务器模型:一个NTP服务器负责提供时间同步服务,客户端计算机通过与该服务器进行通信来同步时间。
-
NTP使用UTC时间:协调世界时(UTC)是国际标准时间,用于同步全球计算机和其他设备的时间。
-
NTP使用层次结构:NTP服务器可以形成一个层次结构,其中较高级别的服务器(称为“上游”服务器)从更准确的时间源获取时间,较低级别的服务器(称为“下游”服务器)则从上游服务器同步时间。
-
NTP支持多种类型的服务器:NTP服务器可以是原子钟,GPS接收器或其他计算机。
二、Chrony
chrony是网络协议ntp的通用实现。
它可以提供:保持系统时间与NTP时钟服务器同步的服务,此外还可以作为NTP服务器对其他服务器提供时间同步服务。
chrony的特点:
1.运行稳定良好
2.精度较高
3.开源
1.chronyd & chronyc
chrony有两个核心组件:chronyd 和 chonyc
- chronyd
- 守护进程,主要用于调整内核中系统时间和时间服务器同步
- chronyc
- 图形化页面,对chronyd的性能进行监控以及改变各种参数
1.1安装
一般再Linux7.0以后的版本都会预装chrony ,如果你的系统没有chrony,用下面的命令安装
yum install chrony -y
1.2 查看运行状态
systemctl status chronyd.service
1.3常用命令
- 显示系统时钟性能参数
Reference ID : CA760151 (time.neu.edu.cn)systemctl status chronyd.service
Stratum : 2
Ref time (UTC) : Wed Apr 12 02:58:13 2023
System time : 0.000019719 seconds fast of NTP time
Last offset : -0.000094920 seconds
RMS offset : 0.047824778 seconds
Frequency : 2.133 ppm slow
Residual freq : -0.009 ppm
Skew : 1.081 ppm
Root delay : 0.033126365 seconds
Root dispersion : 0.001346738 seconds
Update interval : 128.5 seconds
Leap status : Normal
- 显示时间同步源
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Devchronyc sourcestats
==============================================================================
time.neu.edu.cn 25 13 37m +0.006 0.954 +322ns 745us
36.110.233.85 15 9 22m -0.255 1.779 -572us 719us
ntp5.flashdance.cx 22 11 36m +0.056 3.561 -29ms 3066us
130.61.44.4 23 14 35m +0.186 2.629 -1934us 1978us
2. chrony配置
配置文件路径: /etc/chrony.conf
一些常用的配置项:
- pool
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (https://www.pool.ntp.org/join.html). pool 2.rhel.pool.ntp.org iburst
这里可以添加很多世时间服务器,pool 后面 可以写域名 也可以写IP。
pool 210.72.145.44
pool edu.ntp.org.cn
pool 也可以写 server…
server 210.72.145.44
iburst会在启动的2秒内,去快速poll服务器4次来快速矫正当前系统时间
- makestep
# Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3
正常情况下如果系统时钟跟时间服务器不一致,chrony调整的方式是慢慢增加,或慢慢减少,不会一步到位,直接去跟时间服务器对齐。
makestep 1.0 3,意思就是如果时间服务器跟系统时间相差1秒,那么就在下3个时钟更新中追上时间服务器。
- local stratum
# Serve time even if not synchronized to a time source. #local stratum 10
如果将当前主机当作时间服务器的话,就取消掉 local stratum 的注释,
- allow
# Allow NTP client access from local network. #allow 192.168.0.0/16
如果作为时间服务器,取消掉 allow的注释之后,网段范围内所有主机都可以向当前主机同步请求时间。也可以直接指定某一主机的IP。
3. 配置ntp时间服务器
server (/etc/chrony.conf)
local stratum 10
allow 192.168.0.0/16
client (/etc/chrony.conf)
pool 192.168.84.132 iburst
192.168.84.132为服务器IP,
验证
date