服務(wù)部署-NTP時間同步服務(wù)
1. 時間時區(qū)概念
UTC
整個地球分為二十四時區(qū),每個時區(qū)都有自己的本地時間,在國際無線電通信場合,為了統(tǒng)一起見,使用一個統(tǒng)一的時間,稱為通用協(xié)調(diào)時。
GMT
格林威治標(biāo)準(zhǔn)時間指位于英國倫敦郊區(qū)的皇家格林尼治天文臺的標(biāo)準(zhǔn)時間,因為本初子午線被定義在通過那里的經(jīng)線(UTC與GMT時間基本相同)。
CST
中國標(biāo)準(zhǔn)時間
CST = UTC+8 = GMT+8
DST
夏令時指在夏天太陽升起的比較早時,將時間撥快一小時,以提早日光的使用,中國不使用。
# 查看當(dāng)前服務(wù)器時區(qū)
timedatectl
# 列出時區(qū)并設(shè)置時區(qū)
timedatectl list-timezones
timedatectl set-timezone Asia/Shanghai
2. ntpd 與 ntpdate
ntpd在實際同步時間時是一點(diǎn)點(diǎn)的校準(zhǔn)過來時間的,最終把時間慢慢的校正對。而ntpdate不會考慮其他程序是否會陣痛,直接調(diào)整時間。一個是校準(zhǔn)時間,一個是調(diào)整時間。
因為許多應(yīng)用程序依賴連續(xù)的時鐘,而使用ntpdate這樣的時鐘躍變,有時候會導(dǎo)致很嚴(yán)重的問題,如數(shù)據(jù)庫事務(wù)操作等。
不夠安全:ntpdate的設(shè)置依賴于ntp服務(wù)器的安全性,攻擊者可以利用一些軟件設(shè)計上的缺陷,拿下ntp服務(wù)器并令與其同步的服務(wù)器執(zhí)行某些消耗性的任務(wù)。
不夠精確:一旦ntp服務(wù)器宕機(jī),跟隨它的服務(wù)器也就會無法同步時間。與此不同,ntpd不僅能夠校準(zhǔn)計算機(jī)的時間,而且能夠校準(zhǔn)計算機(jī)的時鐘。
不夠優(yōu)雅:由于ntpdate是急變,而不是使時間變快或變慢,依賴時序的程序會出錯。例如,如果ntpdate發(fā)現(xiàn)你的時間快了,則可能會經(jīng)歷兩個相同的時刻,對某些應(yīng)用而言,這是致命的。理想的做法是使用ntpd來校準(zhǔn)時鐘,而不是調(diào)整計算機(jī)時鐘上的時間。
3. 部署 NTP 服務(wù)
使用NTP公共時間服務(wù)器池同步你的服務(wù)器時間,部署完成之后,這樣集群會自動定期進(jìn)行服務(wù)的同步,如此以來集群的時間就保持一致了。
(1) 服務(wù)軟件的安裝
# 查看是否安裝
rpm -q ntp
# 如果沒有安裝過的話,可以執(zhí)行此命令安裝
yum install ntpdate ntp -y
(2) 服務(wù)的基本配置(/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足夠權(quán)限,這個常用的IP地址用來指Linux服務(wù)器本身
restrict 127.0.0.1
restrict ::1
# 這一行的含義是授權(quán)172.16.128.0網(wǎng)段上的所有機(jī)器可以從這臺機(jī)器上查詢和同步時間
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服務(wù)原有的配置
#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
# 新增:時間服務(wù)器列表
# 指定我們需要同步的時間服務(wù)器地址,可以有多個
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
# 新增:當(dāng)外部時間不可用時可以使用本地時間
server 172.16.128.171 iburst
fudge 127.0.0.1 stratum 10
# 新增:允許上層時間服務(wù)器主動修改本機(jī)時間
# 其中restrict語句控制允許哪些網(wǎng)絡(luò)查詢和同步時間
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
......
(3) 設(shè)置系統(tǒng)開機(jī)自啟動
# 默認(rèn)為CentOS7的配置,CentOS6中需要使用chkconfig命令
systemctl enable ntpd
systemctl enable ntpdate
systemctl is-enabled ntpd
# 默認(rèn)為CentOS7的配置,CentOS6中需要使用chkconfig命令
systemctl enable ntpd
systemctl enable ntpdate
systemctl is-enabled ntpd
(4) 加入防火墻
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload
(5) 將正確時間寫入硬件
ss -tlunp | grep ntp
ntpq -p
hwclock -w
(6) 客戶端使用配置
# (1) 以服務(wù)進(jìn)程方式實時同步
# 編輯客戶端的配置文件(/etc/ntp.conf),添加如下內(nèi)容
server 172.16.128.171
# (2) 重啟服務(wù)
# 修改任意節(jié)點(diǎn)服務(wù)器的NTP配置文件都需要重啟ntpd服務(wù)
systemctl restart ntpd
# (3) 設(shè)置定時任務(wù)進(jìn)行時間校對
# 需安裝ntpdate,每天24點(diǎn)更新同步時間
crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171; hwclock -w
(7) 查看 ntp 同步狀態(tài)
# 使用如下命令查看節(jié)點(diǎn)同步狀態(tài)
[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主機(jī)的IP或主機(jī)名稱。
注意最左邊的符號,如果由“+”則代表目前正在作用鐘的上層NTP,
如果是“*”則表示也有連上線,不過是作為次要聯(lián)機(jī)的NTP主機(jī)。
refid:參考的上一層NTP主機(jī)的地址
st:即stratum階層
when:幾秒前曾做過時間同步更新的操作
poll:下次更新在幾秒之后
reach:已經(jīng)向上層NTP服務(wù)器要求更新的次數(shù)
delay:網(wǎng)絡(luò)傳輸過程中延遲的時間
offset:時間補(bǔ)償?shù)慕Y(jié)果
jitter:Linux系統(tǒng)時間與BIOS硬件時間的差異時間
# 查詢你的ntp服務(wù)器同步信息
[root@localhost ~]# ntpdate -q 0.hk.pool.ntp.org
server 203.95.213.129, stratum 2, offset -0.020632, delay 0.06477
server 209.58.185.100, stratum 2, offset -0.011884, delay 0.06216
server 218.189.210.4, stratum 0, offset 0.000000, delay 0.00000
server 218.189.210.3, stratum 2, offset -0.036728, delay 0.11096
6 Apr 12:51:43 ntpdate[10190]: adjust time server 209.58.185.100 offset -0.011884 sec
作者: Escape
鏈接: https://escapelife.github.io/posts/969a8066.html
整理:微信公眾號【開源Linux】
關(guān)注「開源Linux」加星標(biāo),提升IT技能


