企業內部在centos7.2系統中配置NTP服務及內網伺服器時間同步
網路時間協議NTP(Network Time Protocol)是用於互連網中時間同步的標準互連網協議。NTP的用途是把電腦的時間同步到某些時間標準。目前採用的時間標準是世界協調時UTC(Universal Time Coordinated)。NTP的主要開發人員是美國德拉瓦大學的David L. Mills教授。
NTP對於我們個人來說有什麼用呢,簡單的講,當你的電腦時間不準確了,你可以接入到互連網,從網上同步一下時間。
對於企業來說,當你有成百上千的電腦,都不能直接連接互連網,時間不統一影響業務,如果一台一台的修改,工作量太大。這時搭建一個自己的NTP伺服器就顯出它的優勢了。
企業內部假設NTP伺服器的大概拓撲如下:
配置好相關的yum源、確保你的ntp伺服器可以連網
# vim /etc喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcmVzb2x2LmNvbmY8YnI+CiPM7bzTzfi52KOs1eK49r/J0tTU2ndpbmRvd27PwrLpv7SjrGlwY29uZmlnsum/tMSsyM/N+LnYPGJyPgpuYW1lc2VydmVyIDE5Mi4xNjguOC4yNTE8YnI+Cjxicj4KPGgxPsXk1sPE2s34TlRQLVNlcnZlcigxOTIuMTY4LjguMTAwKTwvaDE+CjxpbWcgc3JjPQ=="http://www.2cto.com/uploadfile/Collfiles/20160414/20160414090639480.png" alt="\">
1、yum安裝ntp服務# yum install -y ntp
配置開機啟動
# chkconfig ntpd on
# chkconfig --list ntpd
ntpd 0:關閉1:關閉2:啟用3:啟用4:啟用5:啟用6:關閉
2、編輯ntp的設定檔
# vim /etc/ntp.conf 添加如下內容就可以了
# 允許內網網段 192.168.0.0 其他機器同步時間restrict 192.168.0.0 mask 255.255.0.0 nomodify # 中國這邊最活躍的時間伺服器 : http://www.pool.ntp.org/zone/cnserver ntp.api.bz perfer server 210.72.145.44 # 中國國家受時中心server 202.112.10.36 # 1.cn.pool.ntp.orgserver 59.124.196.83 # 0.asia.pool.ntp.org# 允許上層時間伺服器主動修改本機時間restrict ntp.api.bz nomodify notrap noqueryrestrict 210.72.145.44 nomodify notrap noqueryrestrict 202.112.10.36 nomodify notrap noqueryrestrict 59.124.196.83 nomodify notrap noquery# 外部時間伺服器不可用時,以本地時間作為時間服務server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10
最終的參數:
# cat /etc/ntp.conf "awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
或者
# cat /etc/ntp.conf | grep "^[^#]"
driftfile /var/lib/ntp/driftrestrict default kod nomodify notrap nopeer noqueryrestrict -6 default kod nomodify notrap nopeer noqueryrestrict 127.0.0.1 restrict -6 ::1restrict 192.168.0.0 mask 255.255.0.0 nomodifyserver ntp.api.bz perferserver 210.72.145.44 # 中國國家受時中心server 202.112.10.36 # 1.cn.pool.ntp.orgserver 59.124.196.83 # 0.asia.pool.ntp.orgserver 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10server 0.centos.pool.ntp.org iburstserver 1.centos.pool.ntp.org iburstserver 2.centos.pool.ntp.org iburstserver 3.centos.pool.ntp.org iburstincludefile /etc/ntp/crypto/pwkeys /etc/ntp/keys
國內常用的時間伺服器列表:
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 北京大學
在配置前,先使用ntpdate手動同步下時間,免得本機與外部時間伺服器時間差距太大,讓ntpd不能正常同步
# ntpdate -u ntp.api.bz
13 Apr 14:45:05 ntpdate[11464]: step time server 61.153.197.226 offset 28824.742403 sec
ntp伺服器配置好之後啟動服務# service ntpd start,它監聽的連接埠是UDP的123連接埠、可以使用
# netstat -tnulp | grep ntp
udp 0 0 192.168.8.102:123 0.0.0.0:* 11484/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 11484/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 11484/ntpd
udp6 0 0 fe80::4637:e6ff:fe5:123 :::* 11484/ntpd
udp6 0 0 ::1:123 :::* 11484/ntpd
udp6 0 0 :::123 :::* 11484/ntpd
重啟完了之後要等幾分鐘或者十幾分鐘後就可以實現時間同步了,
# ntpdate 192.168.8.100
11 Aug 11:38:30 ntpdate[7619]: adjust time server 10.17.1.60 offset 0.000178 sec
如果重啟後馬上測試就會出現下面的情況:
# ntpdate 192.168.8.100
11 Aug 11:05:28 ntpdate[7326]: no server suitable for synchronization found
可以用這個命令參數來查看:
# ntpdate -d 192.168.8.100
ntpq -p 查看網路中的NTP伺服器,同時顯示用戶端和每個伺服器的關係
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*61.153.197.226 216.218.192.202 2 u 37 64 1 47.274 10.405 0.000
210.72.145.44 .INIT. 16 u - 64 0 0.000 0.000 0.000
gus.buptnet.edu 10.3.9.9 4 u 35 64 1 81.880 2.559 0.000
59-124-196-83.H .INIT. 16 u - 64 0 0.000 0.000 0.000
LOCAL(0) .LOCL. 10 l - 64 0 0.000 0.000 0.000
ntp2.aliyun.com 10.137.38.86 2 u 22 64 1 35.176 4.490 1.640
ntpstat 命令查看時間同步狀態,這個一般需要5-10分鐘後才能成功串連和同步。所以,伺服器啟動後需要稍等下。
剛啟動的時候,一般是:
# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
串連並同步後:
# ntpstat
synchronised to NTP server (110.75.186.248) at stratum 3
time correct to within 225 ms
polling server every 64 s
NTP的設定檔:
/etc/ntp.conf NTP服務的設定檔。
1)、許可權的設定主要以 restrict 這個參數來設定,主要的文法為:restrict IP地址 mask 子網路遮罩 參數其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP參數有以下幾個:ignore :關閉所有的 NTP 線上服務nomodify:用戶端不能更改服務端的時間參數,但是用戶端可以通過服務端進行網路校時。notrust :用戶端除非通過認證,否則該用戶端來源將被視為不信任子網noquery :不提供用戶端的時間查詢 2)、用server這個參數設定上級時間伺服器,文法為:server IP地址或網域名稱 [prefer]IP地址或網域名稱就是我們指定的上級時間伺服器,如果 Server 參數最後加上 prefer,表示我們的 NTP 伺服器主要以該部主機時間進行校準。3)、解決NTP伺服器校準時間時的傳送延遲使用driftfile參數設定:driftfile 檔案名稱 在與上級時間伺服器聯絡時所花費的時間,記錄在driftfile參數後面的檔案內。注意: driftfile 後面接的檔案需要使用完整的路徑檔案名稱,不能是連結檔案,並且檔案的許可權需要設定成 ntpd守護進程可以寫入。
/usr/share/zoneinfo/ 這個目錄下存放的是各個時區對應的設定檔案。
/etc/localtime 本地系統時間設定檔案。
/etc/sysconfig/clock 本機時區設定檔案。
配置內網NTP-Clients
內網其他裝置作為NTP的用戶端配置,相對就比較簡單,而且所有裝置的配置都相同。
首先需要安裝NTPD服務,然後配置為自啟動(與NTP-Server完全一樣)。然後找其中一台配置/etc/ntp.conf檔案,配置完成驗證通過後,拷貝到其他用戶端機器,直接使用即可。
# yum install -y ntp
# chkconfig ntp on
# vim /etc/ntp.conf
加入如下內容:
# 配置時間伺服器為本地的時間伺服器
server 192.168.8.100
restrict 192.168.8.100 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
請求伺服器前,請先使用ntpdate手動同步下時間
# ntpdate -u 192.168.8.100
13 Apr 14:49:51 ntpdate[4114]: no server suitable for synchronization found
這裡有可能出現同步失敗,一般情況下原因都是本地的NTPD伺服器還沒有正常啟動起來,一般需要幾分鐘時間後才能開始同步。
錯誤判斷請參考後面的錯誤處理。
# service ntpd restart
啟動後,查看同步情況
# ntpq -p
# ntpstat
.....
因為是內網,一般ntpstat很快就可以同步上,幾分鐘需要等下.
本機用戶端配置完成後,使用SCP拷貝/etc/ntp.conf到其他需要同步的用戶端機器,啟動NTPD服務即可。
其他用戶端機器上操作配置如下:
# ntpdate -u 192.168.8.100
# scp 192.168.8.xxx:/etc/ntp.conf /etc/ntp.conf
# service ntpd start
解決ntp的錯誤 no server suitable for synchronization found
當用ntpdate -d 來查詢時會發現導致 no server suitable for synchronization found 的錯誤的資訊有以下2個:
錯誤1.Server dropped: Strata too high
在ntp用戶端運行ntpdate serverIP,出現no server suitable for synchronization found的錯誤。
在ntp用戶端用ntpdate –d serverIP查看,發現有“Server dropped: strata too high”的錯誤,並且顯示“stratum 16”。而正常情況下stratum這個值得範圍是“0~15”。
這是因為NTP server還沒有和其自身或者它的server同步上。
以下的定義是讓NTP Server和其自身保持同步,如果在/ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務提供給ntp用戶端。
server 127.127.1.0 fudge
127.127.1.0 stratum 8
在ntp server上重新啟動ntp服務後,ntp server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鐘,在這個時間之內在用戶端運行ntpdate命令時會產生no server suitable for synchronization found的錯誤。
那麼如何知道何時ntp server完成了和自身同步的過程呢?
在ntp server上使用命令:
# watch ntpq -p
出現畫面:
Every 2.0s: ntpq -p Thu Jul 10 02:28:32 2008
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.30.22 LOCAL(0) 8 u 22 64 1 2.113 179133. 0.001
LOCAL(0) LOCAL(0) 10 l 21 64 1 0.000 0.000 0.001
注意LOCAL的這個就是與自身同步的ntp server。
注意reach這個值,在啟動ntp server服務後,這個值就從0開始不斷增加,當增加到17的時候,從0到17是5次的變更,每一次是poll的值的秒數,是64秒*5=320秒的時間。
如果之後從ntp用戶端同步ntp server還失敗的話,用ntpdate –d來查詢詳細錯誤資訊,再做判斷。
錯誤2.Server dropped: no data
從用戶端執行netdate –d時有錯誤資訊如下:
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123
.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出現這個問題的原因可能有2:
1。檢查ntp的版本,如果你使用的是ntp4.2(包括4.2)之後的版本,在restrict的定義中使用了notrust的話,會導致以上錯誤。
使用以下命令檢查ntp的版本:
# ntpq -c version
下面是來自ntp官方網站的說明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".
In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd
解決:
把notrust去掉。
2。檢查ntp server的防火牆。可能是server的防火牆屏蔽了upd 123連接埠。
可以用命令
#service iptables stop
來關掉iptables服務後再嘗試從ntp用戶端的同步,如果成功,證明是防火牆的問題,需要更改iptables的設定。
centos7關閉防火牆方法;
# systemctl stop firewalld.service
禁止隨機啟動
# systemctl disable firewalld.service