1. 程式人生 > >NTP時間同步 服務端 客戶端 自動化安裝配置

NTP時間同步 服務端 客戶端 自動化安裝配置

網卡ip tail modify func 本地 start art 時間同步 ntpdate

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

#有好的國內時間同步服務地址,歡迎推薦^_^

  

NTP時間同步 服務端 客戶端 自動化安裝配置