Welcome to Elvin's blog

NTP时间同步 服务端 客户端 自动化安装配置

NTP时间同步 服务端 客户端 自动化安装配置

 原创内容 http://www.cnblogs.com/elvi/p/7657994.html

#!/bin/sh
#运行环境 centos6、centos7
# NTP时间同步 服务端 客户端 自动化安装配置
#Mady by Elven

function setntp1() {
. /etc/init.d/functions
pkill ntpd
echo "时间更新……"
/usr/sbin/ntpdate ntp6.aliyun.com
[[ `echo $?` == 0 ]] || { echo "安装ntp服务" ; yum install ntp -y;/usr/sbin/ntpdate ntp6.aliyun.com; }
#bakup
cp -f /etc/ntp.conf /etc/ntp.conf.$(date +"%F-%T")

echo "
#默认配置部分
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
">/etc/ntp.conf
}

function setntp2() {
echo "
# 外部NTP服务
server ntp6.aliyun.com perfer
server cn.ntp.org.cn iburst
server ntp.shu.edu.cn iburst
server s2c.time.edu.cn iburst
# server s2a.time.edu.cn iburst
# server s2m.time.edu.cn iburst

# 允许上层服务主动修改本机时间
restrict ntp6.aliyun.com nomodify notrap noquery
restrict cn.ntp.org.cn nomodify notrap noquery

# 外部时间服务器不可用时,以本地时间作为时间服务
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10
">>/etc/ntp.conf
}

function ntp_server() {
echo "
# 作为内网192.168.20.* 提供NTP服务
restrict 192.168.20.0 mask 255.255.255.0 nomodify notrap
">>/etc/ntp.conf
}

function ntp_client() {
echo '内网时间同步'
/usr/sbin/ntpdate 192.168.20.1
echo "
# 使用内网NTP服务
server 192.168.20.1 iburst
restrict 192.168.20.1 nomodify notrap noquery
# 外部时间服务器不可用时,以本地时间作为时间服务
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10
">>/etc/ntp.conf
}

function ntptest() {
# "重启NTP服务"
[[ `uname -r` == *el6* ]] &&  \
{ chkconfig ntpd on;service ntpd start; } || \
{ systemctl enable ntpd;systemctl start ntpd; }
/usr/sbin/hwclock -w
echo "NTP服务 状态检测"
sleep 5
netstat -upnl |grep ntpd
ntpstat
ntpq -p
}

case "$1" in 
    "s")
    setntp1
    setntp2
    ntp_server
    action "NTP配置" /bin/true
    ntptest
    exit
;;
    "c")
    setntp1
    ntp_client
    action "NTP配置" /bin/true
    ntptest
    exit
;;
    "-h")
    echo "-h 帮助"
    echo "c  安装NTP、并作为内网时间同步服务器Server"
    echo "s  安装NTP、使用内网时间同步服务"
    echo "*  安装配置NTP服务 "
;;
    *)
    setntp1
    setntp2
    action "NTP配置" /bin/true
    ntptest
    exit
;;
esac

 

posted @ 2017-10-23 16:52  blog-elvin-vip  阅读(470)  评论(0编辑  收藏  举报