基于chrony的NTP校时配置方法

基于chrony的NTP校时配置方法

基本原理描述

1.1 原理

在离线的局域网内,存在不少于2台服务器。

选择一台服务器作为时间基准,配置为NTP Server;

其他服务器向时间基准服务器同步,配置为NTP Client

1.2 描述

配置方法

2.1 NTP Server配置方法

1) 首先,确保操作系统防火墙关闭。

通过执行sudo systemctl status firewalld命令查看;

若未关闭,则执行

sudo systemctl stop firewalld

sudo systemctl disable firewalld

进行防火墙关闭。

2) 检查chronyd服务是否安装和激活

通过执行sudo systemctl status chronyd命令查看;

若未安装和激活,则执行以下命令

安装命令: sudo yum install chronyd

激活命令: sudo systemclt start chronyd

sudo systemctl enable chronyd

3) 更改配置文件,配置为server模式

编辑文件/etc/chrony.conf

内容如下:

 

# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).#pool pool.ntp.org iburstserver ntp.ntsc.ac.cn iburstserver ntp1.aliyun.com iburst#server cn.pool.ntp.org iburst# 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.makestep 1.0 3# Enable kernel synchronization of the real-time clock (RTC).rtcsync# 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# 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# Specify file containing keys for NTP authentication.#keyfile /etc/chrony.keys# 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

 

 

 

4) 重新运行服务,自动加载更新的配置文件

sudo systemctl restart chronyd

5) 激活服务

运行命令: chronyc activity

应有类似如下返回

200 OK

0 sources online

0 sources offline

0 sources doing burst (return to online)

0 sources doing burst (return to offline)

2 sources with unknown address

 

6) 查看同步源

执行命令 chronyc sources

因为离线,返回如下

210 Number of sources = 0

MS Name/IP address         Stratum Poll Reach LastRx Last sample               

 

7) 查看同步情况

执行命令 chronyc tracking

返回如下:

Reference ID    : 7F7F0101 ()

Stratum         : 10

Ref time (UTC)  : Sat Sep 13 03:05:36 2025

System time     : 0.000000000 seconds fast of NTP time

Last offset     : +0.000000000 seconds

RMS offset      : 0.000000000 seconds

Frequency       : 0.000 ppm slow

Residual freq   : +0.000 ppm

Skew            : 0.000 ppm

Root delay      : 0.000000000 seconds

Root dispersion : 0.000000000 seconds

Update interval : 0.0 seconds

Leap status     : Normal

 

 

2.2 NTP Client配置方法

1) 首先,确保操作系统防火墙关闭。

通过执行sudo systemctl status firewalld命令查看;

若未关闭,则执行

sudo systemctl stop firewalld

sudo systemctl disable firewalld

进行防火墙关闭。

2) 检查chronyd服务是否安装和激活

通过执行sudo systemctl status chronyd命令查看;

若为安装和激活,则执行以下命令

安装命令 sudo yum install chronyd

激活命令 sudo systemclt start chronyd

sudo systemctl enable chronyd

3) 更改配置文件,配置为server模式

编辑文件/etc/chrony.conf

内容如下:

 

# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).#pool pool.ntp.org iburst# 注意以下是server的ip地址server 192.168.1.86 iburstserver ntp.ntsc.ac.cn iburstserver ntp1.aliyun.com iburst#server cn.pool.ntp.org iburst# 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.makestep 1.0 3# Enable kernel synchronization of the real-time clock (RTC).rtcsync# 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# 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# Specify file containing keys for NTP authentication.#keyfile /etc/chrony.keys# 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

 

 

4) 重新运行服务,自动加载更新的配置文件

sudo systemctl restart chronyd

5) 激活服务

运行命令 chronyc activity

应有类似如下返回

200 OK

0 sources online

0 sources offline

0 sources doing burst (return to online)

0 sources doing burst (return to offline)

2 sources with unknown address

 

6) 查看同步源

执行命令 chronyc sources

返回如下,应该有serverip地址

210 Number of sources = 1

MS Name/IP address         Stratum Poll Reach LastRx Last sample               

===============================================================================

^* 192.168.1.86                 10   6    17     6  +4489ns[  +14us] +/-   35us

 

7) 查看同步情况

执行命令 chronyc tracking

返回如下:

Reference ID    : C0A80156 (192.168.1.86)

Stratum         : 11

Ref time (UTC)  : Sat Sep 13 03:10:51 2025

System time     : 0.000000002 seconds slow of NTP time

Last offset     : +0.000009667 seconds

RMS offset      : 0.000009667 seconds

Frequency       : 3.457 ppm fast

Residual freq   : +0.000 ppm

Skew            : 174.367 ppm

Root delay      : 0.000068986 seconds

Root dispersion : 0.006962806 seconds

Update interval : 2.0 seconds

Leap status     : Normal