如何在 RHEL/CentOS 7 中同步硬件时钟 (hwclock) 与 NTP

在这里,将介绍NTP配置,这些配置必须确保NTP本身在系统启动期间将hwclock与本地时钟同步,并且您无需再担心任何手动配置。
如果您使用的是HP ProLiantKnife 片服务器,则始终建议您使用SNTP设置BIOS日期和时间,否则对于所有其他硬件,请确保您的BIOS指向正确的日期和时间。

在Red Hat Enterprise Linux 6中,每次系统关闭或者重新启动时都会自动运行hwclock命令,但在Red Hat Enterprise Linux 7中则不会。当系统时钟通过网络时间协议(NTP)或者精确时间协议(PTP)同步时,内核每11分钟自动将硬件时钟与系统时钟同步一次。

编辑以下文件并将 SYNC_HWCLOCK 更改为“是”

# vim /etc/sysconfig/ntpdate
# Options for ntpdate
OPTIONS="-p 2"
# Number of retries before giving up
RETRIES=2
# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=yes

接下来在下面的文件中添加 NTP 服务器列表

# vim /etc/ntp/step-tickers
1.in.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org

这个文件会被ntpdate服务引用,下面是ntpdate.service的内容

# systemctl cat ntpdate.service
# /usr/lib/systemd/system/ntpdate.service
[Unit]
Description=Set time via NTP
After=syslog.target network.target nss-lookup.target
Before=time-sync.target
Wants=time-sync.target
[Service]
Type=oneshot
ExecStart=/usr/libexec/ntpdate-wrapper
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

检查名为“/usr/libexec/ntpdate-wrapper”的脚本,它调用“ntpstep=/etc/ntp/step-tickers”来确保ntp同步hwclock

接下来启用 ntpdate 服务以确保它在下次重启时被调用

# systemctl enable ntpdate.service

注意:只要 NTPD 守护进程正在运行,我们将无法启动 ntpdate 服务

如果我们希望启动 ntpdate 服务运行时,那么我们有两个选择

  1. 禁用ntpd服务,然后启动ntpdate.service
  2. 使用 -u 变量执行 ntpdate,使用此服务将使用随机端口号调用

停止 NTPD 守护进程

# systemctl stop ntpd.service

接下来启动ntpdate服务

# systemctl start ntpdate.service

接下来再次启动ntp服务

# systemctl start ntpd.service

或者我们可以执行以下命令(此处将 1.in.pool.ntp.org 替换为 NTP 服务器)

# ntpdate -u 1.in.pool.ntp.org
27 Apr 20:38:59 ntpdate[13893]: adjust time server 1.in.pool.ntp.org offset -0.004723 sec
日期:2020-06-02 22:16:53 来源:oir作者:oir