linux时间同步工具chrony的配置和时间设置的相关说明

目录

目录

介绍

1.搭建ntp服务器

 2.配置ntp客户端

3.其他设置

 4.客户端无法进行时间同步


介绍

目前比较流行的时间同步工具有ntpd和chrony,ntpd采用123/UDP端口通信,chrony采用323/UDP端口通信。Centos7以上版本默认安装chrony服务来同步时间,而Centos6则默认安装ntpd服务。如果chrony作为时间同步的服务器,如下图,Chrony默认listen端口是323,通过 323 端口与 chrond 交互,可监控 chronyd 的性能并在运行时更改各种操作参数;chronyd 在后台静默运行并通过 123 端口与时间服务器定时同步时间。机器2作为时间服务器,则需要开放123/udp给客户端服务器。

选择 chrony 是有原因的,相比 ntpd , 它有如下的优点:

1、时间同步的速度比 ntpd 更快

2、chrony 很好的处理了同步延迟以及网络延迟

3、即使出现网络降级,chrony 仍然能正常工作

4、本地机器可以作为时间服务器,其他机器从这台服务器上同步时间

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 配置NTP服务器
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 ntp.aliyun.com iburst
# Record the rate at which the system clock gains/losses time.

# 记录系统时钟获得/丢失时间的速率至drift文件中
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.
# 默认情况下,chronyd通过减慢或加快时钟速度来逐渐调整时钟。如果时钟与实际时间偏差太大,则需要很长时间才能纠正错误。这种方法叫做步进时钟(时间跳变)。
# 此处表示如果调整值大于1000秒,则这将使系统时钟步进,但仅在前十个时钟更新中。
makestep 1000 10

# Enable kernel synchronization of the real-time clock (RTC).
# 启用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

# Allow NTP client access from local network.
#指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#语法相同,指令也可以多次使用。
#只允许192.168.网段的客户端进行时间同步
#allow 192.168.0.0/16
#拒绝192.168.网段的客户端进行时间同步
#deny 192.168.0.0/16

# Serve time even if not synchronized to a time source.
# NTP服务器不可用时,采用本地时间作为同步标准
#local stratum 10

# Specify file containing keys for NTP authentication.
# 指定包含NTP验证密钥的文件
#keyfile /etc/chrony.keys

# Specify directory for log files.
# 指定日志文件的目录
logdir /var/log/chrony

# Select which information is logged.
# 将对系统增益或损耗率的估计值以及所做的任何转换记录的更改记录到名为的文件中tracking.log。
#log measurements statistics tracking


#**** 其他未在默认配置文件的配置项
# 在第一次时钟更新之后,chronyd将检查每次时钟更新的偏移量,它将忽略两次大于1000秒的调整,并退出另一个调整。
maxchange 1000 1 2
# 该rtcfile指令定义中的文件名chronyd可以保存跟踪系统的实时时钟(RTC)的精度相关的参数。
rtcfile /var/lib/chrony/rtc

1.搭建ntp服务器

# Centos7.6已默认安装,查看状态
systemctl status  chronyd
# 注释其他server开头的配置,添加阿里云NTP公共时间同步服务器
vim /etc/chrony.conf
添加内容
server ntp.aliyun.com iburst
makestep 1000 10
allow 0.0.0.0/0 #或者allow all
local stratum 10
# 重启chronyd
systemctl restart   chronyd
# 查看时间同步源,查看时间同步进度
chronyc sources –v

 2.配置ntp客户端

参考上图,客户端与服务端的区别就在于把自己的server指定为服务主机,而服务端的server就指向上游(外网)NTP服务器。

Chrony默认listen端口是323, 如果有其他client想要通过123端口访问,需要将server配置为NTP的server(listen 123),这里只需要allow 子网即可。默认情况下不允许任何客户端访问,即 chronyd 纯粹作为 NTP 客户端运行。 如果使用了 allow 指令,chronyd 将既是其服务器的客户端,又是其他客户端的服务器。

# Centos7.6已默认安装,查看状态
systemctl status  chronyd
# # 注释其他server开头的配置,添加本地NTP公共时间同步服务器
vim /etc/chrony.conf
server 192.168.66.71 iburst
makestep 1000 10
# 重启chronyd
systemctl restart   chronyd
# 查看时间同步源,查看时间同步进度
chronyc -n sources –v

3.其他设置

#服务器防火墙设置
firewall-cmd --add-port=123/udp --permanent
#或者firewall-cmd --add-service=ntp --permanent 
firewall-cmd --reload

#服务器和客户端的时区设置
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp yes
#设置系统时间的时间日期
timedatectl set-time '16:10:40 2015-11-20'
timedatectl set-time 15:58:30
timedatectl set-time 20151120
#查看时间
date #系统时间system time(Wall Time)
hwclock --show #硬件时间(RTC)
#当系统时钟与硬件时钟不一致时,可进行同步到同一时间
hwclock --systohc    #以系统时钟为准,同步硬件时钟
hwclock --hctosys    #以硬件时钟为准,同步系统时钟

#如果是12小时制,想改为24小时制
update-locale LC_TIME=C.UTF-8

#dns
223.5.5.5       # 阿里DNS
223.6.6.6       # 阿里DNS
180.76.76.76    #百度DNS
8.8.8.8         # google域名服务器
114.114.114.114 # 是国内第一个、全球第三个开放的DNS服务地址,又称114DNS

#ntp
ntp.aliyun.com     # 阿里NTP授时服务器地址
ntp.tencent.com    # 腾讯云NTP授时服务器地址
time.edu.cn        # 教育网内的授时服务器地址
timedatectl

               Local time: Mon 2024-04-01 16:24:49 CST
           Universal time: Mon 2024-04-01 08:24:49 UTC
                 RTC time: Mon 2024-04-01 08:11:55
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
Local time           本地时间 通常为 RTC + 时区
Universal time       系统时间一直是UTC
RTC time             硬件时钟的时间,一般也 BIOS 时间
Time zone            时区, set-timezone就是设置的这个
NTP enabled          是否开启ntp时间同步
NTP synchronized     NTP同步是否完成
RTC in local TZ      是否设置RTC时间,set-local-rtc 选项就是控制该处
DST active           夏令时

 4.客户端无法进行时间同步

如果服务器之上存在防火墙,这时客户端向服务器请求时间不会有任何的响应,在客户端机器输入 chronyc sources 命令可以看到 Reach 字段的值为 0 ,表示客户端无法收到服务器的响应了。因此先排查防火墙问题。

如果排除了防火墙的问题的话,可能需要使用一些工具(tcpdump、nc)检查下客户端是否有收到服务器的数据包。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值