# Linux公共服务组件之时间同步chrony

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/9980.html | 发布：2022-09-14 | 更新：2022-09-14 | 分类：建站教程、投稿分享

## 一、简介

 

定义：chrony 是网络时间协议（NTP）的通用实现。

 

chrony 包含两个程序：chronyd 是一个可以在启动时启动的守护程序。chronyc 是一个命令行界面程序，用于监视 chronyd 的性能并在运行时更改各种操作参数。

 

**时间**概念：

 

**UTC** (Universal Time Coordinated)：世界标准时间，属于世界统一世界

 

**GMT** (Greenwich Mean Time)：英国伦敦郊区的皇家格林尼治天文台的标准时间，因为本初子午线被定义在通过那里的经线。（UTC与GMT时间基本相同，因此不做区分）

 

**CST** (China Standard Time)：中国标准时间，CMT + 8 = UTC + 8 = CST

 

**DST**(Daylight Saving Time)：夏令时指在夏天太阳升起的比较早时，将时间拨快一小时，以提早日光的使用。

 

## 二、安装配置

 

测试环境：

 

| IP | 备注 |
|---|---|
| 120.10.10.10 | Chrony服务端 |
| 120.10.10.11 | Chrony客户端 |

 

**服务端和客户端**均安装软件包：

 

yum -y install chrony  
systemctl enable chronyd  
systemctl start chronyd

 

**服务端配置**，注意两个改动：

 

# 使用 pool.ntp.org 项目中的公共服务器。以server开，理论上想添加多少时间服务器都可以。  
# 建议添加ping较低的公网服务器地址  
# 改动1  
# Use public servers from the pool.ntp.org project.  
# Please consider joining the pool (http://www.pool.ntp.org/join.html).  
server ntp.aliyun.com iburst  
server time.asia.apple.com iburst  
server time.windows.com iburst  
server cn.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

 

# 改动2  
# 指定 NTP 客户端地址或网段，以允许或拒绝连接到扮演时钟服务器的机器  
# Allow NTP client access from local network.  
allow 120.10.10.0/24

 

# 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

 

**服务端**开启同步功能：

 

systemctl enable chronyd  
systemctl restart chronyd

 

# 查看时间同步状态  
timedatectl status  
# 开启网络时间同步  
timedatectl set-ntp true

 

**客户端配置**，也注意两个改动：

 

# 使用 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).  
# 改动1  
server 120.10.10.10 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.  
# 改动2  
allow 120.10.10.10

 

# 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

 

## 三、使用

 

修改时区或日期时间(首先配置)：

 

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

 

# 查看时区列表  
timedatectl list-timezones  
timedatectl list-timezones | grep -E "Asia/S.*"

 

# 修改时区  
timedatectl set-timezone Asia/Shanghai

 

# 修改日期时间（可以只修改其中一个）  
timedatectl set-time "2019-09-19 15:50:20"

 

# 开启 NTP，true表示开启  
timedatectl set-ntp true/flase

 

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

 

# 查看时区列表  
timedatectl list-timezones  
timedatectl list-timezones | grep -E "Asia/S.*"

 

# 修改时区  
timedatectl set-timezone Asia/Shanghai

 

# 修改日期时间（可以只修改其中一个）  
timedatectl set-time "2019-09-19 15:50:20"

 

# 开启 NTP，true表示开启  
timedatectl set-ntp true/flase

 

Chronyc命令：

 

# 查看 ntp_servers  
chronyc sources -v

 

# 查看 ntp_servers 状态  
chronyc sourcestats -v

 

# 查看 ntp_servers 是否在线  
chronyc activity -v

 

# 查看 ntp 详细信息  
chronyc tracking -v

 

![Linux公共服务组件之时间同步chrony](https://www.mfisp.com/wp-content/uploads/2022/01/13e1e49bb26b66e_1_post.png)

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
