Chrony(ntp时间同步服务器),基于ubuntu,rocky,centos,rhel

1. 安装Chrony

银河麒麟V10系统通常默认安装了chrony。如果未安装,可以用以下命令安装:

查看监听端口(默认 UDP 123):sudo ss -ulnp | grep chrony
查询本地同步状态:chronyc tracking
测试客户端连接(从另一台机器执行):ntpdate -q <本机IP> 或使用 chronyc sources -v 查看是否能看到本机。

基于rhel的发行版系统安装如下
yum install -y chrony

基于debian的发行版系统如ubuntu,桌面版麒麟
apt update
apt install chrony -y


2. 配置为时间服务器(/etc/chrony.conf

如果没有网络自定义网络时间

设置时区为亚洲上海
timedatectl set-timezone Asia/Shanghai

设置时间
timedatectl set-time "2026-07-06 14:30:00"

同步到硬件时钟:确保重启后时间不丢失
timedatectl set-local-rtc false

使用 date 命令(备选)
date 命令适合临时或脚本化的修改,但配置通常不持久
date -s "2026-07-06 14:30:00"

同步到硬件时钟:这一步很重要,否则重启后设置会丢失
hwclock --systohc


2.1 设置上游时间源

若服务器能连接外网,可配置公共NTP服务器;若是纯内网环境,则让本机时钟作为时间源:

这是关键步骤,需修改核心配置文件/etc/chrony.conf

2.1.1ubuntu:配置文件配置文件/etc/chrony.conf/chrony.conf,操作系统文件可能不同,可以使用tab按键补齐

# 注释掉所有默认的 pool 或 server 行
# pool ntp.ubuntu.com iburst

# bindaddress 只控制监听地址,不影响 allow 规则:

# 即使绑定到 192.168.1.10,如果 allow 写的是 0.0.0.0/0,理论上任意客户端都能连接这个 IP。正确做法是配合 allow 限制来源网段:
bindaddress 0.0.0.0


# 允许来自 192.168.1.0/24 网段的客户端访问
allow 192.168.1.0/24

# 当所有上游服务器不可用时,将自身声明为 stratum 10 的服务器
local stratum 10

# 没有网络的情况利用本机作为时间同步服务器
server 127.0.0.1 iburst

# 有网络的情况利用阿里云或者别的时间同步服务器
server ntp1.aliyun.com iburst

#默认的配置
pool ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources

#修改为阿里云,把原来的四个选项可以注释掉#前面加#就是注释
pool ntp1.liyun.com  iburst maxsources 1

# 将 NTP_SERVER_IP 替换为你的主NTP服务器IP,也即是你自己服务器的IP地址
server 192.168.1.10 iburst
或
pool 192.168.1.10 iburst maxsources 1

修改好以后重启服务
systemctl restart chronyd


2.2 ubuntu:完整配置文件,可以直接拿去使用

# grep -Ev '^$|#' /etc/chrony/chrony.conf
内容如下
confdir /etc/chrony/conf.d
bindaddress 0.0.0.0
server 127.0.0.1 iburst
local stratum 10
allow 192.168.1.0/24
sourcedir /run/chrony-dhcp
sourcedir /etc/chrony/sources.d
keyfile /etc/chrony/chrony.keys
driftfile /var/lib/chrony/chrony.drift
ntsdumpdir /var/lib/chrony
logdir /var/log/chrony
maxupdateskew 100.0
rtcsync
makestep 1 3
leapsectz right/UTC


2.3 开机启动和立即启动,修改好以后重启服务

systemctl enable chronyd --now
systemctl restart chronyd


2.4 基于rhel(麒麟服务器版本,centos,rocky等)的系统配置文件如下配置文件/etc/chrony.conf

# 基于rhel(麒麟服务器版本,centos,rocky等)的系统配置文件如下
# 配置文件/etc/chrony.conf

# 外网环境:使用阿里云公共NTP服务
server ntp.aliyun.com iburst

# 纯内网环境:使用本地时钟作为时间源
server 127.127.1.0 iburst
local stratum 10

# 监听所有IP地址
bindaddress 0.0.0.0

# 允许哪个IP段可以同步时间服务器
allow 192.168.1.0/24


2.5 基于rhel(麒麟服务器版本,centos,rocky等)的系统完整配置文件

[root@db-1 ~]# grep -Ev '^$|#' /etc/chrony.conf 
#外网使用如下
server ntp.aliyun.com iburst
#内网用如下
#server 127.0.0.1 iburst
local stratum 10
bindaddress 0.0.0.0
allow 192.168.1.0/24
sourcedir /run/chrony-dhcp
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
keyfile /etc/chrony.keys
ntsdumpdir /var/lib/chrony
leapsectz right/UTC
logdir /var/log/chrony


2.6 开机启动和立即启动,修改好以后重启服务

systemctl enable chronyd --now
systemctl restart chronyd


2.7 修改监听地址与访问控制

Chrony默认监听所有网络接口。可通过bindaddress指令指定监听特定IP。例如只监听192.168.1.10

ubuntu:配置文件
配置文件/etc/chrony.conf/chrony.conf,操作系统文件可能不同,可以使用tab按键补齐

基于rhel(麒麟服务器版本,centos,rocky等)的系统配置文件如下
配置文件/etc/chrony.conf

添加如下内容,根据自己的IP修改为自己的IP
bindaddress 192.168.1.10

或者监听所有IP地址
bindaddress 0.0.0.0


同时,通过allow指令控制允许哪些客户端访问。若允许整个局域网访问:

ubuntu:配置文件
配置文件/etc/chrony.conf/chrony.conf,操作系统文件可能不同,可以使用tab按键补齐

基于rhel(麒麟服务器版本,centos,rocky等)的系统配置文件如下
配置文件/etc/chrony.conf

允许哪些IP网段可以同步时间服务器

allow 192.168.1.0/24
allow 10.0.0.1/8
allow 0.0.0.0/0


2.8 其他常用配置可以不用配置

driftfile /var/lib/chrony/drift    # 记录时间偏差
makestep 1.0 3                      # 允许时间步进
rtcsync                             # 启用RTC同步


3. 启动与验证

配置完成后,执行以下命令:

# 重启服务使配置生效
sudo systemctl restart chronyd
# 设置开机自启
sudo systemctl enable chronyd


验证服务状态:

# 查看时间同步源状态
chronyc sources -v
# 查看详细同步信息
chronyc tracking


4. 防火墙与安全

别忘了放行NTP服务端口(UDP 123):

sudo firewall-cmd --permanent --add-service=ntp
sudo firewall-cmd --reload


5.基于rhel客户端(麒麟,centos,rocky等)的系统配置文件如下配置文件/etc/chrony.conf

如果没有安装先来安装一下
yum install chrony -y

修改配置文件
vim /etc/chrony.conf

原内容:
pool 2.rocky.pool.ntp.org iburst

改后内容
把这一行内容改成你服务器的比如我的服务器ip是192.168.1.10
pool 192.168.1.10 iburst

或者这个写法
server 192.168.1.10 iburs

重启服务
systemctl restart chronyd

验证
chronyc  sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 192.168.1.10                  0   7     0     -     +0ns[   +0ns] +/-    0ns
[root@db-1 ~]# chronyc  sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 192.168.1.10                  0   7     0     -     +0ns[   +0ns] +/-    0ns


5.1详细配置文件如下

# grep -Ev '^$|#' /etc/chrony.conf
pool 192.168.1.10 iburst
sourcedir /run/chrony-dhcp
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
keyfile /etc/chrony.keys
ntsdumpdir /var/lib/chrony
leapsectz right/UTC
logdir /var/log/chrony


6.ubuntu客户端配置

如果没有安装先来安装一下
apt update
apt install chrony -y

修改配置文件:
vim /etc/chrony/chrony.conf

原内容:
pool ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

改后内容,把原来的删除掉,在下面新增一行
把这一行内容改成你服务器的比如我的服务器ip是192.168.1.10
pool 192.168.1.10 iburst

或者这个写法
server 192.168.1.10 iburs

重启服务
systemctl restart chronyd


6.1详细配置文件

# grep -Ev '^$|#' /etc/chrony/chrony.conf 
confdir /etc/chrony/conf.d
pool 192.168.1.10 iburst
sourcedir /run/chrony-dhcp
sourcedir /etc/chrony/sources.d
keyfile /etc/chrony/chrony.keys
driftfile /var/lib/chrony/chrony.drift
ntsdumpdir /var/lib/chrony
logdir /var/log/chrony
maxupdateskew 100.0
rtcsync
makestep 1 3
leapsectz right/UTC


总结

  • 配置文件:主要编辑 /etc/chrony.conf

  • 监听地址:用 bindaddress 指定IP。

  • 访问控制:用 allow 设置允许的客户端网段。

  • 重启生效:每次修改配置后需重启 chronyd 服务。




Powered By Z-BlogPHP 1.7.4

 Theme By 优美模版

本博客为开源博客,本博客联系QQ:372097243