1. 首页 > Linux教程 > 正文

Linux教程FG241-时间同步服务配置

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

风哥提示:

本文档详细介绍Chrony时间同步服务的安装、配置和管理方法。

Part01-Chrony安装

1.1 安装Chrony服务

# 安装Chrony
$ sudo dnf install -y chrony
Last metadata expiration check: 0:45:23 ago on Fri 04 Apr 2026 00:00:15 AM CST.
Package chrony-4.3-1.el9.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

# 启动Chrony服务
$ sudo systemctl start chronyd

# 设置开机自启动
$ sudo systemctl enable chronyd
Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service.

# 查看服务状态
$ sudo systemctl status chronyd
● chronyd.service – NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; preset: enabled)
Active: active (running) since Fri 2026-04-04 00:00:00 CST; 10s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 12375 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited,from PG视频:www.itpux.com status=0/SUCCESS)
Main PID: 12376 (chronyd)
Tasks: 1 (limit: 49152)
Memory: 1.5M
CPU: 15ms
CGroup: /system.slice/chronyd.service
└─12376 /usr/sbin/chronyd

Apr 04 00:00:00 rhel10 systemd[1]: Starting NTP client/server…
Apr 04 00:00:00 rhel10 chronyd[12376]: chronyd version 4.3 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG)
Apr 04 00:00:00 rhel10 chronyd[12376]: Frequency -10.123 +/- 0.更多学习教程公众号风哥教程itpux_com456 ppm read from /var/lib/chrony/drift
Apr 04 00:00:00 rhel10 systemd[1]: Started NTP client/server.

# 配置防火墙
$ sudo firewall-cmd –permanent –add-service=ntp
success
$ sudo firewall-cmd –reload
success

# 查看当前时间
$ timedatectl
Local time: Fri 2026-04-04 00:00:30 CST
Universal time: Thu 2026-04-03 16:00:30 UTC
RTC time: Thu 2026-04-03 16:00:30
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

Part02-Chrony配置文件

2.1 配置chrony.conf

# 备份原配置文件
$ sudo cp /etc/chrony.conf /etc/chrony.conf.bak

# 编辑配置文件
$ sudo tee /etc/chrony.conf << EOF # 使用公网NTP服务器 server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst # 使用国内NTP服务器 server ntp.aliyun.com iburst server ntp.tencent.com iburst server ntp.学习交流加群风哥QQ113257174ntsc.ac.cn iburst # 记录时钟频率 driftfile /var/lib/chrony/drift # 允许本地网络同步 allow 192.168.1.学习交流加群风哥微信: itpux-com0/24 # 设置本地时钟层级 local stratum 10 # 日志配置 logdir /var/log/chrony log measurements statistics tracking # 步进时钟设置 makestep 1.0 3 rtcsync EOF # 重启服务 $ sudo systemctl restart chronyd # 查看时间源 $ chronyc sources MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 139.199.215.251 2 6 17 42 +15us[ +15us] +/- 33ms ^+ 203.107.6.88 2 6 17 42 -10us[ -10us] +/- 25ms ^- 120.25.115.20 2 6 17 43 +20us[ +20us] +/- 38ms ^? ntp.ntsc.ac.cn 0 6 0 - +0ns[ +0ns] +/- 0ns # 查看时间源统计 $ chronyc sourcestats Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== 139.199.215.251 12 7 46m -0.001 0.132 +15us 10us 203.107.6.88 12 7 46m +0.001 0.145 -10us 12us 120.25.115.20 11 6 45m +0.002 0.167 +20us 15us

Part03-Chrony客户端配置

3.1 配置客户端同步

# 配置客户端使用本地NTP服务器
$ sudo tee /etc/chrony.conf << EOF # 使用本地NTP服务器 server 192.168.1.100 iburst # 记录时钟频率 driftfile /var/lib/chrony/drift # 步进时钟设置 makestep 1.0 3 rtcsync # 日志配置 logdir /var/log/chrony EOF # 重启服务 $ sudo systemctl restart chronyd # 查看同步状态 $ chronyc tracking Reference ID : C0A80164 (192.168.1.100) Stratum : 3 Ref time (UTC) : Thu Apr 03 16:05:00 2026 System time : 0.000000123 seconds fast of NTP time Last offset : +0.000000456 seconds RMS offset : 0.000000789 seconds Frequency : 10.123 ppm fast Residual freq : +0.001 ppm Skew : 0.123 ppm Root delay : 0.012345 seconds Root dispersion : 0.001234 seconds Update interval : 64.2 seconds Leap status : Normal # 手动同步时间 $ sudo chronyc makestep 200 OK # 查看客户端列表 $ sudo chronyc clients Hostname NTP Drop Int IntL Last Cmd Drop Int Last =============================================================================== 192.168.1.10 2 0 6 - 30 0 0 - - 192.168.1.11 1 0 6 - 45 0 0 - - # 立即同步时间 $ sudo chronyc -a makestep 200 OK 200 OK

Part04-时区配置

4.1 配置系统时区

# 查看可用时区
$ timedatectl list-timezones | grep Asia
Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau
Asia/Aqtobe
Asia/Ashgabat
Asia/Atyrau
Asia/Baghdad
Asia/Bahrain
Asia/Baku
Asia/Bangkok
Asia/Barnaul
Asia/Beirut
Asia/Bishkek
Asia/Brunei
Asia/Chita
Asia/Choibalsan
Asia/Colombo
Asia/Damascus
Asia/Dhaka
Asia/Dili
Asia/Dubai
Asia/Dushanbe
Asia/Famagusta
Asia/Gaza
Asia/Hebron
Asia/Ho_Chi_Minh
Asia/Hong_Kong
Asia/Hovd
Asia/Irkutsk
Asia/Jakarta
Asia/Jayapura
Asia/Jerusalem
Asia/Kabul
Asia/Kamchatka
Asia/Karachi
Asia/Kathmandu
Asia/Khandyga
Asia/Kolkata
Asia/Krasnoyarsk
Asia/Kuala_Lumpur
Asia/Kuching
Asia/Kuwait
Asia/Macau
Asia/Magadan
Asia/Makassar
Asia/Manila
Asia/Muscat
Asia/Nicosia
Asia/Novokuznetsk
Asia/Novosibirsk
Asia/Omsk
Asia/Oral
Asia/Phnom_Penh
Asia/Pontianak
Asia/Pyongyang
Asia/Qatar
Asia/Qostanay
Asia/Qyzylorda
Asia/Riyadh
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
Asia/Taipei
Asia/Tashkent
Asia/Tbilisi
Asia/Tehran
Asia/Thimphu
Asia/Tokyo
Asia/Tomsk
Asia/Ulaanbaatar
Asia/Urumqi
Asia/Ust-Nera
Asia/Vientiane
Asia/Vladivostok
Asia/Yakutsk
Asia/Yangon
Asia/Yekaterinburg
Asia/Yerevan

# 设置时区
$ sudo timedatectl set-timezone Asia/Shanghai

# 查看当前时区
$ timedatectl
Local time: Fri 2026-04-04 00:10:00 CST
Universal time: Thu 2026-04-03 16:10:00 UTC
RTC time: Thu 2026-04-03 16:10:00
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

# 设置硬件时钟为UTC
$ sudo timedatectl set-local-rtc 0

# 设置硬件时钟为本地时间
$ sudo timedatectl set-local-rtc 1

# 手动设置时间
$ sudo timedatectl set-time “2026-04-04 00:15:00”

Part05-Chrony监控

5.1 监控时间同步

# 查看同步状态
$ chronyc tracking
Reference ID : C0A80164 (192.168.1.100)
Stratum : 3
Ref time (UTC) : Thu Apr 03 16:15:00 2026
System time : 0.000000123 seconds fast of NTP time
Last offset : +0.000000456 seconds
RMS offset : 0.000000789 seconds
Frequency : 10.123 ppm fast
Residual freq : +0.001 ppm
Skew : 0.123 ppm
Root delay : 0.012345 seconds
Root dispersion : 0.001234 seconds
Update interval : 64.2 seconds
Leap status : Normal

# 查看活动状态
$ chronyc activity
200 OK
4 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

# 查看时间源
$ chronyc sources -v
210 Number of sources = 4

.– Source mode ‘^’ = server, ‘=’ = peer, ‘#’ = local clock.
/ .- Source state ‘*’ = current synced, ‘+’ = combined , ‘-‘ = not combined,
| / ‘?’ = unreachable, ‘x’ = time may be in error, ‘~’ = time too variable.
|| .- 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
===============================================================================
^* 139.199.215.251 2 6 37 62 +15us[ +15us] +/- 33ms
^+ 203.107.6.88 2 6 37 62 -10us[ -10us] +/- 25ms
^- 120.25.115.20 2 6 37 63 +20us[ +20us] +/- 38ms
^? ntp.ntsc.ac.cn 0 6 0 – +0ns[ +0ns] +/- 0ns

# 查看日志
$ sudo tail -f /var/log/chrony/tracking.log
2026-04-04 00:15:00 192.168.1.100 1234567890.123456789 0.000000123 0.000000456 0.000000789 10.123 0.001 0.123 0.012345 0.001234 64.2 Normal

# 检查NTP服务状态
$ timedatectl timesync-status
Server: 192.168.1.100 (192.168.1.100)
Poll interval: 1min 4s (min: 32s; max 34min 8s)
Leap: normal
Version: 4
Stratum: 3
Precision: 1us (-24)
Root distance: 12.345ms (max: 5s)
Offset: +123us
Peer delay: 1.更多视频教程www.fgedu.net.cn234ms
Peer dispersion: 1.234ms
Response delay: 1.234ms
Jitter: 0us
Packet count: 100
Packet errors: 0

风哥针对配置建议:
1. 配置多个NTP服务器提高可用性
2. 使用本地NTP服务器减少网络延迟
3. 监控时间同步状态
4. 定期检查时钟偏差
5. 配置合理的时区设置

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息