最后更新于

Chrony安装(时间同步),包括设置时区


Chrony是一个高性能的NTP客户端和服务器 ⏰,用于系统时间同步。本文将详细介绍在CentOS系统中安装和配置Chrony的完整流程。

🌐 NTP服务器概述

腾讯云NTP服务器

内网NTP服务器:

time1.tencentyun.com
time2.tencentyun.com
time3.tencentyun.com
time4.tencentyun.com
time5.tencentyun.com

外网NTP服务器:

time1.cloud.tencent.com
time2.cloud.tencent.com
time3.cloud.tencent.com
time4.cloud.tencent.com
time5.cloud.tencent.com

参考文档:

🕐 设置时区

首先设置系统时区为中国标准时间:

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' > /etc/timezone

📦 安装Chrony

yum install chrony -y

⚙️ 配置Chrony

编辑配置文件:

vi /etc/chrony.conf

内网环境配置:

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 注释掉默认的NTP服务器
#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

# 配置腾讯云内网NTP服务器
server time1.tencentyun.com iburst
server time2.tencentyun.com iburst
server time3.tencentyun.com iburst
server time4.tencentyun.com iburst
server time5.tencentyun.com iburst

外网环境配置:

# 配置腾讯云外网NTP服务器
server time1.cloud.tencent.com iburst
server time2.cloud.tencent.com iburst
server time3.cloud.tencent.com iburst
server time4.cloud.tencent.com iburst
server time5.cloud.tencent.com iburst

创建用户chrony,并通过相关命令配置,并启动开机启动chronyd

useradd -M -s /sbin/nologin chrony

mkdir /run/chrony
chown -R chrony:chrony /run/chrony/
chmod 750 /run/chrony/

systemctl restart chronyd
systemctl enable chronyd
[root@server181 ~]# ps -ef | grep chronyd
chrony    3323     1  0 17:09 ?        00:00:00 /usr/sbin/chronyd
root      4304  3358  0 17:14 pts/0    00:00:00 grep --color=auto chronyd

完成

过程中,可能存在以下报错,提示Could not change ownership of /var/run/chrony : Operation not permitted没有权限,可通过上述步骤“创建用户chrony,并通过相关命令配置”解决。

[root@server182 ~]# systemctl restart chronyd
Job for chronyd.service failed because the control process exited with error code. See "systemctl status chronyd.service" and "journalctl -xe" for details.
[root@server182 ~]# systemctl status chronyd.service
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2021-08-24 18:16:03 CST; 7s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 20429 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=1/FAILURE)

Aug 24 18:16:03 server182 systemd[1]: Starting NTP client/server...
Aug 24 18:16:03 server182 chronyd[20431]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Aug 24 18:16:03 server182 chronyd[20431]: Could not change ownership of /var/run/chrony : Operation not permitted
Aug 24 18:16:03 server182 chronyd[20429]: Could not open /var/run/chrony/chronyd.pid : No such file or directory
Aug 24 18:16:03 server182 systemd[1]: chronyd.service: control process exited, code=exited status=1
Aug 24 18:16:03 server182 systemd[1]: Failed to start NTP client/server.
Aug 24 18:16:03 server182 systemd[1]: Unit chronyd.service entered failed state.
Aug 24 18:16:03 server182 systemd[1]: chronyd.service failed.
[root@server182 ~]#

✅ 验证时间同步

# 查看时间同步状态
chronyc tracking

# 查看NTP源状态
chronyc sources -v

# 强制同步时间
chronyc makestep

💡 提示: 配置完成后,系统将自动与NTP服务器保持时间同步,确保系统时间的准确性。