本文档风哥主要介绍系统时间同步配置与验证实战,包括系统时间同步的概念、NTP协议、chronyd服务配置、timedatectl命令使用、本地NTP服务器搭建等内容,参考RHEL 10官方文档,适合系统管理员在生产环境中使用。更多视频教程www.fgedu.net.cn
Part01-基础概念与理论知识
1.1 系统时间同步的概念
系统时间同步是指将本地系统时间与标准时间服务器保持一致的过程。时间同步对于分布式系统、日志分析、安全审计等场景至关重要。
- 硬件时钟(RTC):主板上的实时时钟
- 系统时钟:操作系统维护的软件时钟
- NTP服务器:提供标准时间服务的服务器
- NTP客户端:从NTP服务器同步时间的客户端
1.2 NTP协议基础
NTP(Network Time Protocol)网络时间协议:
- 协议版本:NTPv4(当前版本)
- 协议端口:UDP 123
- 同步精度:毫秒级(局域网)、秒级(广域网)
- 工作模式:客户端/服务器模式、对等模式
- 同步算法:Marzullo算法、过滤算法
1.3 时间同步的重要性
时间同步的重要性:
- 日志分析:统一时间便于日志关联分析
- 安全审计:准确的时间戳用于安全审计
- 分布式系统:分布式事务需要统一时间
- 集群协调:集群节点需要时间同步
- 定时任务:定时任务需要准确时间
Part02-生产环境规划与建议
2.1 时间服务器规划
时间服务器规划建议:
# 时间服务器层次
– Stratum 1:直接连接原子钟的时间服务器
– Stratum 2:从Stratum 1同步的时间服务器
– Stratum 3:从Stratum 2同步的时间服务器
– Stratum N:从Stratum N-1同步的时间服务器
# 公共NTP服务器
– 中国:ntp.aliyun.com、ntp.tencent.com、ntp.cn.pool.ntp.org
– 全球:pool.ntp.org、time.google.com、time.cloudflare.com
# 本地NTP服务器
– 内网NTP服务器:从公共NTP服务器同步
– 集群NTP服务器:从本地NTP服务器同步
– 客户端服务器:从集群NTP服务器同步
# 服务器数量
– 最小数量:1个
– 推荐数量:2-3个
– 生产环境:至少2个,分布在不同网络
2.2 同步策略设计
时间同步策略设计:
# 同步频率
– 默认频率:每64秒同步一次
– 高精度需求:每16秒同步一次
– 低精度需求:每1024秒同步一次
# 同步阈值
– 时间偏差阈值:默认100ms
– 最大时间偏差:默认17分钟
– 超过阈值:逐步调整时间
# 同步模式
– 客户端模式:从NTP服务器同步时间
– 服务器模式:为其他服务器提供时间服务
– 对等模式:与其他服务器互相同步
# 时间调整方式
– 渐进调整:逐步调整时间(默认)
– 立即调整:立即设置时间(不推荐)
2.3 监控与告警规划
时间同步监控与告警规划:
- 监控指标:时间偏差、同步状态、服务器延迟
- 监控频率:每5-10分钟检查一次
- 告警阈值:时间偏差超过1秒
- 告警渠道:邮件、短信、即时通讯
- 日志记录:记录同步历史和异常
Part03-生产环境项目实施方案
3.1 chronyd服务安装与配置
3.1.1 安装chronyd服务
# dnf install -y chrony
Updating Subscription Management repositories.
Last metadata expiration check: 0:30:00 ago on Fri 02 Apr 2026 10:00:00 AM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
chrony x86_64 4.2-1.el10 baseos 350 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 350 k
Installed size: 1.2 M
Downloading Packages:
chrony-4.2-1.el10.x86_64.rpm 350 kB/s | 350 kB 00:01
——————————————————————————–
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : chrony-4.2-1.el10.x86_64 1/1
Running scriptlet: chrony-4.2-1.el10.x86_64 1/1
Verifying : chrony-4.2-1.el10.x86_64 1/1
Installed:
chrony-4.2-1.el10.x86_64
Complete!
# 2. 启动chronyd服务
# systemctl start chronyd
# 3. 设置chronyd服务开机自启
# systemctl enable chronyd
Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service.
# 4. 查看chronyd服务状态
# systemctl status chronyd
● chronyd.service – NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2026-04-02 10:00:00 CST; 5s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
https://chrony.tuxfamily.org/documentation.html
Main PID: 1234 (chronyd)
Tasks: 1 (limit: 4915)
Memory: 1.2M
CGroup: /system.slice/chronyd.service
└─1234 /usr/sbin/chronyd
Apr 02 10:00:00 server1 systemd[1]: Starting NTP client/server…
Apr 02 10:00:00 server1 chronyd[1234]: chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG)
Apr 02 10:00:00 server1 systemd[1]: Started NTP client/server.
3.1.2 配置chronyd服务
# cp /etc/chrony.conf /etc/chrony.conf.bak
# 2. 编辑chrony配置文件
# vi /etc/chrony.conf
# 3. 配置NTP服务器
# cat /etc/chrony.conf
# 使用公共NTP服务器
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server ntp.cn.pool.ntp.org iburst
# 允许本地网络同步
allow 192.168.1.0/24
# 设置时间同步阈值
makestep 1.0 3
# 设置日志级别
logdir /var/log/chrony
# 4. 重启chronyd服务
# systemctl restart chronyd
# 5. 查看chronyd服务状态
# systemctl status chronyd
● chronyd.service – NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2026-04-02 10:00:00 CST; 5s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
https://chrony.tuxfamily.org/documentation.html
Main PID: 1234 (chronyd)
Tasks: 1 (limit: 4915)
Memory: 1.2M
CGroup: /system.slice/chronyd.service
└─1234 /usr/sbin/chronyd
Apr 02 10:00:00 server1 systemd[1]: Starting NTP client/server…
Apr 02 10:00:00 server1 chronyd[1234]: chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG)
Apr 02 10:00:00 server1 systemd[1]: Started NTP client/server.
3.2 timedatectl命令使用
3.2.1 查看和设置系统时间
# timedatectl
Local time: Thu 2026-04-02 10:00:00 CST
Universal time: Thu 2026-04-02 02:00:00 UTC
RTC time: Thu 2026-04-02 02:00:00
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
# 2. 查看所有可用时区
# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
…
Asia/Shanghai
Asia/Chongqing
Asia/Hong_Kong
…
UTC
# 3. 设置系统时区
# timedatectl set-timezone Asia/Shanghai
# 4. 验证时区设置
# timedatectl
Local time: Thu 2026-04-02 10:00:00 CST
Universal time: Thu 2026-04-02 02:00:00 UTC
RTC time: Thu 2026-04-02 02:00:00
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
# 5. 手动设置系统时间(不推荐)
# timedatectl set-time “2026-04-02 10:00:00”
# 6. 启用NTP同步
# timedatectl set-ntp true
# 7. 禁用NTP同步
# timedatectl set-ntp false
3.3 手动时间同步
3.3.1 使用chronyc命令手动同步
# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp.aliyun.com 2 6 377 15 +123us[ +234us] +/- 15ms
^+ ntp.tencent.com 2 6 377 16 -234us[ -123us] +/- 16ms
^+ ntp.cn.pool.ntp.org 2 6 377 17 +345us[ +456us] +/- 18ms
# 2. 查看时间同步详细信息
# chronyc sourcestats
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
ntp.aliyun.com 10 6 234 +0.000 0.009 +123us 234us
ntp.tencent.com 10 6 234 -0.000 0.009 -234us 345us
ntp.cn.pool.ntp.org 10 6 234 +0.000 0.009 +345us 456us
# 3. 查看时间同步跟踪信息
# chronyc tracking
Reference ID : C0A80164 (192.168.1.100)
Stratum : 3
Ref time (UTC) : Thu Apr 2 02:00:00 2026
System time : 0.000000000 seconds fast of NTP time
Last offset : +0.000123456 seconds
RMS offset : 0.000234567 seconds
Frequency : 0.000000000 ppm fast
Residual freq : +0.000000000 ppm
Skew : 0.000000000
Root delay : 0.012345678 seconds
Root dispersion : 0.023456789 seconds
Update interval : 64.0 seconds
Leap status : Normal
# 4. 手动触发时间同步
# chronyc makestep
200 OK
# 5. 查看NTP服务器活动状态
# chronyc activity
200 OK
3 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst
0 sources with unknown address
Part04-生产案例与实战讲解
4.1 搭建本地NTP服务器
4.1.1 配置本地NTP服务器
# dnf install -y chrony
Updating Subscription Management repositories.
Last metadata expiration check: 0:30:00 ago on Fri 02 Apr 2026 10:00:00 AM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
chrony x86_64 4.2-1.el10 baseos 350 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 350 k
Installed size: 1.2 M
Downloading Packages:
chrony-4.2-1.el10.x86_64.rpm 350 kB/s | 350 kB 00:01
——————————————————————————–
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : chrony-4.2-1.el10.x86_64 1/1
Running scriptlet: chrony-4.2-1.el10.x86_64 1/1
Verifying : chrony-4.2-1.el10.x86_64 1/1
Installed:
chrony-4.2-1.el10.x86_64
Complete!
# 2. 配置本地NTP服务器
# cat > /etc/chrony.conf << 'EOF'
# 使用公共NTP服务器作为上游
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server ntp.cn.pool.ntp.org iburst
# 允许本地网络同步
allow 192.168.1.0/24
# 设置时间同步阈值
makestep 1.0 3
# 设置日志级别
logdir /var/log/chrony
# 设置最大客户端数
maxclients 100
# 设置客户端限制
cmdallow 192.168.1.0/24
EOF
# 3. 启动chronyd服务
# systemctl start chronyd
# 4. 设置chronyd服务开机自启
# systemctl enable chronyd
Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service.
# 5. 开放NTP服务端口(UDP 123)
# firewall-cmd --permanent --add-service=ntp
success
# firewall-cmd --reload
success
# 6. 验证防火墙规则
# firewall-cmd --list-services
ssh dhcpv6-client ntp
# 7. 查看chronyd服务状态
# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2026-04-02 10:00:00 CST; 5s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
https://chrony.tuxfamily.org/documentation.html
Main PID: 1234 (chronyd)
Tasks: 1 (limit: 4915)
Memory: 1.2M
CGroup: /system.slice/chronyd.service
└─1234 /usr/sbin/chronyd
Apr 02 10:00:00 server1 systemd[1]: Starting NTP client/server...
Apr 02 10:00:00 server1 chronyd[1234]: chronyd version 4.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG)
Apr 02 10:00:00 server1 systemd[1]: Started NTP client/server.
4.2 批量服务器时间同步
4.2.1 使用Ansible批量配置时间同步
# cat > /etc/ansible/hosts << 'EOF' [webservers] 192.168.1.100 192.168.1.101 192.168.1.102 [dbservers] 192.168.1.103 192.168.1.104 EOF # 2. 创建Ansible playbook # cat > /tmp/time_sync.yml << 'EOF' --- - name: 批量配置时间同步 hosts: all become: yes vars: ntp_servers: - "192.168.1.1" - "192.168.1.2" - "192.168.1.3" tasks: - name: 安装chrony dnf: name: chrony state: present - name: 配置chrony template: src: /etc/chrony.conf.j2 dest: /etc/chrony.conf owner: root group: root mode: '0644' notify: 重启chronyd服务 - name: 启动chronyd服务 systemd: name: chronyd state: started enabled: yes - name: 开放NTP服务端口 firewalld: service: ntp permanent: yes state: enabled notify: 重启防火墙服务 handlers: - name: 重启chronyd服务 systemd: name: chronyd state: restarted - name: 重启防火墙服务 systemd: name: firewalld state: restarted EOF # 3. 创建chrony配置模板 # cat > /etc/chrony.conf.j2 << 'EOF' # 使用本地NTP服务器 {% for server in ntp_servers %} server {{ server }} iburst {% endfor %} # 设置时间同步阈值 makestep 1.0 3 # 设置日志级别 logdir /var/log/chrony EOF # 4. 执行Ansible playbook # ansible-playbook -i /etc/ansible/hosts /tmp/time_sync.yml PLAY [批量配置时间同步] ********************************************************** TASK [Gathering Facts] *************************************************************** ok: [192.168.1.100] ok: [192.168.1.101] ok: [192.168.1.102] ok: [192.168.1.103] ok: [192.168.1.104] TASK [安装chrony] ****************************************************************** ok: [192.168.1.100] ok: [192.168.1.101] ok: [192.168.1.102] ok: [192.168.1.103] ok: [192.168.1.104] TASK [配置chrony] ****************************************************************** changed: [192.168.1.100] changed: [192.168.1.101] changed: [192.168.1.102] changed: [192.168.1.103] changed: [192.168.1.104] TASK [启动chronyd服务] ************************************************************ ok: [192.168.1.100] ok: [192.168.1.101] ok: [192.168.1.102] ok: [192.168.1.103] ok: [192.168.1.104] TASK [开放NTP服务端口] ************************************************************ changed: [192.168.1.100] changed: [192.168.1.101] changed: [192.168.1.102] changed: [192.168.1.103] changed: [192.168.1.104] RUNNING HANDLERS *************************************************************** [WARNING]: Consider using the systemd module rather than running systemctl. TASK [重启chronyd服务] ************************************************************ changed: [192.168.1.100] changed: [192.168.1.101] changed: [192.168.1.102] changed: [192.168.1.103] changed: [192.168.1.104] TASK [重启防火墙服务] ************************************************************ changed: [192.168.1.100] changed: [192.168.1.101] changed: [192.168.1.102] changed: [192.168.1.103] changed: [192.168.1.104] PLAY RECAP ************************************************************************* 192.168.1.100 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.101 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.102 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.103 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.1.104 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.3 常见问题排查
4.3.1 时间同步问题排查
# 检查1:确认chronyd服务运行状态
# systemctl status chronyd
● chronyd.service – NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2026-04-02 10:00:00 CST; 1h 0min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
https://chrony.tuxfamily.org/documentation.html
Main PID: 1234 (chronyd)
Tasks: 1 (limit: 4915)
Memory: 1.2M
CGroup: /system.slice/chronyd.service
└─1234 /usr/sbin/chronyd
# 检查2:查看NTP服务器连接状态
# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? ntp.aliyun.com 0 6 0 – +0ns[ +0ns] +/- 0ns
^? ntp.tencent.com 0 6 0 – +0ns[ +0ns] +/- 0ns
^? ntp.cn.pool.ntp.org 0 6 0 – +0ns[ +0ns] +/- 0ns
# 检查3:测试NTP服务器连通性
# ping -c 4 ntp.aliyun.com
PING ntp.aliyun.com (203.107.6.88) 56(84) bytes of data.
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=1 ttl=56 time=12.345 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=2 ttl=56 time=12.346 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=3 ttl=56 time=12.344 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=4 ttl=56 time=12.345 ms
— ntp.aliyun.com ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 12.344/12.345/12.346/0.000 ms
# 检查4:测试NTP端口连通性
# nc -zuv ntp.aliyun.com 123
Connection to ntp.aliyun.com 123 port [udp] succeeded!
# 检查5:查看防火墙规则
# firewall-cmd –list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh dhcpv6-client
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
# 解决方案:开放NTP服务端口
# firewall-cmd –permanent –add-service=ntp
success
# firewall-cmd –reload
success
# 验证:再次查看NTP服务器连接状态
# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp.aliyun.com 2 6 377 15 +123us[ +234us] +/- 15ms
^+ ntp.tencent.com 2 6 377 16 -234us[ -123us] +/- 16ms
^+ ntp.cn.pool.ntp.org 2 6 377 17 +345us[ +456us] +/- 18ms
# 问题2:时间偏差过大
# 检查1:查看时间同步跟踪信息
# chronyc tracking
Reference ID : C0A80164 (192.168.1.100)
Stratum : 3
Ref time (UTC) : Thu Apr 2 02:00:00 2026
System time : 5.000000000 seconds fast of NTP time
Last offset : +5.000123456 seconds
RMS offset : 5.000234567 seconds
Frequency : 0.000000000 ppm fast
Residual freq : +0.000000000 ppm
Skew : 0.000000000
Root delay : 0.012345678 seconds
Root dispersion : 0.023456789 seconds
Update interval : 64.0 seconds
Leap status : Normal
# 解决方案:手动触发时间同步
# chronyc makestep
200 OK
# 验证:再次查看时间同步跟踪信息
# chronyc tracking
Reference ID : C0A80164 (192.168.1.100)
Stratum : 3
Ref time (UTC) : Thu Apr 2 02:00:00 2026
System time : 0.000000000 seconds fast of NTP time
Last offset : +0.000123456 seconds
RMS offset : 0.000234567 seconds
Frequency : 0.000000000 ppm fast
Residual freq : +0.000000000 ppm
Skew : 0.000000000
Root delay : 0.012345678 seconds
Root dispersion : 0.023456789 seconds
Update interval : 64.0 seconds
Leap status : Normal
Part05-风哥经验总结与分享
5.1 时间同步最佳实践
时间同步最佳实践:
- 使用多个NTP服务器:配置2-3个NTP服务器
- 配置本地NTP服务器:减少对公共NTP服务器的依赖
- 使用iburst选项:加快初始同步速度
- 设置合理的阈值:避免时间跳跃过大
- 定期检查同步状态:使用chronyc命令检查
- 建立监控机制:监控时间偏差和同步状态
- 统一时区设置:所有服务器使用相同时区
5.2 安全加固建议
时间同步安全加固建议:
# 1. 限制NTP服务访问
# cat >> /etc/chrony.conf << 'EOF' # 限制NTP服务访问 allow 192.168.1.0/24 cmdallow 192.168.1.0/24 EOF # 2. 启用NTP认证 # cat >> /etc/chrony.conf << 'EOF' # 启用NTP认证 keyfile /etc/chrony.keys generatecommandkey EOF # 3. 生成NTP密钥 # chronyc genkey 1 Password: Generating key 1... Key 1 generated successfully. # 4. 配置NTP认证 # cat >> /etc/chrony.conf << 'EOF' # 配置NTP认证 server ntp.aliyun.com key 1 EOF # 5. 重启chronyd服务 # systemctl restart chronyd
5.3 工具推荐
推荐的时间同步工具:
- chronyd:RHEL 10默认的时间同步服务
- ntpd:传统的时间同步服务
- timedatectl:系统时间管理工具
- chronyc:chronyd管理命令
- ntpdate:手动时间同步工具
- hwclock:硬件时钟管理工具
本文档介绍了系统时间同步配置与验证实战,包括系统时间同步的概念、NTP协议、chronyd服务配置、timedatectl命令使用、本地NTP服务器搭建等内容。通过合理的时间同步配置,可以确保系统时间准确,保障系统稳定运行。更多学习教程公众号风哥教程itpux_com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
