安裝
yum install ntp
配置檔案 /etc/ntp.conf
restrict default kod nomodify
notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
# 用restrict控管權限
# nomodify - 使用者端不能更改ntp伺服器的時間參數
# noquery - 使用者端不能使用ntpq,ntpc等指令來查詢ntp伺服器
# notrap - 不提供trap遠端登陸
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
# 以下是兩個ntp伺服器
restrict cn.pool.ntp.org
restrict 164.67.62.194
# 用server設定上層ntp伺服器,“prefer”表示優先
server cn.pool.ntp.org 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
一個說明:
我在vmware下搭建的ntp伺服器,别人可以同步它,它自己的時間卻不對。照理是它同步上層,下層同步它呀。後來找了蠻久的資料,發現這個檔案:/etc/ntp/step-tickers,說是ntp伺服器無法主動同步上層伺服器,是以将位址寫入該檔案中即可。
我在實體機裡暫時沒發現這個問題,也許是實體機本來自己的時間就正确,就難以發現它有沒有主動同步上層伺服器。
運作ntp
service ntpd start/stop/restart
chkconfig ntpd on
檢視ntp的端口,應該看到123端口
netstat -unlnp
檢視ntp伺服器有無和上層連通
[root@S5 ~]# ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
注意:此指令出現上述synchronised結果比較慢,我的用了大概5分鐘。
檢視ntp伺服器與上層間的聯系
[root@S5 ~]# ntptrace -n 127.0.0.1
127.0.0.1: stratum 2, offset 0.064524, synch distance 0.121167
210.72.145.44: timed out, nothing received***Request timed out
檢視ntp伺服器與上層ntp伺服器的狀态
[root@S5 ~]# ntpq -p
其中,
remote - 本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先
refid - 參考上一層ntp主機位址
st - stratum階層
when - 多少秒前曾經同步過時間
poll - 下次更新在多少秒後
reach - 已經向上層ntp伺服器要求更新的次數
delay - 網絡延遲
offset - 時間補償
jitter - 系統時間與bios時間差
用戶端同步時間的方法
方法一:使用 ntpdate
ntpdate [IP_Address]
看下面的例子,其中192.168.1.5是我在内網搭建的ntp伺服器
[root@zy180 ~]# date
Thu Sep 25 13:04:17 CST 2008
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 14:33:13 ntpdate[10302]: step time server 192.168.1.5 offset 5327.700798 sec
[root@zy180 ~]# date
Thu Sep 25 14:33:18 CST 2008
ntpdate 的特點:
1,ntpdate隻運作一次就結束,即隻同步一次。是以要使用ntpdate同步時間的話要将該指令寫入cron中。
vi /etc/crontab
* * * * * root ntpdate 192.168.1.5 && hwclock -w
之是以使用hwclock -w指令稍後再說。
2,ntpdate同步時間是跳躍的,如上面的顯示,時間立即就同步了,這将可能導緻檔案時間标記、監控資料的紊亂。
方法二:使用ntpd服務
step1:/etc/ntpd.conf 寫入 server [IP_Address]
step2:chkconfig ntpd on
step3:service ntpd start
大概過一刻鐘開始起作用。
FAQ:系統報錯 - Sep 25 11:59:39 localhost kernel: set_rtc_mmss: can't update from 7 to 59
==================================網上查得源碼==========================================
if (abs(real_minutes - cmos_minutes) < 30) { update_cmos() } else { printk("set_rtc_mmss: can't update from %d to %d\n", cmos_minutes, real_minutes); } ========================================================================================
bios時間與系統時間差異超過30分鐘,就會報上述錯誤,隻要修改bios時間或系統時間即可。
虛拟機下個人體驗:
我的vm用戶端用ntpd同步伺服器後,慢慢變成标準時間,可接着又比标準時間越來越慢,然後出現上述報錯。是以我覺得ntpd的同步頻率比我的vm時鐘頻率跑得慢。
ntpd 的特點:
1,ntpd服務始終運作,定期同步時間。
2,ntpd服務是慢慢改變時間直至标準時間。
3,如果系統時間與bios時間差異超過30分鐘,ntpd就會報錯。