文章目录
- 前言
- 一、介绍chrony
- 二、安装前准备
- 三、安装chrony
- 3.1. 在线安装chrony
- 3.2. 离线安装chrony
- 四、服务端配置
- 4.1. 配置服务端
- 4.1.1. 服务端
- 4.1.2. 客户端
- 4.2. 设置开机自启
- 五、常用命令
- 六、安装验证
- 七、国内常用NTP服务器
- 总结
前言
本文章主要介绍在Centos系统,安装chrony程序,需要保持系统时钟与时钟服务器(NTP)同步,让时间保持精确;不管是内网集群还是外网集群都可参考该文档进行安装。
一、介绍chrony
Chrony是一个开源的自由软件,默认配置文件在 /etc/chrony.conf
它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对同步软件来说,它速度更快、配置和依赖都更简单。
Chrony有两个程序组成,分别是:
- chronyd:是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步;它确定计算机增减时间的比率,并对此进行补偿。
- chronyc:提供一个用户界面,用于监控性能并进行多样化的配置;它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。
二、安装前准备
检查是否已安装chrony
# 使用命令查看
rpm -qa |grep chrony
如果未安装,将输出 package chrony is not installed
如已经安装,请直接查看四、服务端配置
三、安装chrony
3.1. 在线安装chrony
机器本身是可以访问互联网,执行安装命令,等待安装完成
# 安装命令
sudo yum install chrony -y
3.2. 离线安装chrony
从官网下载源码包:https://download.tuxfamily.org/chrony
当前博主下载的是chrony-3.2.tar.gz
包,可根据需求选择,注意:最好下载.tar.gz
结尾的包。
将包传输到指定的上,执行命令,等待安装完成。
# 解压包
tar -xzvf chrony-3.2.tar.gz
# 配置安装路径
cd chrony-3.2
sudo ./configure --prefix=/usr/sbin/chronyd
# 编译和安装
sudo make && make install
四、服务端配置
4.1. 配置服务端
vi /etc/chrony.conf
该文件是配置路径,客户端与服务端配置最大的区别是server ip iburst
# 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
server 192.168.0.1 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 0.0.0.0/0
# Serve time even if not synchronized to a time source.
local stratum 10
# 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.
4.1.1. 服务端
如果通公网,建议配置为公网ntp服务器域名(参考国内的NTP服务器域名):
`server cn.pool.ntp.org iburst`
不通公网及配置为本机ip,若是ntp集群,就把集群都配置上:
`server 192.168.0.1 iburst`
注意:国内主机用国内的ntp服务器域名,海外主机用海外的ntp服务器域名;错用可能会导致网络不通,时间同步失败。
4.1.2. 客户端
客户端配置只需要将ip,修改为刚刚部署的ntp服务器ip即可,配置的数量不限制。
4.2. 设置开机自启
# 启动chrony服务
systemctl start chronyd.service
# 查看服务状态
systemctl enable chronyd.service
# 设置开机自启
systemctl status chronyd.service
五、常用命令
# 查看时间同步源:
chronyc sources -v
# 立即同步
chronyc -a makestep
# 查看时间同步源状态:
chronyc sourcestats -v
# 设置硬件时间, 硬件时间默认为UTC:
timedatectl set-local-rtc 1
# 启用NTP时间同步:
timedatectl set-ntp yes
#校准时间服务器(use):
chronyc tracking
注意:修改配置后,需重启chrony服务,否则可能会不生效
示例: 查看 Chrony 服务是否与 NTP 服务器同步
# 执行命令
chronyc tracking
# 同步过程正常
Leap status : Normal
# 同步过程出错
Leap status: Not synchronised
# Chrony 服务未正常运行
506 Cannot talk to Daemon
六、安装验证
1.chrony服务端机器,执行命令chronyc sources -v
出现如上即为配置成功
2.chrony客户端机器,执行命令chronyc sources -v
出现如上即为配置成功
Ps.以上图片最后一行的ip(223.255.185.2、192.168.1.110)是配置的ntp服务器解析出来的ip(域名)与配置有关。
七、国内常用NTP服务器
1.可靠易用的NTP服务的虚拟集群
pool.ntp.org:提供可靠易用的NTP服务的虚拟集群
- cn.pool.ntp.org
- 03.cn.pool.ntp.org
2.阿里云公共NTP服务器
Unix/linux类:
- ntp.aliyun.com
- ntp1-7.aliyun.com
windows类:
- time.pool.aliyun.com
3.腾讯公共NTP服务器:
time1-5.cloud.tencent.com
4.大学NTP服务器:
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
5.国家授时中心服务器:
210.72.145.44
总结
好的,以上就是chrony安装的步骤,欢迎大家有任何问题均可留言。
如在安装过程中遇到问题,请留言,我将汇总问题及处理办法,持续改进完善该文档,感谢大家的指导就是我不断改进的动力。