当前位置: 首页 > article >正文

Linux - 时间服务器

二、时间服务器

1、Chrony

Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软件。它能让计算机保持系统时钟与时 钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算 机提供时间同步服务。

Chrony由两个程序组成,分别是chronyd和chronyc:

​ chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算 机增减时间的比率,并对此进行补偿。

chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机 上工作,也可以在一台不同的远程计算机上工作。 

安装

yum install chrony -y

开机自启动

systemctl enable --now chronyd

2、时间服务器客户端配置

编辑配置文件

[root@client ~]# vim /etc/chrony.conf
#文件内容:
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).

pool 2.centos.pool.ntp.org iburst

# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp

# 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

# Require authentication (nts or key option) for all NTP sources.
#authselectmode require

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# Insert/delete leap seconds by slewing instead of stepping.
#leapsecmode slew

# Set the TAI-UTC offset of the system clock.
#leapseclist /usr/share/zoneinfo/leap-seconds.list

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

添加新的时间服务器信息

#在“Please consider joining the pool”下添加:
例如:
pool ntp.aliyun.com iburst
#注意:时间服务器的匹配是由上到下依次匹配的,越靠上优先级越高。

添加完成后保存退出,测试

#修改主机时间:date -s "时间"
例如:
[root@client ~]# date -s "20221010"
20221010日 星期一 00:00:00 CST
#修改时间后重启服务(这里是重启守护进程,即chronyd):
[root@client ~]# systemctl restart chronyd
#再次查看时间:
[root@client ~]# date
20241026日 星期六 11:35:46 CST
#时间同步成功。

查看已配置的时间服务器

[root@client ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   6    77    43  -2324us[-8884us] +/-   34ms
^- a.chl.la                      2   6    77    43    +56ms[  +56ms] +/-  149ms
^- makaki.miuku.net              3   6    77    43  +6414us[+6414us] +/-   90ms
^+ 117.80.112.205                2   6    77    43  -1424us[-3232us] +/-   93ms
^- ntp8.flashdance.cx            2   6    33    35    -39ms[  -39ms] +/-  143ms
# * 表示chronyd当前同步到的源
# + 表示可接受的信号源,与选定的信号源组合在一起
# ? 指示已失去连接性或其数据包未通过所有测试的源。它也显示在启动时,直到从中至少收集了3个样本为止  

3、时间服务器服务端配置

首先配置服务端

[root@server ~]# vim /etc/chrony.conf 
#也可以添加其他时间服务器,这里就不做改动。
#修改允许访问本服务器的网段地址:
# Allow NTP client access from local network.
allow 192.168.5.0/24
#完成后保存退出。
#重启服务:
[root@server ~]# systemctl restart chronyd

配置客户端

[root@client ~]# vim /etc/chrony.conf
#添加对端客户端并写在第一条(方便测试,将其他都删除):
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool 192.168.5.100 iburst
#保存退出。

测试

[root@client ~]# date -s "20221010"
20221010日 星期一 00:00:00 CST
[root@client ~]# date
20221010日 星期一 00:00:07 CST
[root@client ~]# systemctl restart chronyd
[root@client ~]# date
20241026日 星期六 11:58:34 CST
#查看时间服务器情况:
[root@client ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.5.100                 2   6    17    10  -7837ns[-1112us] +/-   25ms
^- electrode.felixc.at           2   6    17     9    +44ms[  +44ms] +/-  140ms
^? ntp8.flashdance.cx            0   7     0     -     +0ns[   +0ns] +/-    0ns
^? ntp7.flashdance.cx            0   7     0     -     +0ns[   +0ns] +/-    0ns
^- dns1.synet.edu.cn             1   6    17     9   -682us[ -682us] +/-   22ms

补充

#时间服务器使用udp的123号端口。
#查看在使用的端口号:
[root@server ~]# ss -lntup
#可以结合过滤使用:
[root@server ~]# ss -lntup | grep 123
udp   UNCONN 0      0            0.0.0.0:123        0.0.0.0:*    users:(("chronyd",pid=2121,fd=7))   
#注意:时间服务器与客户端要实现时间同步必须网络可达,关闭防火墙,端口启用。

http://www.kler.cn/a/417299.html

相关文章:

  • 胜任力冰山模型:深入探索职业能力的多维结构
  • 探索从传统检索增强生成(RAG)到缓存增强生成(CAG)的转变
  • C32.【C++ Cont】静态实现双向链表及STL库的list
  • QUIC 与 UDP 关系
  • MAC 安装mysql全过程记录
  • vue3+ts 引入 json-editor-vue3
  • springboot学习-spring-boot-data-jdbc分页/排序/多表查询的例子
  • 基于大数据python 房屋价格数据分析预测可视化系统(源码+LW+部署讲解+数据库+ppt)
  • ESP32-S3模组上跑通ES8388(10)
  • CommonJS 和 ES Modules 的 区别
  • uniapp配置全局消息提醒
  • Spring - RabbitMQ循环依赖问题解决
  • 【英特尔IA-32架构软件开发者开发手册第3卷:系统编程指南】2001年版翻译,2-39
  • QT开发准则
  • JS中的类与对象
  • Axios:现代JavaScript HTTP客户端
  • 社交新零售模式下“2+1 链动模式 S2B2C 商城小程序”的创新实践与发展策略
  • flink学习(10)——allowedLateness/测道输出
  • redis快速进门
  • 贪心算法基础解析
  • 【文档搜索引擎】实现索引构建——解析标题、解析URL、解析正文
  • 【西瓜书】支持向量机(SVM)
  • golang append 相关面试题
  • python冒号是什么意思
  • redis基础spark操作redis
  • pycharm报错没有名称为 ‘pytorch‘ 的模块( No module named ‘pytorch‘)