文章目錄
- 一、時間時區概念
- 二、ntpd 與 ntpdate
- 三、部署 NTP 服務
- 1 服務軟體的安裝
- 2 服務的基本配置(/etc/ntp.conf)
- 3 設定系統開機自啟動
- 4 加入防火牆
- 5. 将正确時間寫入硬體
- 6. 用戶端使用配置
- 7. 檢視 ntp 同步狀态
- 四、NTP的編譯安裝
一、時間時區概念
-
UTC
整個地球分為二十四時區,每個時區都有自己的本地時間,在國際無線電通信場合,為了統一起見,使用一個統一的時間,稱為通用協調時。
-
GMT
格林威治标準時間指位于英國倫敦郊區的皇家格林尼治天文台的标準時間,因為本初子午線被定義在通過那裡的經線(UTC與GMT時間基本相同)。
-
CST
中國标準時間
CST = UTC+8 =
-
DST
夏令時指在夏天太陽升起的比較早時,将時間撥快一小時,以提早日光的使用,中國不使用。
# 檢視目前伺服器時區
timedatectl
# 列出時區并設定時區
二、ntpd 與 ntpdate
ntpd在實際同步時間時是一點點的校準過來時間的,最終把時間慢慢的校正對。而ntpdate不會考慮其他程式是否會陣痛,直接調整時間。一個是校準時間,一個是調整時間。
因為許多應用程式依賴連續的時鐘,而使用ntpdate這樣的時鐘躍變,有時候會導緻很嚴重的問題,如資料庫事務操作等。
不夠安全:ntpdate的設定依賴于ntp伺服器的安全性,攻擊者可以利用一些軟體設計上的缺陷,拿下ntp伺服器并令與其同步的伺服器執行某些消耗性的任務。
不夠精确:一旦ntp伺服器當機,跟随它的伺服器也就會無法同步時間。與此不同,ntpd不僅能夠校準計算機的時間,而且能夠校準計算機的時鐘。
不夠優雅:由于ntpdate是急變,而不是使時間變快或變慢,依賴時序的程式會出錯。例如,如果ntpdate發現你的時間快了,則可能會經曆兩個相同的時刻,對某些應用而言,這是緻命的。理想的做法是使用ntpd來校準時鐘,而不是調整計算機時鐘上的時間。
三、部署 NTP 服務
使用NTP公共時間伺服器池同步你的伺服器時間,部署完成之後,這樣叢集會自動定期進行服務的同步,如此一來叢集的時間就保持一緻了。
1 服務軟體的安裝
# 檢視是否安裝
rpm -q ntp
# 如果沒有安裝過的話,可以執行此指令安裝
yum install
2 服務的基本配置(/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
# 新增:日志目錄
logfile /var/log/ntpd.log
# 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.
# 還要確定localhost足夠權限,這個常用的IP位址用來指Linux伺服器本身
restrict 127.0.0.1
restrict ::1
# 這一行的含義是授權172.16.128.0網段上的所有機器可以從這台機器上查詢和同步時間
restrict 172.16.128.0 mask 255.255.255.0 nomodify notrap
# 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).
# 删減:注釋掉NTP服務原有的配置
#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 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
# 新增:當外部時間不可用時可以使用本地時間
server 172.16.128.171 iburst
fudge 127.0.0.1 stratum 10
# 新增:允許上層時間伺服器主動修改本機時間
# 其中restrict語句控制允許哪些網絡查詢和同步時間
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery
......
阿裡的ntp時鐘源
ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
3 設定系統開機自啟動
# 預設為CentOS7的配置,CentOS6中需要使用chkconfig指令
systemctl enable ntpd
systemctl enable
4 加入防火牆
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload
5. 将正确時間寫入硬體
ss -tlunp | grep
6. 用戶端使用配置
# (1) 以服務程序方式實時同步
# 編輯用戶端的配置檔案(/etc/ntp.conf),添加如下内容
server 172.16.128.171
# (2) 重新開機服務
# 修改任意節點伺服器的NTP配置檔案都需要重新開機ntpd服務
systemctl restart ntpd
# (3) 設定定時任務進行時間校對
# 需安裝ntpdate,每天24點更新同步時間
crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171;
7. 檢視 ntp 同步狀态
# 使用如下指令檢視節點同步狀态
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
218.189.210.3 118.143.17.82 2 u 7 64 1 101.974 -33.967 0.000
209.58.185.100 .INIT. 16 u - 64 0 0.000 0.000 0.000
103-226-213-30- .INIT. 16 u - 64 0 0.000 0.000 0.000
-
remote:即NTP主機的IP或主機名稱。
注意最左邊的符号,如果由“+”則代表目前正在使用中的上層NTP,
如果是“*”則表示也有連上線,不過是作為次要聯機的NTP主機。
- refid:參考的上一層NTP主機的位址
- st:即stratum階層
- when:幾秒前曾做過時間同步更新的操作
- poll:下次更新在幾秒之後
- reach:已經向上層NTP伺服器請求更新的次數
- delay:網絡傳輸過程中延遲的時間
- offset:時間補償的結果
- jitter:Linux系統時間與BIOS硬體時間的差異時間
其中,when和reach是觀察時間是否同步上的标志字段。
[root@Server1 ~]# ntpdate -q ntp.aliyun.com
server 203.107.6.88, stratum 2, offset 0.716122, delay 0.08424
9 Jun 11:31:45 ntpdate[156654]: step time server 203.107.6.88 offset 0.716122 sec
[root@Server1 ~]# ntpdate -q ntp1.aliyun.com
server 120.25.115.20, stratum 2, offset 0.712180, delay 0.06381
9 Jun 11:32:29 ntpdate[157925]: step time server 120.25.115.20 offset 0.712180
四、NTP的編譯安裝
- Centos7編譯安裝ntp-4.2.8p11
- CentOS7編譯安裝ntp