Linux的ntp常用配置整理



環境聲明:

OS CentOS7.2

原文鏈接:http://readshlinux.blog.51cto.com/9322509/1902432



ntp介紹:

       NTP是網絡時間協議(Network Time Protocol),它是用來同步網絡中各個計算機的時間的協議;

在NTP中,定義了時間按照服務器的等級傳播,按照離外部UTC源遠近將所有的服務器歸入不同的Stratum(層)中,例如把通過GPS(Global Positioning System,全球定位系統)取得發送標準時間的服務器叫Stratum-1的NTP服務器,而Stratum-2則從Stratum-1獲取時間,Stratum-3從Stratum-2獲取時間,以此類推,但Stratum層的總數限制在15以內。所有這些服務器在邏輯上形成階梯式的架構相互連接,而Stratum-1的時間服務器是整個系統的基礎;

架構示意圖,如下:


wKiom1i2pK6yCac9AAH6yMIQg48292.png-wh_50



那麼下一步就要進入正題了

1.允許防火牆通過ntp的數據

firewall-cmd --permanent --zone=public --add-port=123/udp
firewall-cmd --list-all



public (active)

target: default

icmp-block-inversion: no

interfaces: eth0

sources:

services: dhcpv6-client ssh

ports:  123/udp #我還開啓了其他的端口,這裏我就不顯示啦

protocols:

masquerade: no

forward-ports:

sourceports:

icmp-blocks:

rich rules:



2.安裝ntp服務

yum -y install ntp


3.配置ntp

ntp服務的主要配置文件爲“/etc/ntp.conf”,沒有修改過的配置文件如下所示

egrep -v "^#|^$" /etc/ntp.conf



driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

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

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor




下面解釋經常能用到的參數意思


driftfile /var/lib/ntp/drift #系統時間與BIOS時間的偏差記錄

-------------------------------------------------------------------------------------------

restrict default nomodify notrap nopeer noquery #控制相關權限

restrict 127.0.0.1

restrict ::1

-------------------------------------------------- #語法爲: restrict ip地址 子網掩碼 參數

參數有一下幾個:

ignore 拒絕所有類型的ntp連接;

nomodify 客戶端不能使用ntpc與ntpq這兩個程序來修改服務器的時間參數;但是客戶端任可以通過該服務端進行網絡校時

notrust 拒絕沒有認證的客戶端;

noquery 不提供客戶端的時間查詢(拒絕客戶端的所有查詢操作),用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器

nopeer 阻止主機嘗試與服務器對等,不允許欺詐性服務器控制時鐘

notrap 不提供trap這個遠端事件登錄(remote event logging)的功能

注意:如果沒有在restrict 參數的地方加上任何參數的話,將表示“ip、網段地址不受任何限制”

-------------------------------------------------------------------------------------------

server 0.centos.pool.ntp.org prefer #指定ntp服務器地址

server 1.centos.pool.ntp.org iburst

server 2.centos.pool.ntp.org iburst

server 3.centos.pool.ntp.org iburst

-------------------------------------------------- #語法爲:server ip地址 prefer/iburst

-------------------------------------------------- #prefer 表示:優先的ntp服務器地址



想要讓自己充當局域網中的ntp服務器,可以使用下面的配置

第一種配置: 允許任何ip地址都可以進行時間同步

“ restrict default nomodify notrap nopeer ” 修改成 “ restrict default nomodify ”

wKioL1jvU_PTNyPxAAAReETAYRU099.png-wh_50


第二種配置:僅僅允許一部分主機同步

“ restrict default nomodify notrap nopeer ” 表示默認拒絕所有ip的時間同步,之後增加一行

restrict 192.168.122.0 mask 255.255.255.0 nomodify

wKiom1jvVCrTbvvoAAAOI6upscc922.png-wh_50


然後重啓ntpd服務即可

wKioL1jvVD6wBhgUAAAW-IyqdXU026.png-wh_50



4.啓動ntp,並查看ntp端口

systemctl start ntpd.service


檢查ntp是否運行

pgrep ntpd 或者

ps -ef | grep ntpd 或者

systemctl status ntpd.service 或者

netstat -anptu | grep ntp



udp        0      0 10.0.0.143:123          0.0.0.0:*                           10466/ntpd

udp        0      0 127.0.0.1:123           0.0.0.0:*                           10466/ntpd

udp        0      0 0.0.0.0:123             0.0.0.0:*                           10466/ntpd

udp6       0      0 fe80::f816:3eff:feb:123 :::*                                10466/ntpd

udp6       0      0 ::1:123                 :::*                                10466/ntpd

udp6       0      0 :::123                  :::*                                10466/ntpd



這樣就表示ntp服務已經起來啦,但是它有沒有去連接ntp服務器,怎麼看?



5.查看ntp是否與上層ntp連通

ntpstat


synchronised to NTP server (85.199.214.101) at stratum 2

time correct to within 112 ms

polling server every 512 s


這個指令可以列出客戶機是否與ntp服務器進行連接,通過上面可以知道我們的機器處於地3層,因爲我們是從第二層去同步的時間

那麼咱們繼續,我們處於第三層,我們肯定想要知道第一層是誰



6.查看ntp與上層的狀態

ntpq -p

wKiom1i2pMnCmruyAAAqqPJksgo705.png-wh_50


下面解釋一下:


remote #ntp主機的ip,左邊‘ * ’表示當前引用

------- #‘ + ’表示 優先選擇,或者下一個候選; 順序爲 ' * +  - '

refid 參考上一層ntp主機地址

st 層, 第一層爲GPS衛星,不明白可以看最上邊的圖

when 多少秒之間做的時間同步;

poll 下一次更新時間在幾秒之後;

reach 向上層更新的次數

delay 網絡過程中的延遲時間

offset 時間補償

jitter linux系統時間與BIOS的時間差




查看ntpd進程狀態 按 Ctrl + c 停止

watch 'ntpq -p'



Every 2.0s: ntpq -p                                                                                                                                                  Wed Mar  1 17:50:18 2017

remote           refid st t when poll reach   delay   offset  jitter

==============================================================================

+marla.ludost.ne 131.188.3.220    2 u  872 1024  177  214.437   24.742   6.540

*85.199.214.101  .GPS.            1 u   11   64  377  216.950    0.123   5.755

+188-39-37-91.st .GPS.            1 u   15   64  355  231.056   -0.961  19.068

-biisoni.miuku.n 207.224.49.219   2 u  395 1024  367  177.958   32.780  39.127




7.ntpdate  同步時間

用法: ntpdate ntp服務器地址

ntpdate 0.asia.pool.ntp.org


這樣更新時間的話,server/client之間的時間不允許超過1000秒,超過,則不會去同步;

強制同步的話,可以使用 -u 選項,-u 會採用一個非特權端口來同步時間;如下所示:

ntpdate -u 0.asia.pool.ntp.org




8.將時間寫入BIOS

hwclock -r查看BIOS時間

hwclock -w 將系統時間寫入到BIOS




底下做一個總結:

ntpd、ntpdate

1.ntpd 時間同步服務器,同步時是平滑同步

2.ntpdate 同步時間時需要依賴ntp服務,並且是立即同步,不會管之前的時間是多少,會立馬同步到與ntp服務器一樣的時間;




參考文檔:

http://linux.vbird.org/linux_server/0440ntp.php

http://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章