【个人记录】同步Linux服务器时间和时区

修改时区

timedatectl set-timezone Asia/Shanghai

时间同步

使用ntp进行同步,时间服务器使用阿里云NTP服务器

安装NTP服务

yum install -y ntp

写入NTP配置文件

cat > /etc/ntp.conf <<EOF
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
EOF

服务生效

systemctl restart ntpd

检查服务状态

ntpstat
date

一键脚本

#!/bin/bash
timedatectl set-timezone Asia/Shanghai
yum install -y ntp
cat > /etc/ntp.conf <<EOF
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
EOF
systemctl restart ntpd
ntpstat
date

参考文档

https://blog.csdn.net/krabbit1997/article/details/112510514
https://help.aliyun.com/zh/ecs/user-guide/alibaba-cloud-ntp-server

你可能感兴趣的:(Linux,服务器,linux)