随笔
目录
[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 已完成网络时间校准