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

#!/bin/sh
#运行环境 centos6、centos7
# NTP时间同步 服务端 客户端 自动化安装配置
#Mady by Elven
function
setntp1() { . /etc/init.d/functions pkill ntpd [[ -f /usr/sbin/ntpdate ]] || { echo "install ntp";yum install ntp -y &> /dev/null; } #若没NTP则安装 echo "时间更新……" /usr/sbin/ntpdate ntp6.aliyun.com #bakup cp -f /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_IP() { ##该部分可自定义# #获取第一块网卡ip地址 IP=`ip add|grep global|awk -F'[ /]+' '{ print $3 }'|head -n 1` #本机网段 IPS=`echo $IP|awk -F\. '{ print $1"."$2"."$3 }'` #设置ntp server ip IPend=99 NTPS=$IPS.$IPend #加载自定义变量IPS,NTPS echo >>my.sh.conf #防止没有出错# source ./my.sh.conf >/dev/null 2>&1 } function ntp_server() { echo " # 作为内网NTP服务 restrict $IPS.0 mask 255.255.255.0 nomodify notrap ">>/etc/ntp.conf grep server /etc/ntp.conf tail -n 3 /etc/ntp.conf } function ntp_client() { echo '内网时间同步' /usr/sbin/ntpdate $NTPS echo " # 使用内网NTP服务 server $NTPS perfer server cn.ntp.org.cn iburst restrict $NTPS nomodify notrap noquery # 外部时间服务器不可用时,以本地时间作为时间服务 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 ">>/etc/ntp.conf grep server /etc/ntp.conf } function ntptest() { # "重启NTP服务" [[ `uname -r` == *el6* ]] && \ { chkconfig ntpd on;service ntpd restart; } || \ { systemctl enable ntpd;systemctl restart ntpd; } /usr/sbin/hwclock -w echo "NTP服务 状态检测" sleep 5 ntpstat ntpq -p } case "$1" in "s") setntp1 setntp2 NTP_IP ntp_server action "NTP配置" /bin/true ntptest exit ;; "c") setntp1 NTP_IP ntp_client action "NTP配置" /bin/true ntptest exit ;; "-h") echo "-h 帮助" echo "c 安装NTP、使用内网时间同步服务" echo "s 安装NTP、并作为内网时间同步服务Server" echo "* 安装配置NTP服务 " ;; *) setntp1 setntp2 action "NTP配置" /bin/true ntptest exit ;; esac exit
##########################
#使用实例

#下载
curl http://elven.vip/ks/sh/ntp.me.sh >ntp.me.sh
#安装配置ntp,并作为内网ntp服务
sh ntp.me.sh  s
#

#有好的国内时间同步服务地址,欢迎推荐^_^

  

猜你喜欢

转载自www.cnblogs.com/kcxg/p/10544113.html