Linux篇之采用Chrony实现网络时间同步

一、Chrony简介
chrony 是网络时间协议NTP的通用实现。
chrony的程序执行包含两类:1、chronyd 2、chronyc
chronyd 是一个可以在启动时启动的守护程序。chronyc 是一个命令行界面程序,用于监视 chronyd 的性能并在运行时更改各种操作参数。

二、Chrony安装
1、chrony下载

yum install -y chrony

2、下载成功后,可以通过开机自启

systemctl  enable chronyd
systemctl start chronyd

3、firewalld设置

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

4、配置Chrony
4.1、服务端配置
vim 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 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.8.8 iburst   # server 为 本机时间服务器
 
# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
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.8.0/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

4.2、开启时间同步

systemctl enable chronyd
systemctl restart chronyd

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

4.3、客户端配置
1、chrony.conf 修改两处

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.8.5 iburst

# Allow NTP client access from local network.
allow 192.168.8.5      # 对应clinet端机器

2、开启时间同步

systemctl enable chronyd
systemctl restart chronyd

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

在这里插入图片描述
三、Chrony命令
1、chronyc 用法

# 查看 ntp_servers
chronyc sources -v

# 查看 ntp_servers 状态
chronyc sourcestats -v

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

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

2、修改时区方法

# 查看日期时间、时区及 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
timedatectl set-ntp true/flase

OK~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值