新聞中心
NTP(Network Time Protocol)網(wǎng)絡時間協(xié)議基于UDP,用于網(wǎng)絡時間同步的協(xié)議,使網(wǎng)絡中的計算機時鐘同步到UTC,再配合各個時區(qū)的偏移調(diào)整就能實現(xiàn)精準同步對時功能。

實驗環(huán)境:
OS:Linux Centos 7.4 x86_64
1.查看當前服務器時區(qū)&列出時區(qū)并設置時區(qū)(如已是正確時區(qū),請略過):
# timedatectl
# timedatectl list-timezones
# timedatectl set-timezone Asia/Shanghai
2.時間時區(qū)概念理解:
GMT、UTC、CST、DST
UTC:
整個地球分為二十四時區(qū),每個時區(qū)都有自己的本地時間,在國際無線電通信場合,為了統(tǒng)一起見,使用一個統(tǒng)一的時間,稱為通用協(xié)調(diào)時(UTC:Universal Time Coordinated)。
GMT:
格林威治標準時間 (Greenwich Mean Time)指位于英國倫敦郊區(qū)的皇家格林尼治天文臺的標準時間,因為本初子午線被定義在通過那里的經(jīng)線(UTC與GMT時間基本相同)。
CST:
中國標準時間 (China Standard Time)
GMT + 8 = UTC + 8 = CST
DST:
夏令時(Daylight Saving Time) 指在夏天太陽升起的比較早時,將時間撥快一小時,以提早日光的使用(中國不使用)。
2.使用 NTP 公共時間服務器池(NTP Public Pool Time Servers)http://www.pool.ntp.org 同步你的服務器時間。
查看是否安裝:
# rpm -q ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64
如已安裝請略過此步,否則請執(zhí)行以下命令安裝:
# yum install ntpdate ntp -y
修改NTP配置:
注:綠色為原有我注釋掉的內(nèi)容,紅色為新增代替之前注釋內(nèi)容,其他默認。
# vim /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.
restrict 127.0.0.1
restrict ::1
#這一行的含義是授權172.16.128.0網(wǎng)段上的所有機器可以從這臺機器上查詢和同步時間.
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).
#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
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
#新增:允許上層時間服務器主動修改本機時間.
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
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
3.設置系統(tǒng)開機自啟動:
# systemctl enable ntpd
# systemctl enable ntpdate
# systemctl is-enabled ntpd
在ntpd服務啟動時,先使用ntpdate命令同步時間:
# ntpdate -u 1.cn.pool.ntp.org
原因之一:當服務端與客戶端之間的時間誤差過大時,此時修改時間可能對系統(tǒng)或應用帶來不可預知問題,NTP會停止時間同步。若檢查NTP啟動后時間未同步時,應考慮可能是時間誤差過大所致,此時需要先手動進行時間同步。
啟動NTP服務器:
# systemctl start ntpdate
# systemctl start ntpd
4.加入防火墻:
# firewall-cmd --permanent --add-service=ntp
# firewall-cmd --reload
5.查看ntp連接狀態(tài)如果沒有問題,將正確時間寫入硬件:
# ss -tlunp | grep ntp
# ntpq -p
# hwclock -w
5.1.系統(tǒng)時間與硬件時間
硬件時間:
RTC(Real-Time Clock)或CMOS時間,一般在主板上靠電池供電,服務器斷電后也會繼續(xù)運行。僅保存日期時間數(shù)值,無法保存時區(qū)和夏令時設置。
系統(tǒng)時間:
一般在服務器啟動時復制RTC時間,之后獨立運行,保存了時間、時區(qū)和夏令時設置。
6.客戶端:
以服務進程方式實時同步(需安裝NTP):
# vim /etc/ntp.conf
server 172.16.128.171
重要:修改任意節(jié)點服務器的NTP配置文件都需要重起ntpd服務:
# systemctl restart ntpd
以crontab任務計劃同步時間(需安裝ntpdate,每天24點更新同步時間):
# crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171;hwclock -w
已部署完畢。這樣集群會自動定期進行服務的同步,如此以來集群的時間就保持一致了。
網(wǎng)頁標題:Centos7部署NTP時間服務器
分享鏈接:http://m.fisionsoft.com.cn/article/djdcjhp.html


咨詢
建站咨詢
