Skip to content

CentOS 7 时间同步 #8

@JasonWu73

Description

@JasonWu73

当我们安装完服务器后,不可避免地需要配置时间同步服务。在 Linux 中时间同步配置是通过 NTP(Network Time Protocol)完成的。

设置时区

查看当前时区:

timedatectl status

设置时区(中国使用的是上海时区):

timedatectl set-timezone Asia/Shanghai

使用 NTP 守护进程同步时间

有两种类型的 NTP 守护进程,分别是 chronydntpd

本教程同时包含了这两个 NTP 守护进程的配置方法,你只需二选其一即可(如果系统中同时运行两个 NTP 守护进程,则会导致不兼容和不稳定):

  • 使用 chronyd 同步时间(适用于虚拟系统)
  • 使用 ntpd 同步时间(适用于网络系统)

不论使用上述哪种 NTP 守护进程,都需要从 NTP 公共池选择时间服务器,比如中国 NTP 池(pool.ntp.org: NTP Servers in China, cn.pool.ntp.org)的是:

server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

使用 chronyd 同步时间

1)安装 chronyd 软件包:

yum install chrony -y

2)编辑配置文件:

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
server 3.centos.pool.ntp.org iburst

修改为:

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

3)启动 chronyd 服务:

systemctl start chronyd

4)设置当系统引导时自动启动 chronyd 服务:

systemctl enable chronyd

使用 ntpd 同步时间

1)安装 ntpd 软件包:

yum install ntp -y

2)编辑配置文件:

vi /etc/ntp.conf

将:

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

修改为:

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

3)启动 ntpd 服务:

systemctl start ntpd

4)设置当系统引导时自动启动 chronyd 服务:

systemctl enable ntpd

Metadata

Metadata

Assignees

No one assigned

    Labels

    教程基础入门

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions