2. CentOS/RHEL 系统
步骤 1:安装 chrony
bash:运行
bash
# CentOS 7/RHEL 7
sudo yum install chrony -y
# CentOS 8/RHEL 8+
sudo dnf install chrony -y
步骤 2:配置 chrony(同 Ubuntu)
编辑配置文件 /etc/chrony.conf:
bash:
bash
sudo vim /etc/chrony.conf
-
替换上游服务器:
vbscriptserver ntp.aliyun.com iburst server time.nist.gov iburst # 美国NIST服务器(备用) -
允许局域网客户端(例如 10.0.0.0/24 网段):
allow 10.0.0.0/24
步骤 3:重启服务并设置自启
bash:
bash
sudo systemctl restart chronyd
sudo systemctl enable chronyd
步骤 4:验证
bash:
bash
chronyc sources -v # 查看同步状态
chronyc tracking # 查看本地时间同步详情
3. 传统 ntp 服务
若需使用传统 ntp 服务(而非 chrony),步骤如下:
Ubuntu/Debian 安装 ntp:
bash:
sudo apt install ntp -y
配置文件:/etc/ntp.conf,添加上游服务器和允许的客户端(同 chrony 配置逻辑),然后重启服务:
bash:
sudo systemctl restart ntp
CentOS/RHEL 安装 ntp:
bash:
bash
sudo yum install ntp -y # CentOS 7
sudo systemctl restart ntpd
sudo systemctl enable ntpd