Centos 7安裝配置NTP網絡時間同步服務器


環境:

OS:Linux Centos 7.9 x86_64

1.查看當前服務器時區&列出時區並設置時區(如已是正確時區,請略過):

# timedatectl
# timedatectl list-timezones
# timedatectl set-timezone Asia/Shanghai

2.時間時區概念理解:

GMT、UTC、CST、DST

UTC:

整個地球分為二十四時區,每個時區都有自己的本地時間,在國際無線電通信場合,為了統一起見,使用一個統一的時間,稱為通用協調時(UTC:Universal Time Coordinated)。

GMT:

格林威治標准時間 (Greenwich Mean Time)指位於英國倫敦郊區的皇家格林尼治天文台的標准時間,因為本初子午線被定義在通過那里的經線(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

如已安裝請略過此步,否則請執行以下命令安裝:

# yum install ntpdate ntp -y

修改NTP配置:

注:綠色為原有我注釋掉的內容,紅色為新增代替之前注釋內容,其他默認。

# 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網段上的所有機器可以從這台機器上查詢和同步時間.
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.設置系統開機自啟動:

# systemctl enable ntpd
# systemctl enable ntpdate
# systemctl is-enabled ntpd

在ntpd服務啟動時,先使用ntpdate命令同步時間:

# ntpdate -u 1.cn.pool.ntp.org

原因之一:當服務端與客戶端之間的時間誤差過大時,此時修改時間可能對系統或應用帶來不可預知問題,NTP會停止時間同步。若檢查NTP啟動后時間未同步時,應考慮可能是時間誤差過大所致,此時需要先手動進行時間同步。

啟動NTP服務器:

# systemctl start ntpdate
# systemctl start ntpd

4.加入防火牆:

# firewall-cmd --permanent --add-service=ntp
# firewall-cmd --reload

5.查看ntp連接狀態如果沒有問題,將正確時間寫入硬件:

# ss -tlunp | grep ntp
# ntpq -p
# hwclock -w

5.1.系統時間與硬件時間

硬件時間:

RTC(Real-Time Clock)或CMOS時間,一般在主板上靠電池供電,服務器斷電后也會繼續運行。僅保存日期時間數值,無法保存時區和夏令時設置。

系統時間:

一般在服務器啟動時復制RTC時間,之后獨立運行,保存了時間、時區和夏令時設置。

6.客戶端:

以服務進程方式實時同步(需安裝NTP):

# vim /etc/ntp.conf
server 172.16.128.171

重要:修改任意節點服務器的NTP配置文件都需要重起ntpd服務:

# systemctl restart ntpd

以crontab任務計划同步時間(需安裝ntpdate,每隔更新同步時間):

# crontab -e
*/10 * * * * /usr/sbin/sntp -P no -r 172.16.128.171;hwclock -w

已部署完畢。這樣集群會自動定期進行服務的同步,如此以來集群的時間就保持一致了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM