內網環境搭建NTP服務器


說在前面:ntp和ntpdate區別

①兩個服務都是centos自帶的(centos7中不自帶ntp)。ntp的安裝包名是ntp;ntpdate的安裝包是ntpdate。他們並非由一個安裝包提供。

②ntp守護進程為ntpd,配置文件是/etc/ntp.conf

③ntpdate用於客戶端的時間矯正,非NTP服務器可以不啟動NTP。

 

簡介:ntp全名 network time protocol 。NTP服務器可以為其他主機提供時間校對服務。

 

環境准備:兩台服務器,一台作為NTP服務器,另一台作為client端向服務器同步時間測試。

  NTP服務器:156.0.26.6

  client端:156.0.0.27

安裝與配置:我們從配置文件的角度來講解一下ntp的配置

 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  #默認即可。driftfile用來指定記錄本機與上層NTP server之間的頻率誤差。單位是百萬分之一秒。
 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 nomodify notrap nopeer noquery 
    #restrict用來管理權限控制。格式為 restrict [單個ip|網絡|default] parameter
      parameter:
        ignore:拒絕所有的ntp連接
        nomodify:客戶端不能使用ntpc和ntpq這兩個程序來更改服務器的時間參數,但客戶端可以通過此主機來進行網絡校時。
        noquery:客戶端不能使用ntpc和ntpq等命令來查詢時間服務器,等於不提供網絡校時服務。
        notrap:不提供trap這個網絡時間登陸的功能
        notrust:拒絕沒有認證的客戶端
  示例:restrict 156.0.26.7 nomodify
9 10 # Permit all access over the loopback interface. This could 11 # be tightened as well, but to do so would effect some of 12 # the administrative functions. 13 restrict 127.0.0.1 #以下兩條默認,放行本機來源 14 restrict ::1 15 16 # Hosts on local network are less restricted. 17 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 18 19 # Use public servers from the pool.ntp.org project. 20 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 21 server 0.centos.pool.ntp.org iburst #以下四條為默認,注釋掉即可 22 server 1.centos.pool.ntp.org iburst 23 server 2.centos.pool.ntp.org iburst 24 server 3.centos.pool.ntp.org iburst 25   server:用來設置上層NTP服務器,說白了就是client向誰請求NTP時間同步。
    特別注意,在內網環境中由於無法連接到內網,所以沒有辦法向例如國家授時服務中心210.72.145.44同步時間
    只能將內網中的某台主機設置為server,用以向其他內網服務器提供NTP服務。
  server 127.127.1.0 prefer #以本機時間作為時間服務。內網中這個配置一定要加上,否則會導致NTP服務不可用
                 #prefer代表這台主機優先級最高。

26 #broadcast 192.168.1.255 autokey # broadcast server 27 #broadcastclient # broadcast client 28 #broadcast 224.0.1.1 autokey # multicast server 29 #multicastclient 224.0.1.1 # multicast client 30 #manycastserver 239.255.254.254 # manycast server 31 #manycastclient 239.255.254.254 autokey # manycast client 32 33 # Enable public key cryptography. 34 #crypto 35 36 includefile /etc/ntp/crypto/pw 37 38 # Key file containing the keys and key identifiers used when operating 39 # with symmetric key cryptography. 40 keys /etc/ntp/keys ##除了restrict來限制客戶端連接外,還可以通過秘鑰方式來給客戶端認證。 41 42 # Specify the key identifiers which are trusted. 43 #trustedkey 4 8 42 44 45 # Specify the key identifier to use with the ntpdc utility. 46 #requestkey 8 47 48 # Specify the key identifier to use with the ntpq utility. 49 #controlkey 8 50 51 # Enable writing of statistics records. 52 #statistics clockstats cryptostats loopstats peerstats 53 54 # Disable the monitoring facility to prevent amplification attacks using ntpdc 55 # monlist command when default restrict does not include the noquery flag. See 56 # CVE-2013-5211 for more details. 57 # Note: Monitoring will not be disabled with the limited restriction flag. 58 disable monitor

示例

服務器端需要修改/etc/ntp.conf,添加以下內容

server 127.127.1.0 prefer  #設置本機為NTP服務器
restrict 156.0.26.7 #允許客戶端156.0.26.7向本機請求時間同步
restrict 156.0.26.0 mask 255.255.255.0 #允許客戶端156.0.26.0網段的所有主機向本機請求時間同步

客戶端需要修改/etc/ntp.conf,添加以下內容

  server 156.0.26.6    #指名上層NTP服務器

  restrict 156.0.26.6       #放行156.0.26.6

驗證

首先驗證服務是否啟動成功

  啟動服務 :service ntpd start

  查看服務是否啟動:netstat -tunlp |grep ntp ;ntp默認監聽於UDP的123端口

其次驗證NTP是否正常工作

  此處需要使用156.0.26.7這台Client服務器,在此主機上可以使用命令ntpstat或ntpq -p 這兩個命令。

ntpstat:這條命令可以查看我們的客戶端(此處為156.0.26.7)是否與server(156.0.26.6) 連接成功。

 上圖的大致意思是:本機已經和156.0.26.6這台位於第七層的NTP服務器同步時間,時間精確到36ms以內。每個512s去同步一次時間。

ntpq  -p:此命令可以列出當前主機的NTP和上層NTP的狀態。

 

remote:上層NTP服務器的IP或者主機名。主要最左邊的*

  *:代表目前正在使用中的上層FTP

  +:已經連接成功,且可以作為下一個提供時間服務的候選人。

refid:它指的是給遠程服務器(156.0.26.6)提供時間同步的服務器,本機上級的上級NTP服務器。

  為什么顯示為LOCAL(0)?

    由於此處為內網環境,無法去外網公用的主機同步時間,因此我們在156.0.26.6上設置的是156.0.26.6自身作為時間服務器同時,使用server 127.127.1.0 prefer指名他的上級NTP為自身。

 st:就是stratum層級。 類似於DNS,NTP是層級結構,有頂端的服務器,最多有15層。 為了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器。

when:幾秒之前同步過時間

poll:在過多長時間去同步時間。

reach:已經同步時間的次數

delay:網絡傳輸過程中的延遲,單位是10的-6次方秒。

offset:時間修正值,這是個最關鍵的值, 它告訴了我們本地機和服務器之間的時間差別.。單位是10的-3次方。

jitter:linux系統時間(軟件時間)與BIOS硬件時間的差異時間,單位是10的-6次方秒。在主機和NTP服務器同步時間歐,可以使用 hwclock -w將系統時間寫入BIOS. 

 


免責聲明!

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



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