linux-tutorial/codes/linux/ops/sys/set-ntp.sh
2019-02-22 18:57:46 +08:00

29 lines
874 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
###################################################################################
# 使用 NTP 进行时间同步
# 参考https://www.cnblogs.com/quchunhui/p/7658853.html
# Author: Zhang Peng
###################################################################################
echo -e "\n>>>>>>>>> 设置 ntp"
echo -e "先安装时钟同步工具 ntp"
yum -y install ntp
ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
/sbin/iptables -A INPUT -p UDP -i eth0 -s ${ip}/24 --dport 123 -j ACCEPT
echo -e "启动 NTP 服务"
systemctl start ntpd.service
echo -e "立即执行时间同步"
/usr/sbin/ntpdate ntp.sjtu.edu.cn
echo -e "自动定时同步时间"
echo "* 3 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn" >> /etc/crontab
systemctl restart crond.service
echo -e "同步后系统时间:"
date