系统运维-Linux使用Chrony实现NTP时钟同步服务

Chrony:是一个开源自由的网络时间协议NTP的服务端和客户端软件 它能使服务端系统时间保持与服务器系统时间同步

实验环境

  • 服务端:192.168.100.200
  • 客户端:192.168.100.10

首先一般默认都是已安装在Linux系统中的 若没有安装 则执行安装

yum install chrony* -y

然后编辑服务端的chrony配置文件

vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
#pool 2.pool.ntp.org iburst        //注释此行
server 192.168.100.200 iburst      //添加此行 server+服务端ip+iburst

# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp

# 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 192.168.100.0/24        //要设置允许的网络段

# Serve time even if not synchronized to a time source.
local stratum 10                //取消注释此行

# Require authentication (nts or key option) for all NTP sources.
#authselectmode require

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# Insert/delete leap seconds by slewing instead of stepping.
#leapsecmode slew

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

# Specify directory for log files.
logdir /var/log/chrony

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

服务器端就配置完成了 接下来保存退出

配置客户端

vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
#pool 2.pool.ntp.org iburst        //注释此行
server 192.168.100.200 iburst      //添加此行 server+服务端ip+iburst

# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp

# 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 192.168.0.0/16

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

# Require authentication (nts or key option) for all NTP sources.
#authselectmode require

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# Insert/delete leap seconds by slewing instead of stepping.
#leapsecmode slew

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

# Specify directory for log files.
logdir /var/log/chrony

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

客户端也就配置完成了 接下来重启并设置自启服务端和客户端的chrony

systemctl restart chronyd		#重启chrony
systemctl enable chronyd		#设置自启

接下来客户端查看同步情况

chronyc sources					#客户机查看同步源

MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.100.200            10  10   377   759  -8687ns[-9982ns] +/-  227us			#同步成功

就同步成功了 如果同步失败检查是否关闭了selinux和firewalld

 

欢迎交流学习

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会为您提供如下步骤: 1. 在 linux1 上安装 chrony: ``` sudo apt-get install chrony ``` 2. 修改 chrony配置文件 /etc/chrony/chrony.conf,将以下内容添加到文件末尾: ``` allow 192.168.1.0/24 # 允许其他主机访问该 NTP 服务器 ``` 请根据您的网络环境修改 IP 地址。 3. 启动 chrony 服务: ``` sudo systemctl start chrony ``` 4. 确认 chrony 服务运行正常: ``` sudo systemctl status chrony ``` 如果服务运行正常,将会看到如下输出: ``` ● chrony.service - chrony, an NTP client/server Loaded: loaded (/lib/systemd/system/chrony.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2021-10-10 16:48:36 CST; 3s ago Docs: man:chronyd(8) man:chronyc(1) man:chrony.conf(5) Main PID: 12606 (chronyd) Tasks: 1 (limit: 4915) CGroup: /system.slice/chrony.service └─12606 /usr/sbin/chronyd Oct 10 16:48:36 linux1 systemd[1]: Started chrony, an NTP client/server. ``` 5. 在其他 Linux 主机上配置 chrony 客户端,将 linux1 的 IP 地址作为 NTP 服务器地址。 ``` sudo apt-get install chrony sudo sed -i 's/^pool/#pool/g' /etc/chrony/chrony.conf sudo sed -i 's/^server/#server/g' /etc/chrony/chrony.conf echo 'server 192.168.1.100 iburst' | sudo tee -a /etc/chrony/chrony.conf sudo systemctl restart chrony ``` 请将 192.168.1.100 修改为实际的 linux1 的 IP 地址。 6. 确认客户端的时间同步正常: ``` chronyc tracking ``` 如果同步正常,将会看到如下输出: ``` Reference ID : 192.168.1.100 (linux1) Stratum : 3 Ref time (UTC) : Sun Oct 10 09:50:31 2021 System time : 0.000000016 seconds slow of NTP time Last offset : +0.000005217 seconds RMS offset : 0.000005217 seconds Frequency : 1.542 ppm slow Residual freq : +0.000 ppm Skew : 0.036 ppm Root delay : 0.004697 seconds Root dispersion : 0.000937 seconds Update interval : 64.4 seconds Leap status : Normal ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾柒SHY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值