在 CentOS 系统中同步网络时间戳(即时间同步),通常使用 NTP(Network Time Protocol) 或 Chrony 来实现。CentOS 7 及以后版本默认使用 Chrony,而 CentOS 6 更早版本常用 ntpd。下面分别说明。
rpm -qa | grep chrony
如果没有安装:
yum install -y chrony
systemctl enable chronyd
systemctl start chronyd
chronyc -a makestep
chronyc tracking
或查看源:
chronyc sources
编辑配置文件:
vi /etc/chrony.conf
例如:
server ntp.aliyun.com iburst
server ntp1.tencent.com iburst
server cn.pool.ntp.org iburst
修改后重启:
systemctl restart chronyd
yum install -y ntp
chkconfig ntpd on
service ntpd start
ntpdate -u pool.ntp.org
⚠️ 注意:
ntpdate 已逐步被弃用ntpdate适合脚本或临时使用:
date -s "$(curl -s --head http://www.baidu.com | grep '^Date:' | cut -d' ' -f2-)"
或:
ntpdate -u ntp.aliyun.com
查看当前时区:
timedatectl
如果不是 Asia/Shanghai:
timedatectl set-timezone Asia/Shanghai
同步硬件时间:
hwclock --systohc
timedatectl | grep NTP
timedatectl set-ntp true
如果你能告诉我:
我可以给你更精确的配置方案。