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

實(shí)驗(yàn)環(huán)境:
OS:Linux Centos 7.4 x86_64
1.查看當(dāng)前服務(wù)器時(shí)區(qū)&列出時(shí)區(qū)并設(shè)置時(shí)區(qū)(如已是正確時(shí)區(qū),請(qǐng)略過(guò)):
# timedatectl
# timedatectl list-timezones
# timedatectl set-timezone Asia/Shanghai
2.時(shí)間時(shí)區(qū)概念理解:
GMT、UTC、CST、DST
UTC:
整個(gè)地球分為二十四時(shí)區(qū),每個(gè)時(shí)區(qū)都有自己的本地時(shí)間,在國(guó)際無(wú)線電通信場(chǎng)合,為了統(tǒng)一起見,使用一個(gè)統(tǒng)一的時(shí)間,稱為通用協(xié)調(diào)時(shí)(UTC:Universal Time Coordinated)。
GMT:
格林威治標(biāo)準(zhǔn)時(shí)間 (Greenwich Mean Time)指位于英國(guó)倫敦郊區(qū)的皇家格林尼治天文臺(tái)的標(biāo)準(zhǔn)時(shí)間,因?yàn)楸境踝游缇€被定義在通過(guò)那里的經(jīng)線(UTC與GMT時(shí)間基本相同)。
CST:
中國(guó)標(biāo)準(zhǔn)時(shí)間 (China Standard Time)
GMT + 8 = UTC + 8 = CST
DST:
夏令時(shí)(Daylight Saving Time) 指在夏天太陽(yáng)升起的比較早時(shí),將時(shí)間撥快一小時(shí),以提早日光的使用(中國(guó)不使用)。
2.使用 NTP 公共時(shí)間服務(wù)器池(NTP Public Pool Time Servers)http://www.pool.ntp.org 同步你的服務(wù)器時(shí)間。
查看是否安裝:
# rpm -q ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64
如已安裝請(qǐng)略過(guò)此步,否則請(qǐng)執(zhí)行以下命令安裝:
# yum install ntpdate ntp -y
修改NTP配置:
注:綠色為原有我注釋掉的內(nèi)容,紅色為新增代替之前注釋內(nèi)容,其他默認(rèn)。
# 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
#這一行的含義是授權(quán)172.16.128.0網(wǎng)段上的所有機(jī)器可以從這臺(tái)機(jī)器上查詢和同步時(shí)間.
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
#新增:時(shí)間服務(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)外部時(shí)間不可用時(shí),使用本地時(shí)間.
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
#新增:允許上層時(shí)間服務(wù)器主動(dòng)修改本機(jī)時(shí)間.
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.設(shè)置系統(tǒng)開機(jī)自啟動(dòng):
# systemctl enable ntpd
# systemctl enable ntpdate
# systemctl is-enabled ntpd
在ntpd服務(wù)啟動(dòng)時(shí),先使用ntpdate命令同步時(shí)間:
# ntpdate -u 1.cn.pool.ntp.org
原因之一:當(dāng)服務(wù)端與客戶端之間的時(shí)間誤差過(guò)大時(shí),此時(shí)修改時(shí)間可能對(duì)系統(tǒng)或應(yīng)用帶來(lái)不可預(yù)知問(wèn)題,NTP會(huì)停止時(shí)間同步。若檢查NTP啟動(dòng)后時(shí)間未同步時(shí),應(yīng)考慮可能是時(shí)間誤差過(guò)大所致,此時(shí)需要先手動(dòng)進(jìn)行時(shí)間同步。
啟動(dòng)NTP服務(wù)器:
# systemctl start ntpdate
# systemctl start ntpd
4.加入防火墻:
# firewall-cmd --permanent --add-service=ntp
# firewall-cmd --reload
5.查看ntp連接狀態(tài)如果沒(méi)有問(wèn)題,將正確時(shí)間寫入硬件:
# ss -tlunp | grep ntp
# ntpq -p
# hwclock -w
5.1.系統(tǒng)時(shí)間與硬件時(shí)間
硬件時(shí)間:
RTC(Real-Time Clock)或CMOS時(shí)間,一般在主板上靠電池供電,服務(wù)器斷電后也會(huì)繼續(xù)運(yùn)行。僅保存日期時(shí)間數(shù)值,無(wú)法保存時(shí)區(qū)和夏令時(shí)設(shè)置。
系統(tǒng)時(shí)間:
一般在服務(wù)器啟動(dòng)時(shí)復(fù)制RTC時(shí)間,之后獨(dú)立運(yùn)行,保存了時(shí)間、時(shí)區(qū)和夏令時(shí)設(shè)置。
6.客戶端:
以服務(wù)進(jìn)程方式實(shí)時(shí)同步(需安裝NTP):
# vim /etc/ntp.conf
server 172.16.128.171
重要:修改任意節(jié)點(diǎn)服務(wù)器的NTP配置文件都需要重起ntpd服務(wù):
# systemctl restart ntpd
以crontab任務(wù)計(jì)劃同步時(shí)間(需安裝ntpdate,每天24點(diǎn)更新同步時(shí)間):
# crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171;hwclock -w
已部署完畢。這樣集群會(huì)自動(dòng)定期進(jìn)行服務(wù)的同步,如此以來(lái)集群的時(shí)間就保持一致了。
網(wǎng)站題目:Centos7部署NTP時(shí)間服務(wù)器
分享鏈接:http://www.5511xx.com/article/djdcjhp.html


咨詢
建站咨詢
