ntp時間服務器采用stratum分級架構來處理時間同步;舉例說明:你搭建了一臺ntp服務器,然后同步的server為stratum-1,你的ntp則為stratum-2,你的下級ntp則為tratum-3。依此類推,最多為15層。
1.ntp server安裝:
[root@localhost ~]# yum -y install ntp tzdata
ntp相關文件說明:
/etc/ntp.conf ntp server配置文件 /usr/sbin/ntpd ntp server程序 /usr/sbin/ntpdate ntp client校正工具 /etc/sysconfig/clock 時區配置文件
2./etc/ntp.conf配置文件說明,主要配置restrict和server
#restrict設置格式: #restrict [授權同步的網段] mask [netmask] [parameter] 例:restrict 192.168.6.0 mask 255.255.252.0 nomodify parameter說明: kod kod技術可以阻止“Kiss of Death “包對服務器的破壞 nomodity client可通過ntp進行時間同步,但不能更改server參數 notrap 不提供trap遠程登陸功能 nopeer 不與其它同一層的ntp server進行時間同步 noquery 拒絕ntp時間同步 notrust 拒絕無認證的client ignore 拒絕連接到ntp server #server設置格式 #server [hostname|ip][parameter] 例:server server asia.pool.ntp.org prefer parameter說明: prefer 最高優先級 burst 當一個運程NTP服務器可用時,向它發送一系列的并發包進行檢測。 iburst 當一個運程NTP服務器不可用時,向它發送一系列的并發包進行檢測。
3.ntp配置實例 server端
1)配置/etc/ntp.conf [root@localhost ~]# vim /etc/ntp.conf 1 # For more information about this file, see the man pages 2 # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). 3 4 driftfile /var/lib/ntp/drift 5 6 # Permit time synchronization with our time source, but do not 7 # permit the source to query or modify the service on this system. 8 restrict default kod nomodify notrap nopeer noquery 9 restrict -6 default kod nomodify notrap nopeer noquery 10 11 # Permit all access over the loopback interface. This could 12 # be tightened as well, but to do so would effect some of 13 # the administrative functions. 14 restrict 127.0.0.1 15 restrict -6 ::1 16 17 # Hosts on local network are less restricted. 18 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 19 restrict 192.168.6.0 mask 255.255.252.0 nomodify 20 21 # Use public servers from the pool.ntp.org project. 22 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 23 #server 0.centos.pool.ntp.org iburst 24 #server 1.centos.pool.ntp.org iburst 25 #server 2.centos.pool.ntp.org iburst 26 #server 3.centos.pool.ntp.org iburst 27 server asia.pool.ntp.org prefer 28 server 0.asia.pool.ntp.org 29 server 1.asia.pool.ntp.org 30 server 2.asia.pool.ntp.org 31 server time.nist.gov 32 33 #broadcast 192.168.1.255 autokey # broadcast server ... 2)NTP啟動與端口檢查: [root@localhost ~]# service ntpd start Starting ntpd: [root@localhost ~]# chkconfig ntpd on [root@localhost ~]# chkconfig --list | grep ntp ntpd 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉 ntpdate 0:關閉 1:關閉 2:關閉 3:關閉 4:關閉 5:關閉 6:關閉 [root@localhost ~]# netstat -nutlp | grep ntp udp 0 0 192.168.2.213:123 0.0.0.0:* 21798/ntpd udp 0 0 192.168.6.213:123 0.0.0.0:* 21798/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 21798/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 21798/ntpd udp 0 0 fe80::221:f6ff:fed4:d502:123 :::* 21798/ntpd udp 0 0 fe80::221:f6ff:fed4:d501:123 :::* 21798/ntpd udp 0 0 ::1:123 :::* 21798/ntpd udp 0 0 :::123 :::* 21798/ntpd 3)ntp server與上聯是否同步 #查看server與上聯是否同步,時間校正約8279ms,每64s輪循更新一次。 [root@localhost ~]# ntpstat synchronised to NTP server (62.201.225.9) at stratum 3 #==>上層ntp time correct to within 8279 ms #==>校正時間差 polling server every 64 s #==>下次同步時間 #查看server與上聯的狀態 [root@localhost ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *time.iqnet.com 62.201.214.162 2 u 146 64 124 393.411 -101.29 40.435 -220.231.122.105 123.204.45.116 4 u 20 64 377 341.475 58.745 47.945 +vps.jre655.com 10.84.87.146 2 u 24 64 377 211.095 0.177 38.002 web10.hnshostin 158.43.128.33 2 u 17 64 177 392.506 -134.76 39.146 +24.56.178.140 .ACTS. 1 u 27 64 377 282.739 -59.521 42.959 參數說明: reomte server上聯的ntp主機名或ip; 注意最左端符號;*表示當前正使用的上層ntp;+代表與本機server也有連接,作為侯選ntp refid 給上層ntp提供時間校對的服務器 st 上層npt stratum級別 when 上一次與上層ntp同步的時間,單位為秒。 poll 下一次與上層ntp同步的時間 reach 已經同上層ntp同步更新的次數 delay 網絡傳輸過程中的延遲時間,單位為10^(-6)s offset 時間補償,單位為10^(-3)s jitter 系統時間與bios硬件時間差,單位為10^(-6)s
4.ntp配置實例client端
通過crontab設置更新時間 [root@dns-2-253 ~]# crontab -l */5 * * * * /usr/sbin/ntpdate 192.168.6.213 &> /dev/null client測試是否同步成功 [root@dns-2-253 ~]# ntpdate 192.168.6.213 10 Nov 21:25:43 ntpdate[26381]: step time server 192.168.6.213 offset 1.318393 sec 備注:如果無法同步,需查是否開啟了防火墻。
原創文章,作者:老鼠上了貓,如若轉載,請注明出處:http://www.www58058.com/9226
非常有參考價值,上次我們部署crond的時候遇到不少問題,一個crond不生效,一個報錯已有進程運行中
@stanley:這樣配置有個奇怪的地方,restrict明明只放行了內網,但在機房外部卻可通過服務器的外網ip進行ntp同步。