ubuntu安裝和使用NTP
安裝NTP
ntp線上安裝方式很簡單,只需要執行以下命令即可幫你安裝好NTP以及所依賴的包。
sudo apt-get install ntp
安裝完成後可以通過如下命令檢視NTP服務是否啟動:
service --status-all
或者
ps -aux | grep ntp
NTP的配置檔案/etc/ntp.conf
使用server命令設定上層NTP伺服器
設定方式:
server [address] [options...]
在server後面填寫伺服器地址(可以使IP或主機名),之後是命令引數主要包括autokey,brust,ibrust,key key
使用restrict命令管理許可權控制
設定方式:
restrict [address] mask [mask] [parameter]
其中parameter的引數主要有底下這些:
- ignore: 拒絕所有型別的NTP聯機;
- nomodify: 客戶端不能使用ntpc與ntpq這兩個程式來修改伺服器的時間引數,但客戶端仍可透過這個主機來進行網路校時;
- noquery: 客戶端不能使用ntpq,ntpc等指令來查詢時間伺服器,等於不提供NTP的網路校時;
- notrap: 不提供trap這個遠端事件登入(remote event logging)的功能;
- notrust: 拒絕沒有認證的客戶端;
如果你沒有在 parameter 的地方加上任何引數的話,這表示該 IP 或網段不受任何限制。
使用driftfile記錄時間差異
設定方式:
driftfile [可以被ntpd寫入的目錄與檔案]
因為預設的 NTP Server 本身的時間計算是依據 BIOS 的晶片震盪週期頻率來計算的,但是這個數值與上層 Time Server 不見得會一致啊!所以 NTP 這個 daemon (ntpd) 會自動的去計算我們自己主機的頻率與上層 Time server 的頻率,並且將兩個頻率的誤差記錄下來,記錄下來的檔案就是在 driftfile 後面接的完整檔名當中了!關於檔名你必須要知道:
- driftfile 後面接的檔案需要使用完整路徑檔名;
- 該檔案不能是連結檔;
- 該檔案需要設定成 ntpd 這個 daemon 可以寫入的許可權;
- 該檔案所記錄的數值單位為:百萬分之一秒 (ppm);
driftfile 後面接的檔案會被 ntpd 自動更新,所以他的許可權一定要能夠讓 ntpd 寫入才行。
使用statsdir和filegen開啟統計分析
設定方式:
statsdir directory_path
filegen name file filename [type type] [link | nolink] [enable | disable]
當開啟統計分析是會在directory_path目錄下產生filegen中所設定的統計檔案。
NTP的啟動與觀察
配置檔案
ntp即是伺服器,又是客戶端,只需要通過配置即可。根據上面的說明,我們最終可以取得這樣的配置檔案案內容。
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
# 時間差異檔案
driftfile /var/lib/ntp/ntp.drift
# 分析統計資訊
#statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# 上層ntp server.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst
# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com
# 不允許來自公網上ipv4和ipv6客戶端的訪問
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited
# 讓NTP Server和其自身保持同步,如果在/etc/ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務提供給ntp客戶端.
restrict 127.0.0.1
restrict ::1
# Needed for adding pool entries
restrict source notrap nomodify noquery
# 允許這個網段的對時請求.
restrict 192.168.123.0 mask 255.255.255.0 nomodify
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
#Changes recquired to use pps synchonisation as explained in documentation:
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918
#server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
#fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware
#server 127.127.22.1 # ATOM(PPS)
#fudge 127.127.22.1 flag3 1 # enable PPS API
NTP執行
配置好ntp.conf檔案後,通過如下命令重啟NTP服務:
server ntp restart
NTP觀察
重啟後通過如下命令觀察NTP的執行狀態:
watch ntpq -p
這個命令可以列出目前我們的 NTP 與相關的上層 NTP 的狀態,上頭的幾個欄位的意義為:
- remote: 它指的就是本地機器所連線的遠端NTP伺服器;
- refid: 它指的是給遠端伺服器提供時間同步的伺服器;
- st: 遠端伺服器的層級別(stratum). 由於NTP是層型結構,有頂端的伺服器,多層的Relay Server再到客戶端。所以伺服器從高到低級別可以設定為1-16. 為了減緩負荷和網路堵塞,原則上應該避免直接連線到級別為1的伺服器的;
- when: 幾秒鐘前曾經做過時間同步化更新的動作;
- poll: 本地機和遠端伺服器多少時間進行一次同步(單位為秒).
在一開始執行NTP的時候這個poll值會比較小,那樣和伺服器同步的頻率也就增加了,可以儘快調整到正確的時間範圍.之後poll值會逐漸增大,同步的頻率也就會相應減小; - reach: 已經向上層 NTP 伺服器要求更新的次數;
- delay: 網路傳輸過程當中延遲的時間,單位為 10^(-6) 秒;
- offset: 時間補償的結果,單位與 10^(-3) 秒;
- jitter: Linux 系統時間與 BIOS 硬體時間的差異時間, 單位為 10^(-6) 秒。簡單地說這個數值的絕對值越小我們和伺服器的時間就越精確;
- *: 它告訴我們遠端的伺服器已經被確認為我們的主NTP Server,我們系統的時間將由這臺機器所提供;
- +: 它將作為輔助的NTP Server和帶有號的伺服器一起為我們提供同步服務. 當號伺服器不可用時它就可以接管;
- -: 遠端伺服器被clustering algorithm認為是不合格的NTP Server;
- x: 遠端伺服器不可用;
要讓你的 NTP Server/Client 真的能運作,在上述的動作中得注意:
- 上述的 watch ntpq -p 的輸出結果中,你的 NTP 伺服器真的要能夠連結上層 NTP 才行! 否則你的客戶端將無法對你的
NTP 伺服器進行同步更新。 - 你的 NTP 伺服器時間不可與上層差異太多。
使用ntpdate更新時間
上面我們說到當本機與伺服器時間相差太遠,ntpd將無法同步成功。這是我們需要先使用ntpdate命令進行強制同步(在使用時需關掉ntpd程序)。
在終端執行如下命令:
sudo ntpdate server_ip
執行這個命令後(如果伺服器有效)將立即更新本地時間。而上面所使用的ntpd對時間的更新是漸進式的。