1、NTP服务概述
在Linux服务器上拥有准确的系统时间非常重要,因为[Cron]和[Anacron]等多个系统组件,[备份脚本]以及更多基于时间的工作。使用网络时间协议(NTP)协议可以实现精确的计时。
NTP是一种古老的,广为人知的跨平台协议,旨在[通过网络同步计算机的时钟]。它通常将计算机与Internet时间服务器或其他源(例如无线电或卫星接收器或电话调制解调器服务)同步。它也可以用作客户端系统的时间源/服务器。
在RHEL Linux 8中,不再支持ntp软件包,它由chrony软件包中提供的chronyd(在用户空间中运行的守护程序)实现。
Chrony是[网络时间协议(NTP)]的灵活实现。它用于同步来自不同NTP服务器的系统时钟,参考时钟或通过手动输入。
它也可以用于NTPv4服务器为同一网络中的其他服务器提供时间服务。它旨在在不同的条件下完美运行,例如间歇性网络连接,网络负载过重,温度变化,这可能会影响普通计算机的时钟。
Chrony带有两个程序:
chronyc – chrony的命令行界面
chronyd –可以在启动时启动的守护程序
下面开始安装chrony
dnf install chrony
[root@localhost ~]# dnf install chrony Last metadata expiration check: 0:11:14 ago on Mon 13 Dec 2021 08:08:40 PM EST. Package chrony-3.3-3.el8.x86_64 is already installed. Dependencies resolved. ================================================================================================= Package Arch Version Repository Size ================================================================================================= Upgrading: chrony x86_64 4.1-1.el8 BaseOS 327 k Transaction Summary ================================================================================================= Upgrade 1 Package Total download size: 327 k Is this ok [y/N]: y Downloading Packages: chrony-4.1-1.el8.x86_64.rpm 104 kB/s | 327 kB 00:03 ------------------------------------------------------------------------------------------------- Total 13 kB/s | 327 kB 00:25 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: chrony-4.1-1.el8.x86_64 1/1 Running scriptlet: chrony-4.1-1.el8.x86_64 1/2 Upgrading : chrony-4.1-1.el8.x86_64 1/2 Running scriptlet: chrony-4.1-1.el8.x86_64 1/2 Running scriptlet: chrony-3.3-3.el8.x86_64 2/2 Cleanup : chrony-3.3-3.el8.x86_64 2/2 Running scriptlet: chrony-3.3-3.el8.x86_64 2/2 Verifying : chrony-4.1-1.el8.x86_64 1/2 Verifying : chrony-3.3-3.el8.x86_64 2/2 Upgraded: chrony-4.1-1.el8.x86_64
chrony套件包括chronyd,和chronyc,这是用来改变各种操作参数和同时它正在运行以监控其性能的命令行实用程序。
现在启动chronyd服务,使其能够在系统启动时自动启动,并使用以下[systemctl命令]验证运行状态
systemctl start chronyd
systemctl status chronyd
systemctl enable chronyd
[root@localhost ~]# systemctl start chronyd [root@localhost ~]# systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-12-13 20:20:23 EST; 4min 27s ago Docs: man:chronyd(8) man:chrony.conf(5) Main PID: 9966 (chronyd) Tasks: 1 (limit: 23859) Memory: 2.2M CGroup: /system.slice/chronyd.service └─9966 /usr/sbin/chronyd Dec 13 20:20:23 localhost systemd[1]: Starting NTP client/server... Dec 13 20:20:23 localhost chronyd[9966]: chronyd version 4.1 starting (+CMDMON +NTP +REFCLOCK +R> Dec 13 20:20:23 localhost chronyd[9966]: Frequency 7.228 +/- 12.206 ppm read from /var/lib/chron> Dec 13 20:20:23 localhost chronyd[9966]: Using right/UTC timezone to obtain leap second data Dec 13 20:20:23 localhost systemd[1]: Started NTP client/server. Dec 13 20:20:29 localhost chronyd[9966]: Selected source 134.175.253.104 (2.centos.pool.ntp.org) Dec 13 20:20:29 localhost chronyd[9966]: System clock TAI offset set to 37 seconds [root@localhost ~]# systemctl enable chronyd
配置NTP服务器
在本节中,我们将展示如何将RHEL 8服务器设置为主NTP时间服务器。/etc/chrony.conf
打开配置文件。
vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project. server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst server ntp3.aliyun.com iburst server ntp4.aliyun.com iburst server ntp5.aliyun.com iburst server ntp6.aliyun.com iburst
chrony.conf 的配置提供以下信息:
服务器-此指令用于描述要从其同步的NTP服务器。
地层重量–每个地层应向同步源添加多少距离。默认值为0.0001。
driftfile -包含漂移数据的文件的位置和名称。
Makestep –该指令通过根据需要加快或降低时钟的速度,使chrony逐步纠正任何时间偏移。
logdir – chrony日志文件的路径。
如果要立即步进系统时钟并忽略当前正在进行的任何调整,则可以使用以下命令:
#chronyc makestep
现在,运行以下命令以显示chronyd正在访问的当前时间源(NTP服务器),它应该是您的NTP服务器地址。
[root@localhost ~]# systemctl restart chronyd [root@localhost ~]# chronyc sources MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^? 120.25.115.20 2 6 3 2 +6233us[+6233us] +/- 50ms ^? 203.107.6.88 2 6 3 2 +5408us[+5408us] +/- 35ms ^? 49.207.184.110.actcorp.in 3 6 3 3 -9985us[-9985us] +/- 159ms ^? 203.172.161.5 2 6 7 3 +50ms[ +67ms] +/- 199ms ^- 119.28.230.190 2 6 17 0 -13ms[ -13ms] +/- 80ms ^- 203.172.161.6 2 6 7 2 +63ms[ +69ms] +/- 179ms ^? 223.113.103.191 3 6 3 3 -8589us[+7994us] +/- 134ms ^? 202.120.127.191 0 6 0 - +0ns[ +0ns] +/- 0ns ^- makaki.miuku.net 2 6 17 3 -13ms[+4059us] +/- 63ms ^- stratum2-1.ntp.led01.ru.> 2 6 17 3 +14ms[ +20ms] +/- 96ms ^- 111.230.189.174 2 6 17 2 -14ms[ -14ms] +/- 50ms ^* time5.aliyun.com 2 6 17 2 +1049us[+6753us] +/- 13ms
安装NTPStat,可以显示时间同步状态
[root@localhost ~]# dnf -y install ntpstat [root@localhost ~]# ntpstat synchronised to NTP server (182.92.12.11) at stratum 3 time correct to within 75 ms polling server every 64 s
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。