一、简介
linux操作系统中时钟同步服务一般是ntp,直接dnf install -y ntp即可自动安装,但是centos操作系统中时钟同步服务为chrony,该时钟同步服务配置也很简单,与ntp类似,下面介绍下安装与简单配置
二、安装
dnf命令自动安装:
dnf install -y chrony
安装完成后启动程序并查看程序状态,常用命令:
systemctl status chronyd //查看程序状态
systemctl start chronyd //启动程序
systemctl stop chronyd //停止程序
systemctl restart chronyd //重启程序
三、配置
chronyd程序的配置文件在/etc/chrony.conf
# 使用的始终同步服务器配置,可配置多个,一般建议ntp.aliyun.com
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
driftfile /var/lib/chrony/drift
# chronyd根据需求减慢或加速时间调整,
# 在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
# 该指令强制chronyd调整时期,大于某个阀值时步进调整系统时钟。
# 只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3
# 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
rtcsync
# Enable hardware timestamping on all interfaces that support it.
# 通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#allow 192.168.0.0/16
#deny 192.168/16
allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
local stratum 10
# 指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys
# 指定日志文件的目录。
logdir /var/log/com/chrony
四、常用命令
Chrony包含chronyd一个在用户空间中运行的守护程序,以及chronyc一个命令行程序,可用于监视其性能chronyd并在运行时更改各种操作参数。常用命令如下:
chronyc -v //查看版本
chronyc sources -v //查看配置的时钟服务器连接情况 ,^*表示正常
chronyc -a makestep //手动同步
五、 问题处理
在配置完成后,查看始终未同步,有以下可能原因:
1.防火墙未放通123端口
查看防火墙状态:
service firewalld status
防火墙开启,执行以下命令放通123端口
firewall-cmd --permanent --add-port=123/tcp //永久添加tcp端口123
firewall-cmd --reload //配置修改后重新加载
firewall-cmd --list-ports //查看123端口在列表中则表示成功
2.时区未修改
博主在配置时,配置防火墙均修改完了,检查了也没问题,但是时间一直都差了一天左右,查看了发现时区配错了,虚拟机配的是纽约时区,我们可以修改为上海时区就可以了,指令如下:
timedatectl //查看已配置的时区
timedatectl list-timezone //列出所有时区
(这里我们可以过滤下执行:timedatectl list-timezone|grep Asia/S,找到Asia/Shanghai)
timedatectl set-timezone Asia/Shanghai //设置时区为上海时区
修改完后再执行timedatectl查看时区成功修改