centos搭建NTP服務器(轉)

#爲了保證應用程序層和數據庫層的服務器能夠同步時鐘,所以要架設一臺NTP服務器,讓內部所有的服務器來同步NTP服務器的時鐘

#檢查服務器是否安裝ntp
[root@ntp ~]# rpm -qa |grep ntp

#yum安裝ntp
[root@ntp ~]# yum install -y ntp

#源碼包安裝ntp
[root@ntp ~]# wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.6p2.tar.gz
[root@ntp ~]# tar -zxvf ntp-4.2.6p2.tar.gz
[root@ntp ~]# cd ntp-4.2.6p2
[root@ntp ntp-4.2.6p2]# ./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
[root@ntp ntp-4.2.6p2]# make;make install

#讓本服務器時間與time.nist.gov時間同步,使服務器爲標準時間
[root@ntp ~]# ntpdate time.nist.gov

#自動同步服務器時鐘
[root@ntp ~]# crontab -e
*/5 * * * * /usr/sbin/ntpdate time.nist.gov

[root@ntp ~]# service crond restart

#修改ntp配置文件
[root@ntp ~]# vi /etc/ntp.conf

# 用restrict控管權限
# nomodify - 用戶端不能更改ntp服務器的時間參數
# noquery - 用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器
# notrap - 不提供trap遠端登陸

#允許任何IP的客戶機都可以進行時間同步
#restrict default nomodify notrap noquery  //註釋掉這一行
restrict default modify notrap //允許任何IP的客戶端都可以進行同步

#只允許10.8.0.0網段的客戶機進行時間同步
restrict default nomodify notrap noquery
restrict 10.8.0.0 mask 255.255.0.0 nomodify     //添加一行

# 以下是兩個ntp服務器
restrict time.nist.gov
restrict 164.67.62.194

# 用server設定上層ntp服務器,“prefer”表示優先
server time.nist.gov prefer
server 164.67.62.194
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

# 系統時間與bios事件的偏差記錄
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

# 日誌
logfile /var/log/ntp.log
broadcastdelay 0.008

#BIOS時間同步
SYNC_HWCLOCK=yes


#yum安裝啓動方式
[root@ntp ~]# service ntpd start

#源碼包安裝以守護進程啓動ntpd
[root@ntp ~]# /usr/local/ntp/bin/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid

#查看端口
[root@ntp ~]# netstat -unlnp

#iptables開啓udp 123端口
[root@ntp ~]# iptables -A INPUT -p udp -m udp --dport 123 -j ACCEPT

#查看ntp服務器有無和上層連通
[root@ntp ~]# ntpstat

#查看ntp服務器與上層間的聯繫
[root@ntp ~]# ntptrace -n time.nist.gov
time.nist.gov: stratum 1, offset 0.000000, synch distance 0.002650, refid 'ACTS'

查看ntp服務器與上層ntp服務器的狀態
[root@ntp ~]# ntpq -p

remote - 本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先
refid - 參考上一層ntp主機地址
st - stratum階層
when - 多少秒前曾經同步過時間
poll - 下次更新在多少秒後
reach - 已經向上層ntp服務器要求更新的次數
delay - 網絡延遲
offset - 時間補償
jitter - 系統時間與bios時間差

#linux客戶端同步NTP服務器時鐘
[root@db_1 ~]# crontab -e
*/5 * * * * /usr/sbin/ntpdate 10.8.2.10

 

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