#指定时间服务器
timeserver=ntp.sjtu.edu.cn
#设定时区
timedatectl set-timezone Asia/Shanghai
#编辑配置文件,将所有0.centos.pool.ntp.org替换为指定时钟服务器地址
sed -i 's/0.centos.pool.ntp.org/$timeserver/g' /etc/chrony.conf
#重启时钟服务
systemctl restart chronyd
使用ntpd
ntpdate强制更改系统时间,在2012年已经废除;之前ntpdate用于ntpd的初始时间设置,随着ntpd的更新,它不再需要ntpdate
ntpd是一个周期性执行时间同步的工具,要使用ntpd进行时间同步仅需要几步:
配置文件位于:/etc/ntp.conf
ansible all -m shell -a "yum -y install ntp"
ansible all -m shell -a "systemctl enable ntpd"
ansible all -m shell -a "systemctl start ntpd"
#验证同步结果
ntpq -p
#添加防火墙规则,如果检测不成功
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
设置服务器时区
\cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock