如何在CentOS 8/RHEL 8上将时间与时区同步

时间:2020-03-21 11:42:14  来源:igfitidea点击:

时间同步是任何Linux发行版中最重要的部分。
使用NTP(网络时间协议)可以直接进行时间同步的配置。
它包括一些可以与时间同步正常工作的命令。
使不同的时钟运行在同一服务器上是一项非常有趣的任务,可以使系统时间保持最新状态。

当任何用户登录到系统时,根据该系统时区的本地时间记录用户登录时间是非常重要的任务。
大多数服务在不同的时区运行,因此至关重要的是要以准确的时间运行系统。

步骤1:检查不同的可用时区,并从其他时区设置时区

列出可用的时区

$sudo timedatectl list-timezones

设置位置时区

$sudo timedatectl Asia/Kolkata

步骤2:使用NTP守护程序同步时间

NTP守护程序有两种类型,包括chronyd和ntpd。
我们应该选择并仅使用一个,以避免在同一台计算机上同时运行两个NTP守护程序,这会导致ntp不兼容,并且同时使用两个NTP守护程序会导致不稳定。

本教程包括两个NTP守护程序,我们应该选择一个:

  • 使用chronyd同步时间,它适用于虚拟系统。
  • 使用ntpd的同步时间,它适用于网络系统。

步骤3:使用chronyd同步时间

安装chronyd软件包

$sudo yum install chrony

启动chronyd服务

$sudo systemctl start chrnoyd

编辑配置文件

$sudo vi /etc/chrony.conf

添加这些行
对于CentOS

server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

对于RHEL

server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

添加这些行后,只需保存并退出。

重新启动服务以使更改生效。

$sudo systemctl restart chrnoyd

启用此服务以在每次启动时运行。

$sudo systemctl enable chrnoyd

步骤4:使用ntpd同步时间

安装ntpd软件包

$sudo yum install ntp

启动ntpd服务

$sudo systemctl start ntpd

编辑配置文件

$sudo vi /etc/ntp.conf

添加这些行
对于CentOS

server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

对于RHEL

server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

添加这些行后,只需保存并退出。

重新启动服务以使更改生效。

$sudo systemctl restart ntpd

启用此服务以在每次启动时运行。

$sudo systemctl enable ntpd

现在我们已在CentOS和RHEL上配置了带有时区的同步时间。