ntp服務器的搭建

NTP時間服務器

1.1 NTP簡介

NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的計算機時間同步的一種協議。

NTP服務器利用NTP協議來提供時間同步服務。

1.2 環境準備

主機名 服務器/客戶端 外網IP      內網IP

ntp-s Server      10.0.0.41 172.16.1.41

ntp-c Client      10.0.0.31 172.16.1.31

1.3 NTP服務端

1.3.1 安裝NTP

CentOS6.7默認已安裝ntp服務。如果沒安裝,就用yum安裝即可。

[root@ntp-s ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64

1.3.2 配置NTP

NTP服務默認的配置文件爲:/etc/ntp.conf 

1.3.2.1 操作前備份

cp /etc/ntp.conf{,.ori}

1.3.2.2 修改配置文件

[root@ntp-s ~]# grep -vE "^$|#" /etc/ntp.conf ##<==過濾空行和註釋行,前面三行爲修改部分
restrict default nomodify
server ntp1.aliyun.com
server s1a.time.edu.cn
driftfile /var/lib/ntp/drift
restrict 127.0.0.1 
restrict -6 ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

1.3.2.3 NTP配置重要參數詳解

driftfile /var/lib/ntp/drift  #<==記錄上次的NTP server與上層NTP server聯接所花費的時間
restrict default nomodify     #<==允許所有(nomodify)主機進行時間同步
server ntp1.aliyun.com        #<==同步時間服務器,阿里雲NTP服務器
server s1a.time.edu.cn prefer #<==prefer表示優先,北京郵電大學NTP服務器

1.3.3 啓動NTP

注意:如果有ntpdate定時任務,需要先關閉;否則兩者會有衝突。

/etc/init.d/ntpd start
chkconfig ntpd on
chkconfig --list ntpd
ss -tunlp |grep :123

1.3.4 查看本機和上層服務器的時間同步結果

[root@ntp-s ~]# ntpq -p      ##<==查看本機和上層服務器的時間同步結果( p == peer )
remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time5.aliyun.co 10.137.38.86     2 u    6   64    3   52.847  -115.81  52.877
202.112.10.60   202.118.1.46     2 u    3   64    3   64.112  -113.12  54.567

1.4 NTP客戶端

客戶機要等幾分鐘再和新啓動的ntp服務器進行時間同步,否則會提示no server suitable for synchronization found錯誤。

[root@ntp-c ~]# date -s "20160606"   #<==修改當前時間
2016年 06月 06日 星期一 00:00:00 CST
[root@ntp-c ~]# ntpdate 172.16.1.41  #<==手動執行與NTP server進行時鐘同步
28 Jul 15:55:27 ntpdate[8510]: step time server 172.16.1.41 offset 4550114.397444 sec
[root@ntp-c ~]# date                 #<==檢查
2016年 07月 28日 星期四 15:55:30 CST

1.4.1 添加到定時任務

[root@ntp-c ~]# crontab -l#<==5分鐘同步一次
#time sync by ChenDianHu at 2016-06-28
*/5 * * * * /usr/sbin/ntpdate 172.16.1.41 >/dev/null 2>&1

1.5 故障:ntp時間服務器,自動斷開!

原因:

ntp本地時間與上級ntp時間差超過1000s,那麼ntp進程就會退出並在系統日誌文件中記錄。

解決方法:

在配置文件/etc/ntp.conf的第一行加上這麼一句

tinker panic 0

1.6 參考博文

http://blog.csdn.net/iloli/article/details/6431757

http://www.zyops.com/ntp


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章