Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

企業內網實戰:CentOS 7.2 搭建 NTP 服務並完成全網時間同步

網絡時間協議 NTP(Network Time Protocol)是互聯網中用於時間同步的標準協議,目的是把計算機時間對齊到統一標準,目前採用的是世界協調時 UTC。NTP 的主要開發者是美國特拉華大學的 David L. Mills 教授。

對個人來說,NTP 的用處很直觀:本機時間不準了,連上網同步一下就好。但對企業而言,成百上千台不能直連互聯網的機器,時間不統一會直接影響業務判斷(日誌排序、認證令牌、定時任務都可能出錯),一台一台手改根本不現實。這時候自建一台內網 NTP 服務器就成了性價比最高的選擇。

典型拓撲是:一台上層能連外網的服務器作為內網時間源,其餘主機全部指向它。

企業內網實戰:CentOS 7.2 搭建 NTP 服務並完成全網時間同步

出發前先配好可用的 yum 源,並確認 NTP 服務器能連外網。DNS 不對的話先補上:

# vim /etc/resolv.conf
nameserver 192.168.8.251      # 默認網關可在 Windows 上用 ipconfig 查到

一、配置內網 NTP 服務器(192.168.8.100)

企業內網實戰:CentOS 7.2 搭建 NTP 服務並完成全網時間同步

1. 安裝並設置開機自啟:

# yum install -y ntp
# chkconfig ntpd on
# chkconfig --list ntpd
ntpd      0:off  1:off  2:on  3:on  4:on  5:on  6:off

2. 編輯 /etc/ntp.conf:

# 允許內網網段 192.168.0.0 的機器前來同步
restrict 192.168.0.0 mask 255.255.0.0 nomodify

# 上層時間服務器
server ntp.api.bz prefer
server 210.72.145.44       # 中國國家授時中心
server 202.112.10.36       # 1.cn.pool.ntp.org
server 59.124.196.83       # 0.asia.pool.ntp.org

# 允許上層主動修改本機時間
restrict ntp.api.bz      nomodify notrap noquery
restrict 210.72.145.44   nomodify notrap noquery
restrict 202.112.10.36   nomodify notrap noquery
restrict 59.124.196.83   nomodify notrap noquery

# 外部服務器不可用時,以本地時間兜底
server 127.127.1.0
fudge  127.127.1.0 stratum 10

企業內網實戰:CentOS 7.2 搭建 NTP 服務並完成全網時間同步

想快速確認哪些配置真正生效,可以過濾掉空行和注釋:

# cat /etc/ntp.conf | grep "^[^#]"
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.0.0 mask 255.255.0.0 nomodify
server ntp.api.bz prefer
server 210.72.145.44
server 202.112.10.36
server 59.124.196.83
server 127.127.1.0
fudge 127.127.1.0 stratum 10
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

國內常用時間服務器參考:210.72.145.44(國家授時中心)、ntp.sjtu.edu.cn(202.120.2.101,上海交通大學)、s1a.time.edu.cn(北京郵電大學)、s1b/s1e/s2a/s2b/s2c.time.edu.cn(清華大學)、s1c.time.edu.cn(北京大學)、s1d.time.edu.cn(東南大學)、s2d.time.edu.cn(西南地區網絡中心)、s2e.time.edu.cn(西北地區網絡中心)、s2f.time.edu.cn(東北地區網絡中心)、s2g.time.edu.cn(華東南地區網絡中心)、s2h.time.edu.cn(四川大學)、s2j.time.edu.cn(大連理工大學)、s2k.time.edu.cn(CERNET 桂林主節點)、s2m.time.edu.cn(北京大學)。完整列表見 https://www.pool.ntp.org/zone/cn 。

3. 啟動前先手動校一次時間,避免本機與上層偏差過大導致 ntpd 無法收斂:

# ntpdate -u ntp.api.bz
13 Apr 14:45:05 ntpdate[11464]: step time server 61.153.197.226 offset 28824.742403 sec

4. 啟動服務並確認端口。NTP 監聽 UDP 123:

# service ntpd start
# netstat -tnulp | grep ntp
udp   0 0 192.168.8.102:123 0.0.0.0:*   11484/ntpd
udp   0 0 127.0.0.1:123     0.0.0.0:*   11484/ntpd
udp   0 0 0.0.0.0:123       0.0.0.0:*   11484/ntpd

企業內網實戰:CentOS 7.2 搭建 NTP 服務並完成全網時間同步

服務起來後需要等幾分鐘到十幾分鐘才會真正完成同步。此後客戶端即可對時:

# ntpdate 192.168.8.100
11 Aug 11:38:30 ntpdate[7619]: adjust time server 10.17.1.60 offset 0.000178 sec

如果重啟後立刻測試,大概率會看到:

# ntpdate 192.168.8.100
11 Aug 11:05:28 ntpdate[7326]: no server suitable for synchronization found

排查細節可以用 ntpdate -d 192.168.8.100。查看與各上層服務器的連接狀態用 ntpq -p,它會同時顯示客戶端與每個服務器的關係:

# ntpq -p
     remote          refid   st t when poll reach  delay  offset jitter
==============================================================================
*61.153.197.226   216.218.192.202  2 u  37   64    1  47.274  10.405  0.000
 210.72.145.44    .INIT.          16 u   -   64    0   0.000   0.000  0.000
 LOCAL(0)         .LOCL.          10 l   -   64    0   0.000   0.000  0.000
 ntp2.aliyun.com  10.137.38.86     2 u  22   64    1  35.176   4.490  1.640

用 ntpstat 查看同步狀態,通常需要 5-10 分鐘才能成功連接:

# ntpstat
unsynchronised
time server re-starting
polling server every 64 s

# 同步完成後
# ntpstat
synchronised to NTP server (110.75.186.248) at stratum 3
   time correct to within 225 ms
   polling server every 64 s

二、關鍵配置文件的含義

/etc/ntp.conf 是 NTP 服務的主配置。

權限控制主要靠 restrict,語法為 restrict IP地址 mask 子網掩碼 參數,IP 也可以寫 default 代表所有來源。常用參數:ignore(關閉所有 NTP 響應)、nomodify(客戶端不能改服務端時間參數,但仍可校時)、notrust(未通過認證的來源不予信任)、noquery(不提供時間查詢)。

上層服務器用 server 指定:server IP或域名 [prefer],加上 prefer 表示優先以該主機時間為準。

driftfile 用來記錄與上層服務器通信所花費的時間,語法為 driftfile 文件名。注意必須使用完整路徑、不能是鏈接文件,且權限要允許 ntpd 進程寫入。

其他相關文件:/usr/share/zoneinfo/ 存放各時區設定文件;/etc/localtime 是本機時間設定;/etc/sysconfig/clock 是本機時區設定。

三、配置內網 NTP 客戶端

客戶端配置簡單,而且所有機器完全一致。先安裝並設為自啟(與服務端相同),然後只改 /etc/ntp.conf:

# yum install -y ntp
# chkconfig ntpd on
# vim /etc/ntp.conf

# 指向內網時間服務器
server 192.168.8.100
restrict 192.168.8.100 nomodify notrap noquery
server 127.127.1.0        # local clock
fudge  127.127.1.0 stratum 10

正式請求前先手動對一次:

# ntpdate -u 192.168.8.100
13 Apr 14:49:51 ntpdate[4114]: no server suitable for synchronization found

出現同步失敗通常是服務端 ntpd 還沒完全起來,等幾分鐘即可。確認無誤後重啟服務並查看狀態:

# service ntpd restart
# ntpq -p
# ntpstat

內網延遲低,一般很快就能同步上。第一台客戶端驗證通過後,直接用 scp 把配置分發下去:

# ntpdate -u 192.168.8.100
# scp 192.168.8.xxx:/etc/ntp.conf /etc/ntp.conf
# service ntpd start

四、常見錯誤排查

錯誤一:Server dropped: Strata too high。用 ntpdate -d 查看會發現 stratum 顯示為 16,而正常範圍是 0-15。這說明 NTP 服務器自身還沒和它的上層同步完成。配置中 server 127.127.1.0 加 fudge 127.127.1.0 stratum 8 的作用,就是在所有上層都不可用時用本地時間兜底。服務端重啟後,自身同步需要一段時間(通常約 5 分鐘),此期間客戶端對時必然報這個錯。判斷是否完成的辦法是在服務端執行:

# watch ntpq -p

注意其中的 LOCAL 行就是與自身同步的源;同時盯住 reach 列,它會從 0 遞增,每次變化間隔等於 poll 值(64 秒),到 17 大約需要 64×5=320 秒。

錯誤二:Server dropped: no data。客戶端 ntpdate -d 會看到反覆 transmit 之後提示 no data。常見原因有兩個:

一是 ntp 版本與 notrust 的行為變化。4.1 及更早版本中 notrust 表示「不信任該主機/子網的時間」,而 4.2 之後變為「忽略所有未經密碼學認證的 NTP 報文」,會強制要求遠端服務器認證自己,於是就出現了這個錯誤。用 ntpq -c version 確認版本,解決辦法是把 restrict 中的 notrust 去掉。

二是服務端防火牆擋了 UDP 123 端口。可以先停掉防火牆驗證:

# systemctl stop firewalld.service
# systemctl disable firewalld.service     # 禁止隨機啟動

若停掉後客戶端能正常同步,說明就是防火牆規則問題,需要針對 123/UDP 放行而不是長期關閉防火牆。

整套方案落地後,內網所有主機的時間都會收斂到同一個源上,後續做日誌審計、集群認證、定時任務排障時,會省下大量扯皮時間。

Copyright © Linux教程網 All Rights Reserved