#!/usr/bin/env bash
# 文件名: ntp-init.sh
# 功能: RHEL/CentOS 7 NTP 安装配置脚本
# 作者: shuhui


### 更新记录
# 2018-05-05 v1.0  功能: 1. 脚本初始化
# 2018-05-05 v1.1  功能: 1. 新增幂 mixpoll 与 millpoll参数,若值为9(2^9次方 秒)
# 2018-05-26 v1.1  功能: 1. 新增interface参数,限制监听全局地址

### 设置时区
if [[ "$(date -R|awk '{print $NF}')" != "+0800" ]]
then
    /usr/bin/timedatectl set-timezone "Asia/Shanghai"
fi

cp /etc/ntp.conf /etc/ntp.conf.origin


### 配置 NTP
yum -y install ntp
/usr/sbin/ntpdate -u 10.16.8.44

cp /etc/ntp.conf /etc/ntp.conf.origin

cat >/etc/ntp.conf<<EOF
# filename: ntp.conf

interface ignore wildcard
interface listen lo

driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Allow Address 
# Allow Address 
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap nopeer noquery
restrict 172.16.0.0 mask 255.240.0.0 nomodify notrap nopeer noquery
restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap nopeer noquery

restrict 127.0.0.1 
restrict -6 ::1

# NTP Server Address [Data From: http://www.ntp.org.cn/ http://www.pool.ntp.org/zone/asia]
server 10.16.8.44 iburst minpoll 5 maxpoll 10
server 10.16.8.43 iburst minpoll 5 maxpoll 10

server  127.127.1.0
fudge   127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys
EOF

systemctl restart ntpd
systemctl status ntpd
