练习实践-基础设施:搭建时钟同步服务器-基于chrony软件在centos7系统上的实现

news/2024/10/22 18:31:32/

参考来源: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   #将系统时间同步给硬件时间

http://www.ppmy.cn/news/1505330.html

相关文章

SQL注入实例(sqli-labs/less-11)

0、初始页面 1、确定闭合字符 2、确定列数 3、确定回显位置 4、爆库名 a union select user(),database() # 5、爆表名 a union select 1,group_concat(table_name) from information_schema.tables where table_schemasecurity# 6、爆列名 a union select 1,group_concat(c…

极简聊天室-websocket版(双向通信)

我们知道WebSocket是可以双向通信的,把极简聊天室代码又改了一下,前端发信息到后端也使用websocket,其实代码量更少了。。。 const express require(express); const app express(); var wsServer require(express-ws)(app)var msgs[];ap…

探究汽车IMU:提升车辆性能与安全性的关键组件

在当今的汽车工业中,高科技元件的集成正在不断推动着驾驶体验及安全性的革新。其中,汽车惯性测量单元(Inertial Measurement Unit,简称IMU)已成为一个至关重要的组成部分,它通过精确测量车辆的运动数据&…

解决VideoReader出现Thread worker: Error sending packet报错

问题现象:对于个别视频,单独读取该视频是正常,使用decord中的VideoReader读取会报如下的错误: [06:56:29] /github/workspace/src/video/ffmpeg/threaded_decoder.cc:292: [06:56:29] /github/workspace/src/video/ffmpeg/threade…

C语言 ——深入理解指针(2)

目录 1. 数组名的理解2. 二级指针3. 指针数组4. 字符指针变量5. 数组指针变量6. 函数指针变量7. 函数指针数组 1. 数组名的理解 这里我们使用 &arr[0] 的方式拿到了数组第一个元素的地址,但是其实数组名本来就是地址,而且是数组首元素的地址&#x…

Java面试题——第二篇(设计模式)

1. 工厂方法模式 1.1 普通工厂模式 建立一个工厂类,对实现了同一接口的一些类进行实例的创建。 1.2 抽象工厂模式 抽象多个工厂类,提高工厂的可扩展性 定义抽象工厂接口 public interface DeviceFactory { Phone createPhone(); Computer creat…

CRC16循环冗余校验VB

代码 Dim Data(6) As Integer Dim a As Long Dim crc_reg As Long Function crc16(Data, length) Dim i, j As IntegerDim b As Long b &HA001 i 0 crc_reg &HFFFF Do While length > 0crc_reg (crc_reg Xor Data(i))If crc_reg < 0 Thencrc_reg 65536 cr…

微调LLaMA-Factory的数据集格式-fintech.json

该文件有三个元素&#xff1a; "instruction": 这个元素通常用来描述给模型的一个指令或者任务。在这个上下文中&#xff0c;它说明了你希望模型执行什么样的操作或理解什么样的指导信息。例如&#xff0c;它可以是“请总结以下段落”或者“请将这句话翻译成英文”。…