NTP 服務的配置和使用

NTP 服務可以用來當做客戶端所在服務器的時間,也可以作爲服務端當做時間校準的服務。

安裝 NTP

$ yum -y install ntp
# 開機自啓
$ systemctl enable ntpd
# 先同步一下時間,此處使用的是阿里雲的 NTP 校準服務
$ ntpdate -u time1.aliyun.com

配置 NTP

  配置文件目錄:/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

# 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 time1.aliyun.com
server time2.aliyun.com
server time3.aliyun.com
server time4.aliyun.com
server time5.aliyun.com
server time6.aliyun.com
server time7.aliyun.com

#broadcast 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

# 修改內容 --- 允許上層時間服務器主動修改本機時間
#restrict time1.aliyun.com nomodify notrap noquery
#restrict time2.aliyun.com nomodify notrap noquery
#restrict time3.aliyun.com nomodify notrap noquery

# 修改內容 --- 只有在上級時鐘源失效時,NTP 纔會使用127.127.1.0的本地時鐘,將local時間作爲ntp服務器時間提供給ntp客戶端。NTP把本地主機的時鐘也看作外部時鐘源來處理,分配的地址是127.127.1.0
server 127.127.1.0
# 修改內容 --- 設置本地時鐘源的層次爲 5,這樣如果NTP服務從本地時鐘源獲取時間的話,NTP對外宣佈的時間層次爲 6
fudge  127.127.1.0 stratum 5


# 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

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

  配置完畢之後重啓 NTP 服務

$ systemctl restart ntpd

# 查看上級連通狀態
$ ntpq -p

上級連通狀態

屬性 作用
remote 本機和上層 NTP 的 ip 或主機名;+ 表示可用,- 表示不可用,* 表示推薦。
refid remote 的上一層 NTP 主機地址;
st Stratum(階層);表示經過 n = 2 次 NTP 同步到當前服務器。
t 類型;u 表示單播(unicast)。其它值包括本地(local)、多播(multicast)、廣播(broadcast)。
when 自從上次和服務器交互後經過的時間(以秒數計)。
poll 和服務器的輪詢間隔,以秒數計。
reach 表示和服務器交互是否有任何錯誤的八進制數;每成功連接一次它的值就會增加,值 337 表示 100% 成功(即十進制的255)。
delay 從本地機發送同步要求到 NTP 服務器的往返時間。
offset 當前服務器通過 NTP 時鐘同步與所同步時間源的時間偏移量,單位爲毫秒,offset 越接近於 0,主機和 NTP 服務器的時間越接近。
jitter 統計了在特定個連續的連接數裏 offset 的分佈情況。簡單地說這個數值的絕對值越小,主機的時間就越精確。
# 查看時間同步狀態
$ ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3
   time correct to within 50 ms
   polling server every 1024 s

  這裏 NTP 服務配置已經完畢,它既是客戶端也是服務端,如果要當服務端可以開放 123 端口,其他服務器上安裝 NTP 服務並把上級時間服務器的配置項更改到本服務器的 IP/域名/主機名 即可。

發佈了172 篇原創文章 · 獲贊 120 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章