Linux主机配置为Chrony服务器

本文介绍了CentOS7中使用Chrony作为时间同步服务的配置和管理,包括安装、启动、防火墙设置、配置文件详解、时区设置、服务器集群间时间同步以及常用命令。通过Chrony,可以实现系统与NTP服务器的精确时间同步,并在内网环境中搭建时间服务器,确保多台服务器时间一致。
摘要由CSDN通过智能技术生成

【Chrony介绍】

Chrony是一个开源的自由软件,已经是Centos7以后代替NTP服务的默认服务,默认配置文件在 /etc/chrony.conf,它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对于NTP时间同步软件,占据很大优势。其用法也很简单。

【Chrony环境准备】

1、SSH登录主机。
在这里插入图片描述2、检查系统是否已安装chrony,如未安装请执行以下命令安装:

yum install chrony -y

3、启动并加入开机自启动

systemctl enable chronyd.service --now
systemctl status chronyd.service

4、firewalld设置

#firewall-cmd --add-service=ntp --permanent
#firewall-cmd --reload
因NTP使用123/UDP端口协议,所以允许NTP服务即可。

【Chrony环境配置】

以下是系统默认配置文件及注释:

# cat /etc/chrony.conf

# 使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。
# 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

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
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

# Serve time even if not synchronized to a time source.
local stratum 10

# 指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys

# 指定日志文件的目录。
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

【配置时区】

1、设置当前系统为Asia/Shanghai上海时区。

timedatectl set-timezone Asia/Shanghai

3、设置完时区后,强制同步下系统时钟。

chronyc -a makestep

在这里插入图片描述
2、 将你的硬件时钟设置为本地时区。

timedatectl set-local-rtc 1

3、启用NTP时间同步。

timedatectl set-ntp yes

4、重启chronyd服务。

systemctl restart chronyd

【服务器集群之间的系统时间同步】

在生产环境中,其网络都是内网结构,那么内网如何保证服务器之间的时间同步呢?其实这个问题很简单,只需要搭建一台内网时间服务器,然后让所有计算机都到服务端(192.168.5.100)去同步时间即可。

具体操作:在服务端注释以下内容:

#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.5.100 iburst

这样我们需求的一台内网时间服务器已经配置完毕。

同样在客户端注释掉其他server,并在客户端配置文件中添加以下:

#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

并添加以下内容:(表示与chrony服务器同步时间)

server 192.168.5.100 iburst

到此已经完成系统时间的同步。如有多台机器,操作也是如此。

【常用命令】

1、查看时间同步源。

chronyc sources -v

在这里插入图片描述2、查看时间同步源状态:

chronyc sourcestats -v

3、硬件时间默认为UTC:

timedatectl set-local-rtc 1

4、启用NTP时间同步:

timedatectl set-ntp yes
最后需要注意的是,配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值