Linux - 时间服务器

二、时间服务器

1、Chrony

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

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

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

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

安装

bash 复制代码
yum install chrony -y

开机自启动

bash 复制代码
systemctl enable --now chronyd

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

编辑配置文件

bash 复制代码
[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

添加新的时间服务器信息

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

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

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

查看已配置的时间服务器

bash 复制代码
[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、时间服务器服务端配置

首先配置服务端

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

配置客户端

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

测试

bash 复制代码
[root@client ~]# date -s "20221010"
2022年 10月 10日 星期一 00:00:00 CST
[root@client ~]# date
2022年 10月 10日 星期一 00:00:07 CST
[root@client ~]# systemctl restart chronyd
[root@client ~]# date
2024年 10月 26日 星期六 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

补充

bash 复制代码
#时间服务器使用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))   
#注意:时间服务器与客户端要实现时间同步必须网络可达,关闭防火墙,端口启用。
相关推荐
Molesidy1 小时前
【随笔】【Debian】【ArchLinux】基于Debian和ArchLinux的ISO镜像和虚拟机VM的系统镜像获取安装
运维·debian·archlinux
喜欢你,还有大家1 小时前
Linux笔记14——shell编程基础-8
linux·前端·笔记
skywalk81632 小时前
mayfly-go:web 版 linux、数据库等管理平台
linux·运维·数据库
dbdr09012 小时前
Linux 入门到精通,真的不用背命令!零基础小白靠「场景化学习法」,3 个月拿下运维 offer,第二十四天
linux·运维·c语言·python·学习
烟雨迷2 小时前
web自动化测试(selenium)
运维·开发语言·前端·python·selenium·测试工具
吐个泡泡v2 小时前
Portainer:Docker可视化管理神器部署与使用攻略
运维·docker·容器·portainer
绵绵细雨中的乡音2 小时前
简易TCP网络程序
linux·网络
Lu Yao_3 小时前
golang -- viper
服务器·数据库·golang
wanhengidc3 小时前
在云手机中游戏可以自动更新吗?
运维·科技·安全·游戏·智能手机
wanhengidc4 小时前
云端虚拟手机:云手机的原理是什么?
运维·安全·游戏·智能手机