参考来源:B站视频:up主:林哥讲运维 【一分钟学会:使用 chrony 部署企业 NTP 时间服务器】
https://chrony-project.org/comparison.html --chrony组织的比较
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-configuring_ntp_using_the_chrony_suite --红帽官方对于chrony和ntpd的比较
一、时钟同步方案的两种实现方式对比–ntpd VS chrony
ntpd软件比较旧,RockLinux 8.0存储库中不再提供ntpd软件包;
chrony比较新,chrony名称的由来,chron词根在希腊语中表示时间,克洛诺斯,希腊神话中掌管时间的神;
各项指标对比,数据来源:https://chrony-project.org/comparison.html
红帽提供的文档: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-configuring_ntp_using_the_chrony_suite
时间同步中的相关概念
stratum 层 层数越小,精度越高,传递过程会消耗时间
时间同步方案的标准模型
二、客户端时间同步的实现
1.安装chrony软件并启动chrony服务
[root@centos7 ~]# dnf install chrony -y
Extra Packages for Enterprise Linux 7 - x86_64 0.0 B/s | 0 B 00:00
CentOS-7 - Base - mirrors.aliyun.com 0.0 B/s | 0 B 00:00
CentOS-7 - Updates - mirrors.aliyun.com 0.0 B/s | 0 B 00:00
CentOS-7 - Extras - mirrors.aliyun.com 0.0 B/s | 0 B 00:00
依赖关系解决。
====================================================================================软件包 架构 版本 仓库 大小
====================================================================================
Installing:chrony x86_64 3.4-1.el7 base 251 k
安装依赖关系:libseccomp x86_64 2.3.1-4.el7 base 56 k事务概要
====================================================================================
安装 2 软件包总下载:307 k
安装大小:788 k
下载软件包:
(1/2): libseccomp-2.3.1-4.el7.x86_64.rpm 518 kB/s | 56 kB 00:00
(2/2): chrony-3.4-1.el7.x86_64.rpm 899 kB/s | 251 kB 00:00
------------------------------------------------------------------------------------
总计 1.1 MB/s | 307 kB 00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务准备中 : 1/1 Installing : libseccomp-2.3.1-4.el7.x86_64 1/2 运行脚本 : libseccomp-2.3.1-4.el7.x86_64 1/2 运行脚本 : chrony-3.4-1.el7.x86_64 2/2 Installing : chrony-3.4-1.el7.x86_64 2/2 运行脚本 : chrony-3.4-1.el7.x86_64 2/2 验证 : chrony-3.4-1.el7.x86_64 1/2 验证 : libseccomp-2.3.1-4.el7.x86_64 2/2 已安装:chrony-3.4-1.el7.x86_64 libseccomp-2.3.1-4.el7.x86_64 完毕!
[root@centos7 ~]# systemctl enable --now chronyd #启动服务并设置开机自启
场景1:客户端可以连接互联网的时间源
通过命令chronyc sources检查当前同步的时间源,注意看“*”表示当前同步的时间源
[root@centos7 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? time.cloudflare.com 0 7 0 - +0ns[ +0ns] +/- 0ns
^? 39.105.209.124 2 7 41 23 -11ms[ -11ms] +/- 104ms
^- ntp8.flashdance.cx 2 6 27 29 -25ms[ -25ms] +/- 125ms
^* gw-jp-1.gdn.lonely.obser> 2 6 17 31 +7379us[+8484us] +/- 59ms
通过timedatectl命令查看当前系统时间,注意看当前Time zone的时区
[root@centos7 ~]# timedatectlLocal time: 三 2024-07-31 18:09:55 CSTUniversal time: 三 2024-07-31 10:09:55 UTCRTC time: 三 2024-07-31 10:09:54Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes
NTP synchronized: yesRTC in local TZ: noDST active: n/a
手动指定当前时区为东八区的命令如下
[root@centos7 ~]# timedatectl set-timezone Asia/Shanghai
三、 服务端时间同步的实现
1.安装命令,通过dnf包管理器安装,命令如下
[root@centos7 ~]# dnf install chrony -y
2.查看并修改配置文件内容
修改三处内容:
修改第3-6行,指定同步上游时间服务器的地址;
修改26行,设置允许时间同步的客户端网段,任意地址可以设置为0.0.0.0/0;
修改29行,如果上游服务器不可用,就用本地时间给客户端同步,对应层级为10,手工授时,不可靠;
[root@centos7 ~]# vim /etc/chrony.conf 1 # Use public servers from the pool.ntp.org project.2 # Please consider joining the pool (http://www.pool.ntp.org/join.html).3 server 0.centos.pool.ntp.org iburst4 server 1.centos.pool.ntp.org iburst5 server 2.centos.pool.ntp.org iburst6 server 3.centos.pool.ntp.org iburst7 8 # Record the rate at which the system clock gains/losses time.9 driftfile /var/lib/chrony/drift10 11 # Allow the system clock to be stepped in the first three updates12 # if its offset is larger than 1 second.13 makestep 1.0 314 15 # Enable kernel synchronization of the real-time clock (RTC).16 rtcsync17 18 # Enable hardware timestamping on all interfaces that support it.19 #hwtimestamp *20 21 # Increase the minimum number of selectable sources required to adjust22 # the system clock.23 #minsources 224 25 # Allow NTP client access from local network.26 #allow 192.168.0.0/1627 28 # Serve time even if not synchronized to a time source.29 #local stratum 1030 31 # Specify file containing keys for NTP authentication.32 #keyfile /etc/chrony.keys33 34 # Specify directory for log files.35 logdir /var/log/chrony
3.使能配置文件
配置文件修改完成后重启服务,设置开机自启,如果开启了防火墙,还需要设置放行规则,
[root@centos7 ~]# systemctl restart chronyd
[root@centos7 ~]# systemctl enable chronyd
[root@centos7 ~]# firewall-cmd --add-service=ntp --permanent
[root@centos7 ~]# firewall-cmd --reload
服务端配置完成
场景1:内网搭建时钟服务器,指定内网地址为时间源;
在企业内网中搭建的时间服务器场景中(无法获取互联网同步时间源),则对应的配置文件中第三行修改为
server 内网时钟服务器地址 iburst
场景2:手工在服务端侧设置时间,通过chrony软件保持内网机器时间的一致性;
如果内网时间服务器无法获取上游时间源信息,可通过date -s命令手工指定时间同步服务器的时间信息,再通过chrony同步给客户端,保持内网机器时间的一致性;
维护–查看状态信息
在时间同步服务器上可以通过chrony client命令,查看哪些客户端在同步时间;
在客户端上通过chrony sources命令,查看是否正常同步服务器时间;
两个时间概念–硬件时间、系统时间
主板BIOS时间与系统时间是两个不同的概念,但它们之间有着密切的联系。
主板BIOS时间,也称为硬件时钟,是指主板上的实时时钟(RTC),这个时钟是由电池供电的,即使在关机状态下也能保持时间运行。BIOS时间可以在BIOS设置中进行调整,其格式通常为“月/日/年”对于系统日期和“时:分:秒”对于系统时间,采用24小时制。调整BIOS时间可以通过在BIOS界面中选择“System Date”来设置系统日期,通过“+”和“-”键来增加或减少数值,也可以通过按数字键直接输入具体数值,设置完成后,时间立即生效。同样地,通过选择“System Time”,可以设置系统时间,也是通过“+”和“-”键来调整小时、分钟和秒,设置完成后,时间也会立即生效12。
系统时间,则是指操作系统内核保持的时间。在Linux系统中,系统时间最初是由内核从硬件时钟(BIOS时间)读取的,然后系统时间会独立于硬件时钟运行。由于系统时间和硬件时钟可能因为不同的操作或系统故障而不同步,因此有时需要进行时间同步。在Linux系统中,可以通过特定的命令来实现时间设置及时钟同步。
硬件时间和系统时间相关的的查询修改命令小结
[root@centos7 ~]# hwclock #查询硬件时间
2024年08月05日 星期一 15时51分39秒 -0.897413 秒
[root@centos7 ~]# date #查询系统时间
2024年 08月 05日 星期一 15:51:40 CST
[root@centos7 ~]# hwclock --help #查询命令使用帮助用法:hwclock [功能] [选项...]功能:-h, --help 显示此帮助并退出-r, --show 读取硬件时钟并打印结果--set 将 RTC 设置为 --date 指定的时间-s, --hctosys 从硬件时钟设置系统时间-w, --systohc 从当前系统时间设置硬件时钟...[root@centos7 ~]# hwclock -s --hctosys #将硬件时间同步给系统时间
[root@centos7 ~]# hwclock -w --systohc #将系统时间同步给硬件时间