一、NTP簡介
NetworkTime Protocol(NTP)是用來使計算機時間同步化的一種協議,它可以使計算機對其服務器或時鐘源(如石英鐘,GPS等等)做同步化,它可以提供高精準度的時間校正(LAN上與標準間差小於1毫秒,WAN上幾十毫秒),且可介由加密確認的方式來防止惡毒的協議攻擊。截止到2010年6月,最新的NTP版本是第4版(NTPv4),採用UDP 123端口傳輸。
二、安裝配置
2.1.環境說明
當服務器多了時間準確一致與否是個大問題。雖然這個問題總是被忽略,但是統一的時間是很有必要的。下面就來討論在局域網內配置Linux時間服務器的方法。
配置的環境及要求:
1. 在192.168.10.0網段內,IP爲192.168.10.110的Linux主機作爲時間服務器。
2. 192.168.10.110服務器連外網,與較權威的時間服務器同步
3. 同網段內的其他機器每小時自動向192.168.10.110同步時間
2.2.詳細配置
2.2.1 服務器配置
配置NTP服務器(假定要配置的服務器IP爲192.168.10.110) ,NTP server的主配置文件爲/etc/ntp.conf,現對/etc/ntp.conf的各項進行說明
1. 配置前做好備份
# cp /etc/ntp.conf/etc/ntp.conf.bak
2. vi /etc/ntp.conf(以下是ntp.conf文件的內容)
# 記錄ntp server與上層ntp server上次聯接所花費的時間
driftfile/var/lib/ntp/drift
# 默認策略爲允許任何主機連接到此服務器進行時間同步
restrict defaultkod nomodify notrap nopeer noquery
restrict -6default kod nomodify notrap nopeer noquery
# 設置允許訪問此時間服務器的時間服務的IP地址
# 根據自己實際情況配置
restrict 127.0.0.1
restrict192.168.10.2 # 指定某臺機器時間同步
restrict 192.168.10.0mask 255.255.255.0
# 允許192.168.10.0/254子網內主機時間同步
restrict 0.0.0.0mask 0.0.0.0 nomodify notrap # 允許任何主機跟進行時間同步
# 設置此ntp server同上層ntp server做時間同步的域名/IP地址,prefer意味着首選IP地址
serverntp.sjtu.edu.cn prefer
server0.centos.pool.ntp.org
server1.centos.pool.ntp.org
server2.centos.pool.ntp.org
# 指定本機爲NTP服務器,供其他主機同步時間
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
以下爲此次環境配置
# vi /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noqueryrestrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
server ntp.sjtu.edu.cn prefer
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
3. 添加UDP123端口到白名單
# iptables -IINPUT -p udp --dport 123 -j ACCEPT
4. NTP server的維護
----添加爲服務
chkconfig --level345 ntpd on
----啓動
service ntpd start
or /etc/rc.d/init.d/ntpdstart
----停止
service ntpd stop
or /etc/rc.d/init.d/ntpdstop
----管理命令
ntpq –p # 查看本機和上層服務器的時間同步結果ntpstat # 查看ntp服務狀態信息的命令
ntptrace # 可以用來追蹤某臺時間服務器的時間對應關係
ntpdate IP # 客戶端要和NTP server進行時鐘同步。
netstat -tunl | grep 123 # 檢查ntp端口是否正常開啓(服務是否開啓)
/var/log/ntp/ntp.log # 查看ntp日誌
2.2.2 客戶端配置
1. Linux客戶端(局域網內對時間有要求的機器)
# echo “00 */1 * * * root /usr/sbin/ntpdate 192.168.10.110;/sbin/hwclock-w”>>/etc/crontab
每小時同NTP server進行一次時鐘同步,並寫入本機BIOS
三、注意事項
1. restrictdefault ignore # 默認禁止任何主機連接到此服務器進行時間同步
2. 解決ntp的錯誤no server suitable for synchronizationfound
在ntp客戶端運行ntpdate serverIP,出現no server suitable for synchronization found的錯誤。
http://www.blogjava.net/spray/archive/2008/07/10/213964.html
四、參考鏈接
1、http://blog.sina.com.cn/s/blog_511b46d10100agzf.html
2、http://hzhg12345.blog.163.com/blog/static/45376342201212014123560/
3、http://www.blogjava.net/spray/archive/2008/07/10/213964.html
五、糾錯反饋
QQ羣:216083473 (認證:IT零起步)