forked from dunwu/linux-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-ntp.sh
More file actions
28 lines (20 loc) · 875 Bytes
/
set-ntp.sh
File metadata and controls
28 lines (20 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/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