Linux NTP 时间同步问题解决指南
问题描述
错误提示:"the NTP socket is in use, exiting" 表示 NTP 套接字正在被使用,通常是因为系统中的 ntpd 服务已经在运行,而 ntpdate 命令和 ntpd 服务不能同时使用端口 123。
解决步骤
1. 停止 ntpd 服务
在执行 ntpdate 命令之前,确保 ntpd 服务已经停止。
CentOS 7 及之前版本:
sudo service ntpd stop
CentOS 8 及之后版本:
sudo systemctl stop ntpd
2. 使用 ntpdate 进行时间同步
执行 ntpdate 进行手动时间同步。以下是一些常用的 NTP 服务器地址:
# 使用 pool.ntp.org
sudo ntpdate pool.ntp.org
# 或使用 time.nist.gov
sudo ntpdate time.nist.gov
# 或使用阿里云 NTP
sudo ntpdate ntp.aliyun.com
3. 禁用 ntpd 服务的自动启动
如果不希望 ntpd 服务在系统启动时自动启动,可以禁用其开机自启:
CentOS 7 及之前版本:
sudo chkconfig ntpd off
CentOS 8 及之后版本:
sudo systemctl disable ntpd
4. 设置计划任务(Crontab)
如果需要定期同步时间,可以将 ntpdate 命令加入到计划任务中。
编辑 crontab 文件:
sudo crontab -e
添加定时任务,例如每天凌晨 2 点同步时间:
0 2 * * * /usr/sbin/ntpdate pool.ntp.org
现代时间同步服务(推荐)
为了实现持续的时间同步,建议使用以下现代时间同步服务:
方案一:使用 chrony(推荐)
chrony 是一种现代且高效的时间同步服务,比 ntpd 更快、更准确。
安装 chrony:
CentOS 7:
sudo yum install chrony
CentOS 8/9:
sudo dnf install chrony
Ubuntu/Debian:
sudo apt install chrony
启动服务:
sudo systemctl start chronyd
sudo systemctl enable chronyd
查看同步状态:
chronyc tracking
chronyc sources
配置文件(/etc/chrony.conf):
# 使用阿里云 NTP 服务器
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
# 或使用国际 NTP 池
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
方案二:使用 systemd-timesyncd
systemd-timesyncd 是 systemd 的一部分,提供简单的时间同步功能。
启动服务:
sudo systemctl start systemd-timesyncd
sudo systemctl enable systemd-timesyncd
查看同步状态:
timedatectl status
配置文件(/etc/systemd/timesyncd.conf):
[Time]
NTP=ntp.aliyun.com ntp1.aliyun.com
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org
常见问题(FAQ)
Q1: 防火墙阻止 NTP 通信
确保防火墙允许 UDP 123 端口:
# firewalld
sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload
# iptables
sudo iptables -A INPUT -p udp --dport 123 -j ACCEPT
sudo iptables -A OUTPUT -p udp --sport 123 -j ACCEPT
Q2: NTP 服务器不可达
测试 NTP 服务器连通性:
# 测试网络连通性
ping ntp.aliyun.com
# 测试 NTP 端口
nc -u -v ntp.aliyun.com 123
Q3: 时间偏差过大无法同步
如果时间偏差超过 1000 秒,NTP 可能拒绝同步。解决方法:
# 先手动设置大致时间
sudo date -s "2024-01-01 12:00:00"
# 然后使用 ntpdate 同步
sudo ntpdate -b pool.ntp.org
Q4: chrony 和 ntpd 冲突
两者不能同时运行,选择其一:
# 停止并禁用 ntpd
sudo systemctl stop ntpd
sudo systemctl disable ntpd
# 启动 chrony
sudo systemctl start chronyd
sudo systemctl enable chronyd
推荐方案
*推荐1:使用 chrony
- 同步速度更快
- 适应网络变化能力强
- 资源占用少
- 支持更多场景(虚拟机、移动设备)
简单场景:使用 systemd-timesyncd
- 配置简单
- 系统自带
- 适合桌面和简单服务器
不推荐:定时执行 ntpdate
- 时间跳变可能影响应用
- 不如持续同步准确
- 已被标记为过时工具