首頁 系統教程 Linux Centos 7安裝設定NTP網路時間同步伺服器

Centos 7安裝設定NTP網路時間同步伺服器

Aug 05, 2024 pm 10:35 PM
linux linux教程 紅帽 linux系統 linux指令 linux認證 紅帽linux linux視頻

Centos 7安装配置NTP网络时间同步服务器

實驗環境:

OS:Linux Centos 7.4 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,每天24點更新同步時間):

# crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171;hwclock -w
登入後複製

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

以上是Centos 7安裝設定NTP網路時間同步伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1663
14
CakePHP 教程
1419
52
Laravel 教程
1313
25
PHP教程
1263
29
C# 教程
1237
24
Linux體系結構:揭示5個基本組件 Linux體系結構:揭示5個基本組件 Apr 20, 2025 am 12:04 AM

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

Linux的主要目的是什麼? Linux的主要目的是什麼? Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

notepad怎麼運行java代碼 notepad怎麼運行java代碼 Apr 16, 2025 pm 07:39 PM

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

sublime寫好代碼後如何運行 sublime寫好代碼後如何運行 Apr 16, 2025 am 08:51 AM

在 Sublime 中運行代碼的方法有六種:通過熱鍵、菜單、構建系統、命令行、設置默認構建系統和自定義構建命令,並可通過右鍵單擊項目/文件運行單個文件/項目,構建系統可用性取決於 Sublime Text 的安裝情況。

laravel安裝代碼 laravel安裝代碼 Apr 18, 2025 pm 12:30 PM

要安裝 Laravel,需依序進行以下步驟:安裝 Composer(適用於 macOS/Linux 和 Windows)安裝 Laravel 安裝器創建新項目啟動服務訪問應用程序(網址:http://127.0.0.1:8000)設置數據庫連接(如果需要)

git軟件安裝 git軟件安裝 Apr 17, 2025 am 11:57 AM

安裝 Git 軟件包括以下步驟:下載安裝包運行安裝包驗證安裝配置 Git安裝 Git Bash(僅限 Windows)

如何設置重要的 Git 配置全局屬性 如何設置重要的 Git 配置全局屬性 Apr 17, 2025 pm 12:21 PM

自定義開發環境的方法有很多種,但全局 Git 配置文件是最有可能用於自定義設置(例如用戶名、電子郵件、首選文本編輯器和遠程分支)的一種。以下是您需要了解的有關全局 Git 配置文件的關鍵事項。

See all articles