搭建和配置NTP服務器


NTP服務概述:

1、NTP【Network Time Protocol(NTP)】是用來使計算機時間同步化的一種協議,它可以使計算機對其服務器或時鍾源(如石英鍾,GPS等等)做同步化,它可以提供高精准度的時間校正(LAN上與標准間差小於1毫秒,WAN上幾十毫秒),且可介由加密確認的方式來防止惡毒的協議攻擊。時間按NTP服務器的等級傳播。按照離外部UTC源的遠近把所有服務器歸入不同的Stratum(層)中,最多16層。

 

 

 

2、端口

[root@localhost ~]# vim /etc/services

ntp              123/tcp

ntp              123/tcp                                             #Network Time Protocol

3、檢查ntp服務的安裝情況

[root@localhost ~]# rpm -qa |grep ntp
ntpdate-4.2.6p5-25.el7.centos.2.x86_64                        #客戶端
ntp-4.2.6p5-25.el7.centos.2.x86_64                               #服務端

 

4、配置ntp

[root@localhost mnt]# vi /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1

restrict 192.168.11.0 mask 255.255.255.0       #允許192.168.11.0網段中的服務,訪問本ntp服務器來同步時間。

restrict 192.168.10.25                                       #允許單個IP地址訪問本ntp服務器來同步時間

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst                        #默認配置提供了四個時鍾同步服務器。

server 1.centos.pool.ntp.org iburst                        #指定為本NTP服務器的上游ntp服務器。
server 2.centos.pool.ntp.org iburst                        #同步時間為從上到下,寫的越靠前優先級越高。第一個服務器同步
server 3.centos.pool.ntp.org iburst                        #不了時間,則往下尋找另一個NTP服務器來同步時間。

server 127.127.1.0                                                #local clock  如果上面的服務器都無法同步時間,就和本地系統時間同步。

fudge 127.127.1.0 stratum 10                               #指定127.127.1.0 為第10層。ntp和127.127.1.0同步完后,就變成了11層。ntp是層次階級的。

                       #同步上層服務器的stratum大小不能超過或等於16。

 

5、NTP服務的啟動與觀察

先同步一下本ntp服務器的時間

[root@localhost ~]# ntpdate 0.centos.pool.ntp.org
30 Oct 19:27:16 ntpdate[137647]: adjust time server 86.108.190.23 offset 0.000980 sec

或手動同步

[root@localhost ~]# date -s "2021-10-30 19:27:16"
Sat Oct 30 19:27:16 CST 2021

 

 

然后啟動ntp服務

[root@localhost ~]# service ntpd start 

 

設置ntp服務開機自啟

[root@localhost ~]# chkconfig ntpd on

查看123端口是否監聽

[root@localhost ~]# netstat -nap |grep 123

 

查看本ntp服務器與上層服務器的連接狀態

[root@localhost ~]# ntpstat
synchronised to NTP server (192.36.143.130) at stratum 2
time correct to within 235 ms
polling server every 64 s

 

列出我們的ntp服務器與上游ntp服務器之間的連接狀態

[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
localhost.pgw .INIT. 16 u - 64 0 0.000 0.000 0.000
*time100.stupi.s .PPS. 1 u 61 64 77 84.571 1.309 0.604
+tor.nocabal.de 131.188.3.222 2 u 58 64 77 69.126 -3.367 0.444
2001:440:1880:5 .INIT. 16 u - 64 0 0.000 0.000 0.000
+time.cloudflare 10.80.8.4 3 u 64 64 77 19.632 -0.309 0.667
time-b-g.nist.g .NIST. 1 u 8 64 177 149.759 1.451 0.281
LOCAL(0) .LOCL. 10 l 398 64 100 0.000 0.000 0.000

remote:本機與上層ntp的ip或主機名,“+”表示優先,“*”表示次優先。

refid:參考的上一層ntp主機地址

st:stratum階層

poll:每幾秒更新一次

offset:時間補償的結果

 

     拓展:

1、客戶端在手動測試同步時間時遇到如下情況

[root@localhost ~]# ntpdate 192.168.11.23

30 Oct 19:55:43 ntpdate[450405]: the NTP socket is in use, exiting

 

需要先停掉客戶端的ntp服務,再進行同步

[root@localhost ~]# service ntpd stop

 

再嘗試手動測試同步

[root@localhost ~]# ntpdate 192.168.11.23
30 Oct 19:57:55 ntpdate[445112]: adjust time server 192.168.11.23 offset 0.000074 sec

 

測試好后再啟動ntp服務

[root@localhost ~]# service ntpd start

 

可能還會遇到另一種情況

[root@localhost ~]# ntpdate 192.168.11.23

30 Oct 19:59:55 ntpdate[11520]: no server suitable for synchronization found

這是由於每次重啟ntp服務之后大約需要3-5分鍾客戶端才能與server建立正常的通訊連接。一般等待幾分鍾就可以了。

 

2、客戶端的自動同步時間除了通過啟用ntp服務,還可以配合cron命令,來進行定期同步。

[root@localhost ~]# crontab -e

0 12 * * * /usr/sbin/ntpdate 192.168.11.23


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM