NTP服務的安裝
[root@m01 ~]# yum install ntp
[root@m01 ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64
NTP 服務端配置
[root@m01 ~]# egrep -v "#|^$" /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server ntp1.aliyun.com
server time.nist.gov
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
ntp.conf配置詳解
driftfile /var/lib/ntp/drift #driftfile 參數解決NTP伺服器校準時間時的傳送延遲,格式:driftfile 檔案名;在與上級時間伺服器聯系時所花費的時間記錄在driftfile參數後面的檔案内。注意:driftfile 後面接的檔案必須是完整的全路徑,不能是連結檔案,并且檔案權限要寫成ntpd守護程序可寫的狀态
restrict 是控制權限參數,主要的文法格式為:restrict IP位址 mask 子網路遮罩 參數。其中IP 可以是ip 也可以是default(即:所有IP);
參數包含:
ignore:關閉所有的NTP聯機服務
nomodify:用戶端不能更改服務端的時間參數,但是用戶端可以通過服務端進行網絡校對
notrust:用戶端除非通過認證,否則用戶端來源将被認為不信任子網
noquery:不提供用戶端的時間查詢
注:如果參數麼有設定,預設是沒有任何限制
restrict -6 default kod nomodify notrap nopeer noquery#拒絕IPV6
restrict default kod nomodify notrap nopeer noquery #拒絕IPV4
restrict 127.0.0.1 #放行本機來源
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 放行網絡來源
server ntp1.aliyun.com prefer 以這個為最優先級(上級)
server time.nist.gov 以這個為最上級備機(參考)
restrict 133.100.9.2 #開放server 通路我們ntp服務的權限
NTP服務的啟動
在啟動前,先使用ntpdate手動同步下時間,免得本機與外部時間伺服器時間差距太大,讓ntpd不能正常同步。
# ntpdate -u 202.112.10.36
22 Dec 16:52:38 ntpdate[6400]: adjust time server 202.112.10.36 offset 0.012135 sec
[root@m01 ~]# /etc/init.d/ntpd start
正在啟動 ntpd: [确定]
如果定時任務裡面有時間同步任務時候要注釋掉
[root@m01 ~]# chkconfig ntpd on
[root@m01 ~]# netstat -lntup|grep ntp
udp 0 0 172.16.1.61:123 0.0.0.0:* 2312/ntpd
udp 0 0 10.0.0.61:123 0.0.0.0:* 2312/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2312/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2312/ntpd
udp 0 0 fe80::20c:29ff:fed3:8d56:123 :::* 2312/ntpd
udp 0 0 fe80::20c:29ff:fed3:8d4c:123 :::* 2312/ntpd
udp 0 0 ::1:123 :::* 2312/ntpd
udp 0 0 :::123 :::* 2312/ntpd
[root@m01 ~]# tail -10 /var/log/messages
Jun 26 19:49:35 m01 ntpd[2312]: Listen normally on 5 lo ::1 UDP 123
Jun 26 19:49:35 m01 ntpd[2312]: Listen normally on 6 eth0 fe80::20c:29ff:fed3:8d4c UDP 123
Jun 26 19:49:35 m01 ntpd[2312]: Listen normally on 7 eth1 fe80::20c:29ff:fed3:8d56 UDP 123
Jun 26 19:49:35 m01 ntpd[2312]: Listening on routing socket on fd #24 for interface updates
Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c016 06 restart
Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c011 01 freq_not_set
Jun 26 19:49:36 m01 ntpd[2312]: 0.0.0.0 c61c 0c clock_step +222384.777553 s
Jun 29 09:36:01 m01 ntpd[2312]: 0.0.0.0 c614 04 freq_mode
Jun 29 09:36:02 m01 ntpd[2312]: 0.0.0.0 c618 08 no_sys_peer
如何确定伺服器順利的更新了時間?使用下面的指令檢視
[root@m01 ~]# ntpstat
unsynchronised
polling server every 64 s
上面是麼有成功的,可以重新開機ntp服務,如下:
[root@m01 ~]# /etc/init.d/ntpd restart
關閉 ntpd: [确定]
synchronised to NTP server (182.92.12.11) at stratum 3
time correct to within 7952 ms
這裡代表順利的更新了時間
[root@m01 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*time5.aliyun.co 10.137.38.86 2 u 43 64 7 9.230 8.139 2.038
host-24-56-178- .ACTS. 1 u 105 64 2 333.570 -34.886 0.000
上面的ntpq -p列出了與NTP服務相關的上聯ntp服務的狀态,其中每行的意思:
remote:NTP的的主機ip或是主機名,其中*代表正在使用當中的上聯NTP;+代表也能上聯,作為下個提供時間更新的候選
refid:參考的上一級NTP伺服器位址
st: remote遠端伺服器的級别,由于NTP是層級結構,有頂端的伺服器,也有多層的Relay Server再到用戶端,是以伺服器從高到低分為1-16個級别,為了減緩負荷和網絡堵塞,原則上應該直接連到級别為1的伺服器上
when :上次成功請求後,到現在的秒數
poll: 本地和遠端伺服器多少時間進行一次時間同步。剛開始的運作ntp的時候找個poll值會比較小,那樣和伺服器的同步頻率就增加了,可以盡快調整的正确的時間範圍,之後poll的值會逐漸增大,同步的頻率也會相對減小
reach : 這是一個八進制值,用來測試能和伺服器連接配接,每成功一次它的值就會增加
delay : 從本地發送同步要求到ntp伺服器的時間,也就是網絡傳輸的延遲時間,機關是10^(-3)秒
offset : 主機通過NTP時鐘同步與所同步時間源的時間偏移量,機關為毫秒(ms)。offset越接近于0,主機和ntp伺服器的時間越接近
jitter : 這是一個用來做統計的值. 它統計了在特定個連續的連接配接數裡offset的分布情況. 簡單地說這個數值的絕對值越小,主機的時間就越精确
NTP服務的安全配置:
雖然已經在ntp.conf中使用restrict參數配置了限制,但是最好在IPtables中再次限制
iptables -A INPUT -i $EXTIF -p udp -s 192.168.100.0/24 --dport 123 -j ACCEPT
NTP客戶的時間校準
1、手動的方式進行校準
使用date hwclock-r;hwclock -w這三個指令來檢視時間并且寫入bioos中
2、網絡校對的方式
ntpdate 10.0.0.61
使用hwclock -w 寫入到bios中
添加crontab定時任務
通過在用戶端啟動ntp伺服器程序,在ntp.conf配置檔案中配置
restrict 10.0.0.61 授權來自61伺服器
server 10.0.0.61 這個就是ntp伺服器位址
本文轉自 kesungang 51CTO部落格,原文連結:http://blog.51cto.com/sgk2011/1793889,如需轉載請自行聯系原作者