NTP服务器既是客户端,又是服务端。
- ntp局域网时间精度0.1ms,互联网中精度在1-50ms;
- chrony局域网中的精度在几十微秒,互联网中的时间进度在几毫秒内;
常见的时间同步服务器软件有两个软件ntp、chrony都是基于NTP网络时间同步协议。但是chrony效率更高,接下来以chrony实现NTP服务器的搭建。
安装chrony
yum -y install charony
chrony配置服务器
[root@rocky8]/root#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).
#pool 2.rocky.pool.ntp.org iburst
server ntp.aliyun.com iburst
server edu.ntp.org.cn iburst
server ntp.tencent.com iburst
# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 0.0.0.0/0 #允许哪些客户端可以使用此服务
# Serve time even if not synchronized to a time source.
local stratum 10 #及时此服务离线了也可以对外提供服务,提供服务的层级时10
客户端配置
[root@rocky8]/root#vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
#pool 2.rocky.pool.ntp.org iburst
server 10.0.0.145 iburst #只需要配置这一项就可以了,也就是你局域网的chrony服务器地址
以下内容是一些杂项,可以增进对chrony的使用与理解
查看chrony信息
[root@rocky8]/root#rpm -qi chrony
Name : chrony
Version : 4.2
Release : 1.el8.rocky.1.0
Architecture: x86_64
Install Date: Sun 25 Jun 2023 07:38:45 AM CST
Group : System Environment/Daemons
Size : 686851
License : GPLv2
Signature : RSA/SHA256, Mon 14 Nov 2022 11:27:31 PM CST, Key ID 15af5dac6d745a60
Source RPM : chrony-4.2-1.el8.rocky.1.0.src.rpm
Build Date : Mon 14 Nov 2022 11:23:52 PM CST
Build Host : ord1-prod-x86build001.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager : infrastructure@rockylinux.org
Vendor : Rocky
URL : https://chrony.tuxfamily.org
Summary : An NTP client/server
Description :
chrony is a versatile implementation of the Network Time Protocol (NTP).
It can synchronise the system clock with NTP servers, reference clocks
(e.g. GPS receiver), and manual input using wristwatch and keyboard. It
can also operate as an NTPv4 (RFC 5905) server and peer to provide a time
service to other computers in the network.
时间同步的服务
查看服务是否已经运行了systemctl status chronyd.service
服务器配置设置的文件位置,要同步的时间同步服务器
添加公共服务器地址,可以在“公共NTP服务”中查询可以使用的服务地址
公共NTP服务
阿里云NTP服务器说明_云服务器 ECS-阿里云帮助中心 (aliyun.com)
TUNA NTP (网络授时) 服务使用说明 | 清华大学 TUNA 协会
全国公共NTP授时服务器地址大全|Public NTP Server - dns.iui.im
建议增加3个以上的NTP服务器,以备服务器的故障时不能始终同步,文件修改地址 /etc/chrony.conf
查看服务端口:323
查询是否同步时钟服务chronyc sources -v
'*'表示当前已同步
'-' 表示目前还没有连接上;
'?' 表示不可达
添加允许同步的网络地址
重启服务systemctl restart chronyd.service后查看端口ss -ntul,发现新增加123的端口,这是ntp协议的标准端口号
设置时间timedatectl set-time '2028-08-29 20:17:16'如果出现“Failed to set time: NTP unit is active”的告警,可以执行关闭ntp服务再操作。
[root@rocky8]/root#timedatectl set-time '2028-08-29 20:17:16'
Failed to set time: NTP unit is active
[root@rocky8]/root#timedatectl set-ntp false
[root@rocky8]/root#timedatectl set-time '2028-08-29 20:17:16'
[root@rocky8]/root#date
Tue Aug 29 20:17:17 CST 2028
值得注意的是服务端如果是离线的状态,又想要为其它客户端提供NTP服务,就一定要将“local stratum 10 ”取消注释。