NTP校时-Chrony

NTP校时-Chronychrony 一个开源自由的网络时间协议 NTP 的客户端与服务器端软件 让计算机保持系统时钟与时钟服务器 NTP 同步 ntp 校时

大家好,欢迎来到IT知识分享网。

一.NTP与Chrony介绍

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

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

# 使用 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" 

七.授时中心

八.常见时区

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) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用) 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/120669.html

(0)
上一篇 2025-10-28 10:26
下一篇 2025-10-28 10:45

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信