linux-tutorial/codes/linux/sys/set-ntp.sh

29 lines
875 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/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