NTP校时-Chrony

一.NTP与Chrony介绍

1.NTP(Network Time Protocol)是用来使计算机时间同步的一种协议,可以使计算机对其校时服务器或时钟源(如石英钟,GPS等等)进行时间同步,提供高精准度的时间校正,且可以使用加密确认的方式来防止病毒的协议攻击。

2.Chrony 是网络时间协议 (NTP) 的通用实现。Chrony是一个开源的软件,如果在chrony配置文件中指定了ntp服务器的地址,那么chrony就是一台客户端,会去同步ntp服务器的时间;如果在chrony配置了允许某些客户端来同步时间,则chrony就是一台ntp服务器。

Chrony有两个核心组件:
1.chronyd:是 Chrony 的守护进程,它负责与外部时间源通信、测量计算机时钟的偏移,并根据需要调整计算机时钟。chronyd 还负责记录日志,以便管理员可以查看时间同步的详细信息。
2.chronyc:是 Chrony 的命令行工具,它允许管理员与 chronyd 进程进行交互,并查询时间同步的状态。使用 chronyc,管理员可以手动请求时间同步,查看计算机时钟的偏移,以及查看时间源的详细信息。

chrony的配置文件是:/etc/chrony.conf
文件默认内容详解:

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

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 通过使用 hwtimestamp 指令启用硬件时间戳
# 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

# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器,allow all为允许所有地址
# Allow NTP client access from local network.
#allow 192.168.0.0/16

# 即使未同步到时间源,也要提供时间。取值范围1-15,当默认时钟源无法同步,则当前系统层级为10 
# Serve time even if not synchronized to a time source.
#local stratum 10

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

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

# 选择日志文件要记录的信息。
# Select which information is logged.
#log measurements statistics tracking

二.Chrony安装

1.关闭防火墙

systemctl stop firewalld  #停止firewall
systemctl disable firewalld  #禁止firewall开机启动

或者不关闭防火墙、但允许NTP服务

firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload

因NTP使用123/UDP端口协议,所以允许NTP服务即可

2.使用rpm包安装Chrony,准备好安装包:

chrony-3.4-1.el7.x86_64.rpm
libseccomp-2.3.1-4.el7.x86_64.rpm

执行命令安装:

rpm -ivh --force ./*.rpm

3.启用chronyd服务

systemctl start chronyd
systemctl enable chronyd

4.查看版本

# chronyd -v
[root@localhost chrony]# chronyd -v
chronyd (chrony) version 3.4 (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)

三.服务端和客户端Chrony配置

1.服务端配置
(1)配置文件修改
vi /etc/chrony.conf

a、修改第4-7行,若网络时间不可用时,可手动配置时钟源,即:

server 192.168.0.2 iburst

b、修改第33行,配置允许访问的客户端子网,allow all表示允许所有客户端地址:

allow 192.168.0.0/16

c、修改第37行,配置的时钟源不可用时,使用本地时间同步,打开注释即可,即:

local stratum 10

(2)配置文件生效需要重启chrony服务

systemctl restart chronyd

2.客户端配置
(1)配置文件修改
vi /etc/chrony.conf

a、修改第4-7行,添加校时服务器ip,格式如下:

server 192.168.0.3 iburst

b、若时钟源为windows NTP server,则需要在配置文件最后一行加上如下配置(maxdistance默认大小为3):

maxdistance 16.0

(2)配置文件生效需要重启chrony服务

systemctl restart chronyd

四.手动同步

以上配置后可实现时间的自动同步,手动同步则要执行如下命令:

chronyc -a makestep
# 需重启chronyd服务才可同步
systemctl restart chronyd 

或者:
# 使用ntpdate,需预先安装(ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm)
# ipv4
# 客户端立即获取
ntpdate 192.168.2.104

# ipv6
# [root@localhost home]# ntpdate 2001:da8:85b3::8a2e:270:190
# 4 May 15:10:55 ntpdate[14683]: adjust time server 2001:da8:85b3::8a2e:270:190 offset # 0.002714 sec
ntpdate 2001:da8:85b3::8a2e:270:190 

# ipv6 链路本地地址,后需加%+接口名(仅支持ntpdate手动同步,不支持chrony或ntpd自动同步)
ntpdate fe80::150%enp2s0 

五.查看同步状态

chronyc sources -v

[root@localhost chrony]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 172.27.51.108                 2   6    17     7  -1054ns[  -82us] +/-  10.1s

# *代表同步状态正常

六、chrony相关命令

1.chronyc 命令:

# 1.查看 ntp_servers
chronyc sources -v
 
# 2.查看 ntp_servers 状态
chronyc sourcestats -v
 
# 3.查看 ntp_servers 是否在线
chronyc activity -v
 
# 4.查看 ntp 详细信息
chronyc tracking -v
 
# 5.强制同步下系统时钟
chronyc -a makestep
 
# 6.查看哪些ip连接我的ntp时间了
chronyc clients

2.timedatectl命令:

# 1.查看日期时间、时区及 NTP 状态
timedatectl

# 2.查看时区列表
timedatectl list-timezones
timedatectl list-timezones |  grep  -E "Asia/S.*"

# 3.设置时区
timedatectl set-timezone Asia/Shanghai

# 4.修改日期时间(可以只修改其中一个)
timedatectl set-time "2024-05-04 15:50:20" # 或 date -s "2024-05-04 15:50:20"

七.授时中心

210.72.145.44 国家授时中心
ntp.aliyun.com 阿里云
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
s1d.time.edu.cn 东南大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学
s2c.time.edu.cn 北京邮电大学
s2d.time.edu.cn 西南地区网络中心
s2e.time.edu.cn 西北地区网络中心
s2f.time.edu.cn 东北地区网络中心
s2g.time.edu.cn 华东南地区网络中心
s2h.time.edu.cn 四川大学网络管理中心
s2j.time.edu.cn 大连理工大学网络中心
s2k.time.edu.cn CERNET桂林主节点
s2m.time.edu.cn 北京大学
ntp.sjtu.edu.cn 202.120.2.101 上海交通大学

八.常见时区

1.UTC  整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)2.GMT  格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的×××格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)

3.CST  中国标准时间 (China Standard Time) GMT + 8 = UTC + 8 = CST

4.DST  夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用)

参阅:
1.chronyd同步windows时钟源问题
2.NTP授时服务器支持IPv6配置
3.linux-时间同步工具Chrony的使用
4.chrony 详解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值