跳转至

Rocky Linux 配置NTP

概要: 在Rocky Linux 8上,利用chrony工具同步时间

创建时间: 2022.12.21 21:07:51

更新时间: 2023.05.20 23:44:55

手动设置时间

语法

Bash
date MMDDhhmmYYYY
示例,假设现在是2023年5月17日晚上11点05分,那么
Bash
date 051723052023

配置时区

查看当前时区

Bash
timedatectl
image.png

查看可用时区

Bash
timedatectl list-timezones

设置时区

Bash
timedatectl set-timezone Asia/Shanghai

利用chrony同步时间

chrony是一个将本地系统时钟与网络时间协议Network Time Protocol (NTP)服务器同步的工具。

安装chrony

Bash
dnf install chrony -y

配置chrony

编辑chrony的配置文件

Bash
vim /etc/chrony.conf
在下面的示例中,我们与中国科学院国家授时中心NTSC同步时间,详细的配置方式,参考chrony – Documentation
Bash
server ntp.ntsc.ac.cn iburst
server ntp1.aliyun.com

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Specify directory for log files.
logdir /var/log/chrony
然后重启服务以保证配置被重新加载
Bash
systemctl restart chronyd

管理chrony服务

与其他服务类似,主要的命令如下

Bash
1
2
3
4
5
6
7
8
systemctl start chronyd    # 启动服务
systemctl stop chronyd     # 停止服务
systemctl restart chronyd  # 重启服务,可用于重新加载配置文件

systemctl status chronyd   # 查看服务状态

systemctl enable chronyd   # 开启开机自启
systemctl disable chronyd  # 关闭开机自启

确认时间已同步

可以通过以下命令确认自己的时间是否已和指定的NTP服务器同步

Bash
ping ntp.ntsc.ac.cn
chronyc tracking
image.png

参考