1 概述
linux服務(wù)器在提供服務(wù)時,要和其他機器進行請求的交互,實際生產(chǎn)環(huán)境中,可能因為時間不同步,導(dǎo)致了服務(wù)異常。本文將接受NTP服務(wù)器的相關(guān)配置和 概念,通過腳本實現(xiàn)一鍵安裝NTP服務(wù)器和客戶端
將服務(wù)器的本地機器的時間設(shè)置為NTP服務(wù)器,讀者可以自己將server調(diào)整為網(wǎng)絡(luò)上的時間服務(wù)器
如果實驗中,使用vmware來安裝虛擬機,建議vmware安裝vmware tools,然后設(shè)置vmware選項,開啟時間同步,使得虛擬機的時間和物理機的時間同步 ,防止虛擬機時間自動變更為錯誤的時間
注意,CentOS7開始改用chrony同步時間,原來ntp同步方式在CentOS7也可以使用,但要安裝ntp服務(wù)。
2 服務(wù)器的配置
用ntpd時間同步的話,前提是客戶端和服務(wù)器時間基本一致,才能通過服務(wù)ntpd同步,為了防止時間同步服務(wù)異常,所以可以先手動同步該服務(wù),然后再啟用ntpd服務(wù)進行自動同步.
服務(wù)器端要安裝包 ntp包
命令如下
yum -y install ntp
服務(wù)器端需要修改配置文件命令如下
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 ?
#將該服務(wù)器的本地時間作為ntp server的時間,如果其他客戶端同步,將同步這臺服務(wù)器的時間,這里如果要同步公網(wǎng)上的服務(wù)器,可以寫上公網(wǎng)的服務(wù)器,
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
重啟ntpd:
# service ntpd restart
等五六分鐘,讓ntpd完成自身的時間同步
設(shè)置開機啟動
chkconfig ntpd on
驗證
ntpq ?-p?
這里st為 stratum,范圍是0--15,如果為16就是錯誤的
關(guān)于restrict格式如下:
restrict [ip] mask [mask_number] [parameter]
其中parameter如下:
ignore:居然所有類型的ntp連接
nomodify:不允許客戶端修改服務(wù)器的時間參數(shù),但是允許客戶端透過這部主機進行時間校驗。
noquery:不允許客戶端進行時間校驗。
notrap:不提供trap時間登錄
notrust:拒絕沒有認證的客戶端
3 客戶端同步時間
客戶端需要停用ntp服務(wù),否則無法運行ntpdata ?服務(wù)器地址這個命令 來同步時間,同步的時候會提示ntp被使用中
停用服務(wù)命令如下
service ntpd stop
同步服務(wù)器的時間,如本例子中服務(wù)器的ip是192.168.32.61
ntpdate 192.168.32.61
設(shè)置定時同步
使用cron或修改crontab文件定期向NTP服務(wù)器更新時間
語句如下
crontab -e
* 10 * * *? /usr/sbin/ntpdate 192.168.32.61;/sbin/hwclock -w ? //每天十點執(zhí)行同步命令
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區(qū)別和設(shè)置
要檢查各個服務(wù)器時間格式,EDT或者是CST,在中國我們將服務(wù)器的時間格式調(diào)為CST
EDT:指美國東部夏令時間,波士頓、紐約市、華盛頓哥倫比亞特區(qū),都在這個時區(qū)內(nèi),跟北京時間有12小時的時差,晚12小時。
CST:可以指下面兩種:
1. 美國中部標準時間(西六區(qū),-6:00),中國是東八區(qū)(+8:00),北京時間比美國中部標準時間早14個小時。3:45 PMCST是北京時間凌晨1:45。
2. 中澳大利亞標準時間(+10:30),中國是東八區(qū)(+8:00),北京時間比中澳大利亞標準時間晚2個半小時。3:45 PMCST是北京時間下午上午5:45。
將系統(tǒng)的時間格式調(diào)整為CST的命令如下兩條
[root@test ~]# mv /etc/localtime /etc/localtime.bak
[root@test ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai? /etc/localtime
/usr/share/zoneinfo/Asia/Shanghai? 為源文件? ? /etc/localtime 新創(chuàng)建的鏈接
如果要把CST 時間調(diào)整為 EDT,使用如下語句
[root@localhost America]# mv /etc/localtime /etc/localtime.bak
[root@localhost America]#ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
5.2 參考文獻
本文的編寫參考了以下的文章,讀者如果要獲取更多詳細信息,建議參考如下內(nèi)容
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 相關(guān)授時服務(wù)器ip
127.127.1.0 ? ?本機的ip
210.72.145.44? (國家授時中心服務(wù)器IP地址)
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大學(xué)網(wǎng)絡(luò)中心NTP服務(wù)器地址)
s1a.time.edu.cn 北京郵電大學(xué)
s1b.time.edu.cn 清華大學(xué)
s1c.time.edu.cn 北京大學(xué)
s1d.time.edu.cn 東南大學(xué)
s1e.time.edu.cn 清華大學(xué)
s2a.time.edu.cn 清華大學(xué)
s2b.time.edu.cn 清華大學(xué)
s2c.time.edu.cn 北京郵電大學(xué)
s2d.time.edu.cn 西南地區(qū)網(wǎng)絡(luò)中心
s2e.time.edu.cn 西北地區(qū)網(wǎng)絡(luò)中心
s2f.time.edu.cn 東北地區(qū)網(wǎng)絡(luò)中心
s2g.time.edu.cn 華東南地區(qū)網(wǎng)絡(luò)中心
s2h.time.edu.cn 四川大學(xué)網(wǎng)絡(luò)管理中心
s2j.time.edu.cn 大連理工大學(xué)網(wǎng)絡(luò)中心
s2k.time.edu.cn CERNET桂林主節(jié)點
s2m.time.edu.cn 北京大學(xué)