所以就出现了chrony
# 硬件时间查看
[root@server ~]# hwclock
2023-11-18 23:00:27.416227+08:00
[root@server ~]# date -s 10:00 # 修改为错误时间
2023年 08月 20日 星期日 10:00:00 CST
[root@server ~]# date # 查看软件时间
2023年 08月 20日 星期日 10:00:06 CST
[root@server ~]# hwclock -s # 向硬件时间同步
[root@server ~]# date # 查看软件时间
2023年 08月 20日 星期日 09:19:22 CST
注意:Chrony与NTP都是时间同步软件,两个软件不能同时开启,会出现时间冲突,RHEL9默认使用Chrony作为时间服务器,不再支持NTP软件包
[root@server ~]# yum install chrony -y
[root@server ~]# systemctl start chronyd
[root@server ~]# systemctl enable chronyd
[root@server ~]# systemctl status chronyd
[root@server ~]# 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.ntp.org项目中的公共服务器,或者使用server开头的服务器,理论上想添加多少时间服务器都可以,iburst表示的是首次同步的时候快速同步
pool 2.centos.pool.ntp.org iburst
#pool:项目名,也可以使用server
#向2.centos.pool.ntp.org(域名)服务器进行时间同步
#中间也可以直接写ip地址
#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.
# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中被步进
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
#启用实时时钟(RTC)的内核同步
rtcsync
#通过使用hwtimestamp指令启用硬件时间戳
# 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
#指定NTP客户端地址,以允许或拒绝连接到扮演时钟服务器的机器,默认允许所有
# 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
#指定包含NTP身份验证密钥的文件
# 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
# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC
#指定日志文件的目录
# Specify directory for log files.
logdir /var/log/chrony
#选择日志文件要记录的信息
# Select which information is logged.
#log measurements statistics tracking
同步时间
[root@server ~]# date -s 12:00
2023年 11月 19日 星期日 12:00:00 CST
[root@server ~]# vim /etc/chrony.conf # 定位第3行,修改为阿里云的时间同步服务器地址
server ntp.aliyun.com iburst
[root@server ~]# vim /etc/chrony.conf
server ntp.aliyun.com iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
logchange 0.5
logdir /var/log/chrony
[root@server ~]# systemctl restart chronyd
[root@server ~]# date
2023年 11月 19日 星期日 00:53:49 CST
# 查看时间是否同步
[root@server ~]# 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
===============================================================================
^* 203.107.6.88 2 6 17 20 +217us[+1210us] +/- 30ms
[root@server ~]# timedatectl status # 查看状态
Local time: 日 2023-11-19 00:59:17 CST
Universal time: 六 2023-11-18 16:59:17 UTC
RTC time: 六 2023-11-18 16:59:17
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes # yes表示已经完成时间同步,no表示未完成时间同步
NTP service: active
RTC in local TZ: no
搭建本地时间服务器
性质 | 地址 | 同步对象(向谁同步) |
---|---|---|
服务端server | 192.168.80.129 | ntp.aliyun.com |
客户端node1 | 192.168.80.130 | 192.168.80.129 |
要求
第一步:定位server端
[root@server ~]# vim /etc/chrony.conf
server ntp.aliyun.com iburst # 修改第3行为阿里的时间服务器地址
allow 192.168.80.130/24 # 删除第26行的前导星号,启用白名单,将node1的ip添加进入
[root@server ~]# systemctl restart chronyd # 重启服务
[root@server ~]# chronyc sources -v # 同步测试
[root@server ~]# timedatectl status # 查看是否同步
[root@node1 ~]# vim /etc/chrony.conf
server 192.168.80.129 iburst # 修改为向server端同步时间
[root@node1 ~]# systemctl restart chronyd # 重启服务
[root@node1 ~]# timedatectl status
[root@server ~]# 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.80.129 3 6 17 27 +2349ns[ +33us] +/- 37ms
[root@server ~]# chronyc sources -v # -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
===============================================================================
^* 203.107.6.88 2 6 377 0 +1431us[+1789us] +/- 38ms
[root@server ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 377 26 +1443us[+2227us] +/- 31ms
[root@server ~]# chronyc sourcestats -v
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
203.107.6.88 22 13 22m +0.021 4.603 +377ns 2373us
[root@server ~]# chronyc activity -v
200 OK # 表示在线
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
[root@server ~]# chronyc -a makestep
200 OK