Linux 服务器时间同步

方法一

1
2
3
4
5
6
7
8
9
10
11
12
# 安装 ntpd 相关服务
yum install -y ntp

# 同步阿里云服务时间
ntpdate ntp1.aliyun.com

# 编辑 /etc/ntp.conf 文件
server ntp.aliyun.com iburst

# 开启自动同步及开机同步
systemctl restart ntp
systemctl enable ntp

方法二
下载 ntpdate

yum install -y ntpdate

调整时区为上海,也就是北京时间+8区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

yes | cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

使用NTP来同步时间

ntpdate us.pool.ntp.org

定时同步时间(每隔10分钟同步时钟)

1
2
3
4
5
6
7
8
crontab -l >/tmp/crontab.bak

echo "*/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP" >> /tmp/crontab.bak

crontab /tmp/crontab.bak

# 查看定时任务
crontab -l