一 環境需求
1.1 需求
User-client:局域網所有節點主機;
IN-NTP Server:隱藏於局域網內部的NTP服務器;
Border-NTP:邊界NTP服務器,用於同步外部時鍾,同時對內部IN-NTP服務器提供校對;
亞洲授時中心:公網NTP授時中心。
內部局域網所有節點client需要和IN-NTP Server同步,IN-NTP Server對外隱藏,同時通過 Border-NTP進行同步, Border-NTP邊界NTP服務器實時同步公網授時中心。
1.2 架構
注意:
- Border-NTP部署在邊界上,作為和互聯網(亞洲授時中心)同步,同時為局域網內部IN-NTP提供同步服務。
- User-client,包括所有局域網內客戶端等,從兩台(一主一備)IN-NTP進行同步。
二 Border-NTP配置
2.1 服務包安裝
1 [root@border_ntp ~]# rpm -qa | grep ntp 2 [root@border_ntp ~]# yum -y install ntp #安裝ntp服務
2.2 ntp配置
1 [root@border_ntp ~]# vi /etc/ntp.conf 2 driftfile /var/lib/ntp/drift 3 #定義與上級時間服務器聯系時所花費的時間,記錄在driftfile參數后面的文件內。 4 driftfile /var/lib/ntp/drift 5 pidfile /var/run/ntpd.pid #進程pid文件 6 logfile /var/log/ntp.log #開啟日志記錄 7 #定義與上級時間服務器聯系時所花費的時間,記錄在driftfile參數后面的文件內。 8 9 restrict default nomodify notrap nopeer noquery #默認拒絕所有NTP連接 10 restrict 127.0.0.1 11 restrict ::1 #開啟本地授權 12 13 restrict 172.24.8.72 mask 255.255.255.255 nomodify notrap #添加IN-NTP Server允許校時 14 restrict 172.24.8.73 mask 255.255.255.255 nomodify notrap #添加IN-NTP Server允許校時 15 16 #server 0.cn.pool.ntp.org 17 #server 1.asia.pool.ntp.org 18 #restrict 0.cn.pool.ntp.org nomodify notrap noquery 19 #restrict 1.asia.pool.ntp.org nomodify notrap noquery 20 #添加Border-NTP的上一級時間中心,即亞洲授時中心服務器,此處建議注釋,轉而采用國內阿里雲時鍾,相對網絡更穩定,如下。 21 server ntp1.aliyun.com iburst minpoll 4 maxpoll 10 22 restrict ntp1.aliyun.com nomodify notrap nopeer noquery 23 server ntp2.aliyun.com iburst minpoll 4 maxpoll 10 24 restrict ntp2.aliyun.com nomodify notrap nopeer noquery 25 #允許阿里授時中心修改本地時間。 26 server 127.127.1.0 27 fudge 127.127.1.0 stratum 4 28 #當授時不可用,采用本地時間,stratum級別為4。
2.3 其他配置
1 [root@border_ntp ~]# chown ntp:ntp /var/log/ntp.log 2 [root@border_ntp ~]# chcon -t ntpd_log_t /var/log/ntp.log 3 [root@border_ntp ~]# firewall-cmd --permanent --add-service=ntp 4 [root@border_ntp ~]# firewall-cmd --reload 5 [root@border_ntp ~]# setenforce 0
提示:若開啟日志功能,需要ntp進程具備寫入日志權限。同時防火牆開放ntp服務,SELinux寫入上下文授權。
建議:若沒有特殊必要,建議關閉SELinux和防火牆。
2.4 開啟服務
1 [root@border_ntp ~]# systemctl start ntpd.service 2 [root@border_ntp ~]# systemctl enable ntpd.service
提示:開啟ntp server后,需要等待一定時間(通常為5分鍾以內),才能實現和亞洲授時中心的同步。
2.5 驗證
1 [root@border_ntp ~]# ntpq -np
提示:ntpq相關含義見附錄一。
三 IN-NTP Server配置
3.1 服務包安裝
1 [root@in_ntp01 ~]# rpm -qa | grep ntp #查看是否安裝ntp 2 [root@in_ntp01 ~]# yum -y install ntp #安裝ntp服務
3.2 ntp配置
1 [root@in_ntp01 ~]# vi /etc/ntp.conf 2 driftfile /var/lib/ntp/drift 3 pidfile /var/run/ntpd.pid #進程pid文件 4 logfile /var/log/ntp.log #開啟日志記錄 5 #定義與上級時間服務器聯系時所花費的時間,記錄在driftfile參數后面的文件內。 6 tinker stepout 0 7 restrict default ignore #默認拒絕所有連接 8 restrict -6 default ignore #默認拒絕所有ipv6連接 9 restrict 127.0.0.1 10 restrict -6 ::1 #開啟本地授權 11 12 restrict 172.24.8.0 mask 255.255.255.0 nomodify notrap #允許局域網內網段校時 13 14 restirct 172.24.8.71 nomodify notrap nopeer noquery 15 #允許根據上一級時間修改本地時間 16 server 172.24.8.71 iburst minpoll 3 maxpoll 3 prefer 17 #添加上一層Border-NTP服務地址 18 19 #peer 172.24.8.72 iburst minpoll 4 maxpoll 6 20 #peer 172.24.8.73 iburst minpoll 4 maxpoll 6 21 #服務器接收其他服務器的地址,同時也會為其他設備提供NTP服務器,即互相同步,用於在孤島環境中,若同時具備上層時鍾,可注釋掉。 22 23 server 127.127.1.0 24 fudge 127.127.1.0 stratum 8 #上一級不可用,使用本地時間,stratum為8
3.3 其他配置
參考2.3.
3.4 開啟服務
1 [root@in_ntp01 ~]# systemctl start ntpd 2 [root@in_ntp01 ~]# systemctl enable ntpd
3.5 驗證
1 [root@in_ntp01 ~]# ntpq -np
注意:兩台IN-NTP Server都需要配置。
四 user-client配置
4.1 服務包安裝
1 [root@client ~]# rpm -qa | grep ntp #查看是否安裝ntp 2 [root@client ~]# yum -y install ntp #安裝ntp服務
4.2 ntp配置
1 [root@client ~]# vi /etc/ntp.conf 2 driftfile /var/lib/ntp/drift 3 pidfile /var/run/ntpd.pid #進程pid文件 4 logfile /var/log/ntp.log #開啟日志記錄 5 #定義與上級時間服務器聯系時所花費的時間,記錄在driftfile參數后面的文件內。 6 7 tinker panic 600 8 #當client和server時差在600秒以內,使用漸變調整client時間,超過600秒,ntpd進程自動終止,需要手動進行調整。 9 restrict default ignore #默認拒絕所有連接 10 restrict -6 default ignore #默認拒絕所有ipv6連接 11 restrict 127.0.0.1 #開啟本地授權 12 13 restrict 172.24.8.72 nomodify notrap noquery #允許根據上一級時間修改本地時間 14 restrict 172.24.8.73 nomodify notrap noquery #允許根據上一級時間修改本地時間 15 16 server 172.24.8.72 iburst minpoll 4 maxpoll 6 prefer 17 server 172.24.8.73 iburst minpoll 4 maxpoll 6
4.3 其他配置
參考2.3.
4.4 開啟服務
1 [root@client ~]# systemctl start ntpd 2 [root@client ~]# systemctl enable ntpd
4.5 驗證
1 [root@client ~]# ntpq -np
1 [root@client ~]# ntpstat
附錄一
ntpq參數解釋:
remote:本地服務器所連接的遠程NTP服務器。
refid:NTP服務器使用的上一級ntp服務器,即給遠程ntp服務器提供時間同步是服務器。。
st :remote遠程服務器的級別,由於NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端。所以服務器從高到低級別可以設定為1-16,為了減
緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器的。
when: 上一次成功請求之后到現在的秒數。
poll : 本地機和遠程服務器多少時間進行一次同步(單位為秒),在一開始運行NTP的時候這個poll值會比較小,那樣和服務器同步的頻率也就增加了,可以盡快調
整到正確的時間范圍,之后poll值會逐漸增大,同步的頻率也就會相應減小。
reach:這是一個八進制值,用來測試能否和服務器連接,每成功連接一次它的值就會增加
delay:從本地機發送同步要求到ntp服務器的round trip time
offset:主機通過NTP時鍾同步與所同步時間源的時間偏移量,單位為毫秒(ms)。offset越接近於0,主機和ntp服務器的時間越接近。
jitter:這是一個用來做統計的值,它統計了在特定個連續的連接數里offset的分布情況,簡單地說這個數值的絕對值越小,主機的時間就越精確。
『 * 』:代表目前正在作用當中的NTP,即主NTP Server;
『 + 』:代表輔助的NTP Server和帶有*號的服務器一起為我們提供同步服務, 當主NTP Server服務器不可用時備服務器接管,即作為下一個提供時間更新的候
選者。
『 - 』:遠程服務器被認為是不合格的NTP Server。
『 x 』:遠程服務器不可用。
附錄二
ntp服務,默認只會同步系統時間。若需要ntp同時同步硬件時間,可以在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes ,則可以讓硬件時間與系統時間一起同步。
SYNC_HWCLOCK=yes
注意:允許BIOS與系統時間同步,也可以通過hwclock -w 命令。