1 NTP簡介
NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的各個計算機時間同步的一種協議。它的用途是把計算機的時鐘同步到世界協調時UTC,其精度在局域網內可達0.1ms,在互聯網上絕大多數的地方其精度可以達到1-50ms。
NTP服務器就是利用NTP協議提供時間同步服務的。
2 NTP服務器安裝
查看是否安裝ntp服務
[root@node1 ~]# rpm -qa ntp
ntp-4.2.6p5-12.el6.centos.2.x86_64
如果沒有就安裝
yum install ntp -y
3 配置NTP服務
[root@node1 ~]# vim /etc/ntp.conf
# restrict default kod nomodify notrap nopeer noquery
restrict default nomodify
# nomodify客戶端可以同步
# 將默認時間同步源註釋改用可用源
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# server 2.centos.pool.ntp.org iburst
# server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com
server time.nist.gov
#沒有同步服務器時配
server 127.127.1.0
fudge 127.127.1.0 stratum 8
4 啓動NTP服務器
如果計劃任務有時間同步,先註釋,兩種用法會衝突。
[root@node1 ~]# crontab -e
#*/10 * * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1;/sbin/hwclock -w
啓動ntpd服務,並設置開機自啓動
[root@node1 ~]# service ntpd status
ntpd 已停
[root@node1 ~]# service ntpd start
正在啓動 ntpd: [確定]
[root@node1 ~]# service ntpd status
ntpd (pid 31990) 正在運行...
[root@node1 ~]# chkconfig --list|grep ntpd
ntpd 0:關閉 1:關閉 2:關閉 3:關閉 4:關閉 5:關閉 6:關閉
ntpdate 0:關閉 1:關閉 2:關閉 3:關閉 4:關閉 5:關閉 6:關閉
[root@node1 ~]# chkconfig ntpd on
[root@node1 ~]# chkconfig --list|grep ntpd
ntpd 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
ntpdate 0:關閉 1:關閉 2:關閉 3:關閉 4:關閉 5:關閉 6:關閉
測試
[root@node1 ~]# ntpq -p
remote refid st t when poll reach
delay offset jitter
==============================================================================
*ntp1.aliyun.com 10.137.38.86 2 u 22 64 1 525.885 -42.367 0.000
[root@node1 ~]# ntpstat
synchronised to NTP server (110.75.186.247) at stratum 3
time correct to within 4257 ms
polling server every 64 s
[root@node1 ~]# ntpdate 172.168.1.101
7 Dec 18:43:07 ntpdate[26950]: the NTP socket is in use, exiting
5 客戶機時間同步
客戶機要等幾分鐘再與新啓動的ntp服務器進行時間同步,否則會提示no server suitable for synchronization found錯誤。
[root@node2 ~]# ntpdate 172.168.1.101
7 Dec 18:40:16 ntpdate[1453]: step time server 172.168.1.101 offset 40.880807 sec
將命令放入計劃任務。
[root@node2 ~]# crontab -l
5 * * * * /usr/sbin/ntpdate 172.168.1.101 > /dev/null 2>&1;/sbin/hwclock -w
[root@node2 ~]# service crond status
crond (pid 2940) 正在運行...
6 報錯
解決ntp的錯誤 no server suitable for synchronization found
當用 ntpdate 來查詢時報錯:
"no server suitable for synchronization found "
錯誤的原因有以下2個:
6.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和其自身對峙同步,若是在/etc/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 Wed Oct 24 18:06:04 2018
remote refid st t when poll reach delay offset jitter
==============================================================================
*120.25.115.20 10.137.53.7 2 u 8 64 377 22.675 20.870 3.324
time-a-g.nist.g .INIT. 16 u - 1024 0 0.000 0.000 0.000
重視LOCAL的這個就是與自身同步的ntp server。
重視reach這個值,在啓動ntp server辦過後,這個值就從0開端絡續增長,當增長到17的時辰,從0到17是5次的變革,每一次是poll的值的秒數,是64秒*5=320秒的時候。
若是之後從ntp客戶端同步ntp server還失敗的話,用ntpdate –d來查詢具體錯誤信息,再做斷定。
6.2 Server dropped: no data
從客戶端履行netdate –d時有錯誤信息如下:
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
呈現這個錯誤的原因可能有2個:
- 檢查ntp的版本,若是你應用的是ntp4.2(包含4.2)之後的版本,在restrict的定義中應用了notrust的話,會導致以上錯誤。
- 檢查ntp server的防火牆。可能是server的防火牆樊籬了upd 123端口。
service iptables stop
關掉iptables辦過後再測驗測驗從ntp客戶端的同步,若是成功,證實是防火牆的題目,須要更改iptables的設置。