1. 程式人生 > >自動化安裝 NTP服務器

自動化安裝 NTP服務器

ntp 服務器

1 概述

linux服務器在提供服務時,要和其他機器進行請求的交互,實際生產環境中,可能因為時間不同步,導致了服務異常。本文將接受NTP服務器的相關配置和 概念,通過腳本實現一鍵安裝NTP服務器和客戶端

將服務器的本地機器的時間設置為NTP服務器,讀者可以自己將server調整為網絡上的時間服務器

2 服務器的配置

服務器端要安裝包 ntp包

命令如下

yum -y install ntp

服務器端需要修改配置文件命令如下

vim /etc/ntp.conf

driftfile /var/lib/ntp/drift
restrict 192.168.32.0 mask 255.255.255.0 nomodify 
#允許哪些機器同步
restrict 172.18.0.0  mask 255.255.0.0  nomodify
restrict 127.0.0.1
restrict -6 ::1
server 127.127.1.0  
#將該服務器的本地時間作為ntp server的時間,如果其他客戶端同步,將同步這臺服務器的時間,這裏如果要同步公網上的服務器,可以寫上公網的服務器,
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

重啟ntpd:

service ntpd restart

等五六分鐘,讓ntpd完成自身的時間同步

設置開機啟動

chkconfig ntpd on

驗證

ntpq  -p


技術分享

這裏st為 stratum,範圍是0--15,如果為16就是錯誤的

關於restrict格式如下:

restrict [ip] mask [mask_number] [parameter]

其中parameter如下:

ignore:居然所有類型的ntp連接

nomodify:不允許客戶端修改服務器的時間參數,但是允許客戶端透過這部主機進行時間校驗。

noquery:不允許客戶端進行時間校驗。

notrap:不提供trap時間登錄

notrust:拒絕沒有認證的客戶端

3 客戶端同步時間

客戶端需要停用ntp服務,否則無法運行ntpdata 服務器地址這個命令 來同步時間,同步的時候會提示ntp被使用中

停用服務命令如下

service ntpd stop

同步服務器的時間,如本例子中服務器的ip是192.168.32.61

ntpdate 192.168.32.61

設置定時同步

使用cron或修改crontab文件定期向NTP服務器更新時間

語句如下

crontab -e
* 10 * * *  /usr/sbin/ntpdate 192.168.32.61;/sbin/hwclock -w   
#每天十點執行同步命令

4 自動化安裝腳本

#!/bin/bash
#
#******************************************************************************
#Author:              Sunny
#Date:                2017-09-08
#FileName:            install_ntp.sh
#version:              1.0
#Your change info:
#Description:          For install ntp by  auto
#Copyright(C):        2017  All rihts reserved
#*****************************************************************************
is_bakfile(){
[ -e /root/bakfile ] || mkdir /root/bakfile
}
min_time () {
time=`date +%Y%m%d%H%M`
}
is_bakfile
min_time
[ -e /etc/ntp.conf ] &&mv /etc/ntp.conf /root/bakfile/ntp.conf."$time".bak
read -p "Please input your choice,server or client: " choice
if ! $(date | grep CST &>/dev/null);then
[ -e /etc/localtime ]&& mv /etc/localtime /root/bakfile/localtime."$time".bak
ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
fi
case $choice in
server)
echo "#################################################################################################"
echo "if your server is virtual host,suggest you will use its own time to  synchronization host time."
echo "you should also install vmware tools in the server host in order  use phisical  host  time to syn your virtual time,you need to start time ntp option in vmware"
echo "If your want to user common time from internet  to syn server host time,you  should make sure your host can access to the common time server from internet.You just replace server ip which line in line "server 127.127.0.1" "
echo "start to config ntp server"
echo "###################################################################################################"
echo
echo
rpm -q ntp &>/dev/null || yum -y install ntp &>/dev/null
cat >>/etc/ntp.conf<<eof
driftfile /var/lib/ntp/drift
restrict 192.168.32.0 mask 255.255.255.0 nomodify
restrict 172.18.0.0  mask 255.255.0.0  nomodify
restrict 127.0.0.1
restrict -6 ::1
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
eof
service ntpd restart&>/dev/null || echo "Something wrong when restart ntp service"
netstat -ntlup | grep ntpd &>/dev/null && echo "ntpd is running " || echo "Something wrong happened when start ntp,please check"
chkconfig ntpd on
echo "now it use cmd "ntpq -p",if you see st is what you set in  stratum and less than 16,ntp server is now now ,but it need few minutes to work normal."
ntpq  -p
echo "hint: to syn client host time after 3 minites"
;;
client)
netstat -ntlup | grep ntpd &>/dev/null && service ntpd stop&>/dev/null
read -p "Please input which server to syn(default:192.168.32.61): " ip
if [ -z "${ip:-}" ];then
ip=192.168.32.61
fi
ntpdate $ip
#set auto syn schedule
grep ntpdate /etc/crontab &>/dev/null && echo "already set auto ntp schdule,please check" || echo "* * * * * root /usr/sbin/ntpdate 192.168.32.61;/sbin/hwclock -w">>/etc/crontab
;;
*)
echo "Your choice is wrong,please check"
;;
esac
unset ip
unset time
exit

5 附錄

5.1 EDT 和 CST區別和設置

要檢查各個服務器時間格式,EDT或者是CST,在中國我們將服務器的時間格式調為CST

EDT:指美國東部夏令時間,波士頓、紐約市、華盛頓哥倫比亞特區,都在這個時區內,跟北京時間有12小時的時差,晚12小時。

CST:可以指下面兩種:

1. 美國中部標準時間(西六區,-6:00),中國是東八區(+8:00),北京時間比美國中部標準時間早14個小時。3:45 PMCST是北京時間淩晨1:45。

2. 中澳大利亞標準時間(+10:30),中國是東八區(+8:00),北京時間比中澳大利亞標準時間晚2個半小時。3:45 PMCST是北京時間下午上午5:45。

將系統的時間格式調整為CST的命令如下兩條

[[email protected] ~]# mv /etc/localtime /etc/localtime.bak

[[email protected] ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

/usr/share/zoneinfo/Asia/Shanghai 為源文件 /etc/localtime 新創建的鏈接

如果要把CST 時間調整為 EDT,使用如下語句

[[email protected] America]# mv /etc/localtime /etc/localtime.bak

[[email protected] America]#ln -s /usr/share/zoneinfo/America/New_York /etc/localtime

5.2 參考文獻

本文的編寫參考了以下的文章,讀者如果要獲取更多詳細信息,建議參考如下內容

http://blog.csdn.net/love_life2011/article/details/8067652

http://michael2017.blog.51cto.com/151632/1440488

http://www.cnblogs.com/13188196we/archive/2012/10/20/ntp.html

http://www.linuxidc.com/Linux/2012-08/67223.htm

5.3 相關授時服務器ip

127.127.1.0 本機的ip

210.72.145.44 (國家授時中心服務器IP地址)

ntp.sjtu.edu.cn 202.120.2.101 (上海交通大學網絡中心NTP服務器地址)

s1a.time.edu.cn 北京郵電大學

s1b.time.edu.cn 清華大學

s1c.time.edu.cn 北京大學

s1d.time.edu.cn 東南大學

s1e.time.edu.cn 清華大學

s2a.time.edu.cn 清華大學

s2b.time.edu.cn 清華大學

s2c.time.edu.cn 北京郵電大學

s2d.time.edu.cn 西南地區網絡中心

s2e.time.edu.cn 西北地區網絡中心

s2f.time.edu.cn 東北地區網絡中心

s2g.time.edu.cn 華東南地區網絡中心

s2h.time.edu.cn 四川大學網絡管理中心

s2j.time.edu.cn 大連理工大學網絡中心

s2k.time.edu.cn CERNET桂林主節點

s2m.time.edu.cn 北京大學

本文出自 “自學linux” 博客,請務必保留此出處http://ghbsunny.blog.51cto.com/7759574/1963847

自動化安裝 NTP服務器