使用Chrony配置 NTP

前言:

官方参考文档: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-configuring_ntp_using_the_chrony_suite

在IT领域,出于多种原因,准确计时很重要。例如,在网络中,数据包和日志中需要准确的时间戳。在 Linux 系统中,该NTP协议由运行在用户空间中的守护进程实现。

用户空间守护进程更新内核中运行的系统时钟。系统时钟可以通过使用各种时钟源来计时。通常使用时间戳计数器( TSC )。TSC 是一个 CPU 寄存器,用于计算自上次复位以来的周期数。它非常快,具有高分辨率,并且没有中断。

有两个时间同步守护进程可供选择:ntpd和chronyd。不能同时存在,只能用其中一个.
两个服务的优缺点可以在官方文档中查看,这里不细说,直接说如何使用。

简介

Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算机提供时间同步服务。

Chrony由两个程序组成,分别是chronyd和chronyc

chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步。
RHEL7中默认使用chrony作为时间服务器,也支持NTP,需要额外安装。

安装与配置chrony

安装chrony

# yum install chrony

配置解释

Chrony的配置文件是/etc/chrony.conf

# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3

# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (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

# 指定包含 NTP 身份验证密钥的文件。
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# 指定日志文件的目录。
# Specify directory for log files.
logdir /var/log/chrony

# 选择日志文件要记录的信息。
# Select which information is logged.
#log measurements statistics tracking

配置

服务器端设置

# 加一行国内的阿里云时间服务器
server ntp.aliyun.com iburst
allow 192.168.0.0/24

启动并设置开机自启动

sudo systemctl enable --now chronyd

开通防火墙允许客户端访问

sudo firewall-cmd --permanent --zone=public --add-port=123/udp
sudo firewall-cmd --reload

客户端设置

编辑配置文件

# 192.168.0.80为服务器端的地址
server 192.168.0.80
allow 192.168.0.80

设置开机自启动,并重启

systemctl enable --now chronyd
systemctl restart  chronyd

查看时间同步设置

timedatectl status

开启网络时间同步
timedatectl set-ntp true

检查 chrony 是否已同步

要检查是否chrony是同步的,利用的tracking,sources和sourcestats命令。

chronyc tracking
chronyc sources
chronyc sourcestats

手动调整系统时钟

要立即步进系统时钟,绕过任何正在进行的调整,请发出以下命令

`chronyc makestep`
原文地址:https://www.cnblogs.com/zydev/p/15688530.html