Linux 网络时间校准

随笔

目录

[1. ntp 校验](#1. ntp 校验)

[2. timedatectl 命令来进行时间同步](#2. timedatectl 命令来进行时间同步)

[2.1 安装 Chrony 软件包](#2.1 安装 Chrony 软件包)

[2.2 编辑Chrony的配置文件/etc/chrony.conf,选择你想要同步的NTP服务器](#2.2 编辑Chrony的配置文件/etc/chrony.conf,选择你想要同步的NTP服务器)

[2.3 确认Chrony服务已经启动](#2.3 确认Chrony服务已经启动)

[2.4 命令查看系统时间与NTP服务器的同步情况](#2.4 命令查看系统时间与NTP服务器的同步情况)


1. ntp 校验

复制代码
1. 确保你的系统上安装了NTP客户端软件
 
[root@bogon instanta]# ntpdate pool.ntp.ort
bash: ntpdate: command not found...      # 没有安装 ntp
[root@bogon instanta]#

2. 安装 ntp
# yum install ntp


3. 使用ntpdate命令来进行网络时间校准
# sudo ntpdate pool.ntp.org

2. timedatectl 命令来进行时间同步

最新版本的CentOS 7中,NTP已被替换为Chrony作为时间同步服务。

2.1 安装 Chrony 软件包

复制代码
如果你在执行ntpdate命令时遇到"command not found"的错误消息,这意味着 ntpdate 命令没有在你的系统上安装或找不到可执行文件。在某些较新的Linux发行版中,ntpdate 可能已被弃用或替代。
解决这个问题的一种方法是使用 timedatectl 命令来进行时间同步

1. 安装Chrony软件包
# yum intall chrony


[root@bogon instanta]# yum install chrony
Last metadata expiration check: 2:45:22 ago on Sat 11 May 2024 09:33:20 PM CST.
Package chrony-4.2-1.el8.x86_64 is already installed.
Dependencies resolved.
==============================================================================================================================================================================
 Package                                  Architecture                             Version                                     Repository                                Size
==============================================================================================================================================================================
Upgrading:
 chrony                                   x86_64                                   4.5-1.el8                                   baseos                                   357 k

Transaction Summary
==============================================================================================================================================================================
Upgrade  1 Package

Total download size: 357 k
Is this ok [y/N]: y
Downloading Packages:
chrony-4.5-1.el8.x86_64.rpm                                                                                                                    23 kB/s | 357 kB     00:15
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                          23 kB/s | 357 kB     00:15
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                      1/1
  Running scriptlet: chrony-4.5-1.el8.x86_64                                                                                                                              1/1
  Running scriptlet: chrony-4.5-1.el8.x86_64                                                                                                                              1/2
  Upgrading        : chrony-4.5-1.el8.x86_64                                                                                                                              1/2
  Running scriptlet: chrony-4.5-1.el8.x86_64                                                                                                                              1/2
  Running scriptlet: chrony-4.2-1.el8.x86_64                                                                                                                              2/2
  Cleanup          : chrony-4.2-1.el8.x86_64                                                                                                                              2/2
  Running scriptlet: chrony-4.2-1.el8.x86_64                                                                                                                              2/2
  Verifying        : chrony-4.5-1.el8.x86_64                                                                                                                              1/2
  Verifying        : chrony-4.2-1.el8.x86_64                                                                                                                              2/2

Upgraded:
  chrony-4.5-1.el8.x86_64

Complete!
[root@bogon instanta]#

2.2 编辑Chrony的配置文件/etc/chrony.conf,选择你想要同步的NTP服务器

复制代码
选择你想要同步的NTP服务器。你可以在配置文件中找到已列出的NTP服务器列表,使用你所偏好的NTP服务器。例如,你可以添加以下行来同步到CentOS提供的默认NTP服务器:
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst


[root@bogon instanta]# vi /etc/chrony.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst


[root@bogon instanta]# tail -10 /etc/chrony.conf

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
[root@bogon instanta]#

2.3 保存并关闭配置文件后,重启Chrony服务以应用更改

复制代码
# sudo systemctl restart chronyd

[root@bogon instanta]# systemctl restart chronyd
[root@bogon instanta]#

2.3 确认Chrony服务已经启动

复制代码
确认Chrony服务已经启动,并且正在与NTP服务器进行时间同步。

如果chronyd服务正在运行,并且状态显示为"active (running)",说明Chrony服务已配置并正常工作。
现在,你的系统应该在后台通过Chrony与NTP服务器自动进行时间同步

使用以下命令检查Chrony服务状态
# systemctl status chronyd


[root@bogon instanta]#
[root@bogon instanta]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2024-05-12 00:28:37 CST; 8h left
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 20088 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 20084 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 20086 (chronyd)
    Tasks: 1 (limit: 821382)
   Memory: 964.0K
   CGroup: /system.slice/chronyd.service
           └─20086 /usr/sbin/chronyd

May 12 00:28:36 bogon systemd[1]: Starting NTP client/server...
May 12 00:28:36 bogon chronyd[20086]: chronyd version 4.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG)
May 12 00:28:36 bogon chronyd[20086]: Loaded 0 symmetric keys
May 12 00:28:36 bogon chronyd[20086]: Using right/UTC timezone to obtain leap second data
May 12 00:28:37 bogon systemd[1]: Started NTP client/server.
May 12 00:28:42 bogon chronyd[20086]: Selected source 202.112.31.197 (2.centos.pool.ntp.org)
May 12 00:28:42 bogon chronyd[20086]: System clock wrong by -29157.764766 seconds
May 11 16:22:44 bogon chronyd[20086]: System clock was stepped by -29157.764766 seconds
May 11 16:22:44 bogon chronyd[20086]: System clock TAI offset set to 37 seconds
[root@bogon instanta]#

2.4 命令查看系统时间与NTP服务器的同步情况

复制代码
系统时间会逐渐校准到正确的时间。你可以使用以下命令查看系统时间与NTP服务器的同步情况:
# chronyc tracking

[root@bogon instanta]#
[root@bogon instanta]# chronyc tracking
Reference ID    : CA701FC5 (dns2.synet.edu.cn)
Stratum         : 2
Ref time (UTC)  : Sat May 11 08:22:45 2024
System time     : 0.000001325 seconds fast of NTP time
Last offset     : +0.000001779 seconds
RMS offset      : 0.000001779 seconds
Frequency       : 13.598 ppm fast
Residual freq   : +0.000 ppm
Skew            : 157.455 ppm
Root delay      : 0.012316211 seconds
Root dispersion : 0.005261188 seconds
Update interval : 2.0 seconds
Leap status     : Normal
[root@bogon instanta]#

至此 CentOS7 已完成网络时间校准

相关推荐
HABuo14 分钟前
【Linux进程(四)】进程切换&环境变量深入剖析
linux·运维·服务器·c语言·c++·ubuntu·centos
oMcLin28 分钟前
如何在 Rocky Linux 8.7 上通过 Prometheus 与 Grafana 结合,提升大规模容器环境的监控与性能分析?
linux·grafana·prometheus
橘颂TA29 分钟前
【Linux】死锁四条件的底层逻辑:从锁冲突到 STL 组件的线程安全实践(Ⅵ)
linux·运维·服务器·c++·死锁
埃伊蟹黄面32 分钟前
ELF深入解剖:从文件头到动态段,图解库的二进制构成
linux·c·
五仁火烧1 小时前
生产环境中配置了接口3000后,不能启动,改成8080后就可以
linux·网络·安全·vue
黄焖鸡能干四碗1 小时前
智能制造工业大数据应用及探索方案(PPT文件)
大数据·运维·人工智能·制造·需求分析
JiMoKuangXiangQu1 小时前
Linux build:头文件安装
linux·headers_install
橙露1 小时前
国产PLC与进口PLC全面对比分析:技术、市场与未来趋势
运维·网络
lbb 小魔仙1 小时前
【Linux】云原生运维效率提升:Linux 终端工具链(kubectl + tmux + fzf)组合拳教程
linux·运维·云原生
清水白石0081 小时前
解构异步编程的两种哲学:从 asyncio 到 Trio,理解 Nursery 的魔力
运维·服务器·数据库·python