1. 程式人生 > >【linux】Linux下搭建ntp時間伺服器

【linux】Linux下搭建ntp時間伺服器

NTP(Network Time Protocol)伺服器是用於區域網伺服器時間同步使用的,可以保證區域網所有的伺服器與時間伺服器的時間保持一致,應用於對時間實時性要求高的、必須統一時間事件。

NTP時間伺服器是一個簡單的常用的伺服器,在工作中我們只要做到會用、能搭建起來就行,初學者不用太去深入研究

ntp時間伺服器的原理。

服務端配置:

一、安裝ntp服務      

yum install ntp ntpdate -y

二、修改ntp.conf配置檔案

cp /etc/ntp.conf /etc/ntp.conf.bak   #對ntp.conf檔案進行備份

 修改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 #在與上級時間伺服器聯絡時所花費的時間,記錄在driftfile引數後面的檔案內   # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default kod nomodify notrap nopeer noquery  #我們對於預設的客戶端拒絕所有的操作 restrict -6 default kod 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  #開啟內部遞迴網路介面lo 即允許本機地址一切的操作 restrict -6 ::1   # Hosts on local network are less restricted. restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap#我們允許區域網內所有客戶端連線到這臺伺服器同步時間.但是拒絕讓他們修改伺服器上的時間   service 192.168.75.132  # 這句也是手動增加的,指明區域網中作為NTP伺服器的IP   # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.rhel.pool.ntp.org    #網際網路上的三個ntp伺服器 server 1.rhel.pool.ntp.org server 2.rhel.pool.ntp.org   #sebroadcast 192.168.1.255 autokey    # broadcast server #broadcastclient                        # broadcast client #broadcast 224.0.1.1 autokey            # multicast server #multicastclient 224.0.1.1              # multicast client #manycastserver 239.255.254.254         # manycast server #manycastclient 239.255.254.254 autokey # manycast client   # Undisciplined Local Clock. This is a fake driver intended for backup # and when no outside source of synchronized time is available. server  127.127.1.0     # local clock #當伺服器與公用的時間伺服器失去聯絡時(連不上網),以區域網的時間伺服器為客戶端提供時間同步服務 fudge   127.127.1.0 stratum 10   # Enable public key cryptography. #crypto   includefile /etc/ntp/crypto/pw   # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys   # Specify the key identifiers which are trusted. #trustedkey 4 8 42   # Specify the key identifier to use with the ntpdc utility. #requestkey 8   # Specify the key identifier to use with the ntpq utility. #controlkey 8

# Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats

三、啟動NTP服務

service ntpd start

 為了使服務可以在系統引導的時候自動啟動,執行 

service ntpd startchkconfig ntpd on

四、檢查時間伺服器是否正確同步

 一個可以證明同步有問題的證據是:所有遠端伺服器的jitter值是4000並且delay和reach的值是0。

可能的原因有:

(1)有防火牆阻斷了與server之間的通訊,即123埠是否正常開放;

檢查ntp server的防火牆。可能是server的防火牆遮蔽了upd 123埠。可以用命令:

service iptables stop

來關掉iptables服務後再嘗試從ntp客戶端的同步,如果成功,證明是防火牆的問題,需要更改iptables的設定。

在vi /etc/sysconfig/iptables新增如下規則:

-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT 

(2) 此外每次重啟NTP伺服器之後大約要3-5分鐘客戶端才能與server建立正常的通訊連線,否則你在客戶端執行“ntpdate 伺服器ip”的時候將返回:     27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found

客戶端配置:

一、客戶端安裝ntp服務

yum install ntp ntpdate -y

二、配置ntp.conf檔案 

 只需增加 server 伺服器IP

三、手動同步時間

ntpdate 伺服器IP或域名

--------------------- 本文來自 -紙短情長 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/l_liangkk/article/details/80497992?utm_source=copy