步骤:

1. 放行防火墙、2.安装chrony服务、3.配置chrony服务端/客户端

1:放行防火墙

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

也可以选择关闭防火墙 or 永久关闭。

临时关闭:systemctl stop firewalld.service
永久禁用:systemctl disable firewalld.service

2:安装chrony服务

dnf install chrony -y

3:配置服务端/客户端

# vi /etc/chrony.conf

进入配置文件,删除或注释掉官方 NTP 上游服务器地址,手动配置 server 地址

 配置 server 为网上源时,服务器需有网络支持,否则配置局域网 server 或 127.0.0.1
#上游服务器,阿里云:
server time1.aliyun.com iburst
server time2.aliyun.com iburst
server time3.aliyun.com iburst

#本地:
server 127.0.0.1 iburst

限制网段

# Allow NTP client access from local network.
allow 192.168.1.1/24

即使未同步上游服务器也提供 server(当配置上游为本地并作为“服务端”时),如果上游时 ntp server,不打开此行注释也可以

# Serve time even if not synchronized to a time source.
local stratum 10

配置完成后,重启并设置开机启动;

systemctl restart chronyd.service 
&& 
systemctl enable chronyd.service --now

查看同步状态

# chronyc sources -v

210 Number of sources = 1

  .-- 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
===============================================================================
^* 192.168.1.66                3   6    17    16  +3424ns[ +110us] +/-   31ms

ip 为 ntp_server 地址。

附录:

  1. 查看日期时间、时区及NTP状态:
# timedatectl

2. 修改时区为上海

# timedatectl set-timezone Asia/Shanghai

3.修改本地日期时间

# timedatectl set-time "2021-03-14 23:00:00"(可以只修改其中一个)

4.ntp 开启/关闭

# timedatectl set-ntp true/flase