安裝
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 0.cn.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
server 3.centos.pool.ntp.orglinux
broadcast 172.255.255.255 ios
broadcastclient web
disable authcentos
broadcast是指對哪一個局域網廣播時間,broadcastclient和disable auth是指客戶端不須要加密驗證就能夠同步服務器服務器
三、編輯/etc/ntp/step-tickers寫入上層NTP服務器IP網絡
cn.pool.ntp.org
socket
127.0.0.1加密
四、配置NTP服務自動啓動spa
chkconfig ntpd onrest
五、啓動NTP
sercive ntpd start
# 系統時間與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就會報錯。
總結:
1,推薦使用 ntpd 服務。
2,ntpd開啓後,沒法使用ntpdate,看以下例子:
[root@zy180 ~]# service ntpd start
Starting ntpd: [ OK ]
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 15:25:22 ntpdate[12334]: the NTP socket is in use, exiting
附:linux的時間、時區
1,時間
系統時間:date 命令
bios時間:hwclock命令 (-r 讀取bios時間,-w 寫入bios時間)
每次開機時系統從新讀bios時間。
因此前面ntpdate命令時的cron腳本須要用到hwclock,以免機器重啓後時間混亂。
2,時區
linux的全部時區文件都放在 /usr/share/zoneinfo 下。
linux系統的時區文件是 /etc/localtime,這是個時間格式的檔案,不是ACSII碼。它與/usr/share/zoneinfo/下的本身的時區文件是同樣的。
個人時區是Asia-Shanghai,用diff比較兩個文件,以下:[root@zy180 ~]# diff /usr/share/zoneinfo/Asia/Shanghai /etc/localtime[root@zy180 ~]#在 /etc/sysconfig/clock 文件中也寫有本時區的信息:[root@zy180 ~]# cat /etc/sysconfig/clockZONE="Asia/Shanghai"UTC=falseARC=false