NTP服務搭建
ntp和ntpdate區別
1.兩個服務都是centos自帶的(centos7中不自帶ntp)。ntp的安裝包名是ntp,ntpdate的安裝包是ntpdate。他們並非由一個安裝包提供。
2.ntp守護進程為ntpd,配置文件是/etc/ntp.conf
3.ntpdate用於客戶端的時間矯正,非NTP服務器可以不啟動NTP。
簡介:ntp全名 network time protocol 。NTP服務器可以為其他主機提供時間校對服務。
環境准備:兩台服務器,一台作為NTP服務器,另一台作為client端向服務器同步時間測試。
NTP服務器:156.0.26.6
client端:156.0.0.27
安裝與配置:我們從配置文件的角度來講解一下ntp的配置
# 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用來指定記錄本機與上層NTP server之間的頻率誤差。單位是百萬分之一秒。
# 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
#restrict用來管理權限控制。格式為 restrict [單個ip|網絡|default] parameter
parameter:
ignore:拒絕所有的ntp連接
nomodify:客戶端不能使用ntpc和ntpq這兩個程序來更改服務器的時間參數,單客戶端可以通過此主機來進行網絡校時。
noquery:客戶端不能使用ntpc和ntpq等命令來查詢時間服務器,等於不提供網絡校時服務。
notrap:不提供trap這個網絡時間登陸的功能
notrust:拒絕沒有認證的客戶端
#示例:restrict 156.0.26.7 nomodify
# 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 0.centos.pool.ntp.org iburst #以下四條為默認,注釋掉即可
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
#server:用來設置上層NTP服務器,說白了就是client向誰請求NTP時間同步。
#特別注意,在內網環境中由於無法連接到內網,所以沒有辦法向例如國家授時服務中心210.72.145.44同步時間
#只能講內網中的某台主機設置為server,用以向其他內網服務器提供NTP服務。
server 127.127.1.0 prefer
#以本機時間作為時間服務。內網中這個配置一定要加上,否則會導致NTP服務不可用
#prefer代表這台主機優先級最高。
#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
# 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 ##除了restrict來限制客戶端連接外,還可以通過秘鑰方式來給客戶端認證。
# 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
示例
#服務器端需要修改/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層級。 類似於DN,NTP是層級結構,有頂端的服務器,最多有15層。 為了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器。
when:幾秒之前通不過時間
poll:在過多長時間去同步時間。
reach:已經同步時間的次數
delay:網絡傳輸過程中的延遲,單位是10的-6次方秒。
offset:時間修正值,這是個最關鍵的值, 它告訴了我們本地機和服務器之間的時間差別.。單位是10的-3次方。
jitter:linux系統時間(軟件時間)與BIOS硬件時間的差異時間,單位是10的-6次方秒。在主機和NTP服務器同步時間歐,可以使用 hwclock -w將系統時間寫入BIOS.