如何在Linux上安裝和配置NTP客戶端和伺服器?
本文將幫助您瞭解如何在RHEL/Cent OS Linux上配置NTP(網路時間協議)伺服器和客戶端,以藉助NTP伺服器管理系統時鐘。
NTP(網路時間協議)
NTP用於將計算機的機器時間與另一個時間源同步。在RHEL/CentOS Linux中,我們可以使用NTP或OpenNTPD伺服器,它提供用於時間同步的客戶端和伺服器軟體。
安裝NTP軟體
NTP軟體包包含實用程式和守護程序,這些實用程式和守護程序將透過NTP協議將機器時間與協調世界時 (UTC) 同步。NTP軟體包包含ntpdate(使用網路從遠端機器更新日期和時間)和ntpd(一個調整系統時間的守護程序)。
# yum install ntp -y Loaded plugins: fastestmirror, security Setting up Install Process Loading mirror speeds from cached hostfile * base: ftp.iitm.ac.in * epel: mirror01.idc.hinet.net * extras: ftp.iitm.ac.in * updates: ftp.iitm.ac.in Resolving Dependencies --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-5.el6.ntosce will be updated ---> Package ntp.x86_64 0:4.2.6p5-5.el6.centos.4 will be an update --> Processing Dependency: ntpdate = 4.2.6p5-5.el6.centos.4 for package: ntp-4.2.6p5-5.el6.c entos.4.x86_64 --> Running transaction check ---> Package ntpdate.x86_64 0:4.2.6p5-5.el6.centos will be updated ---> Package ntpdate.x86_64 0:4.2.6p5-5.el6.centos.4 will be an update --> Finished Dependency Resolution Dependencies Resolved ============================================================================================ Package Arch Version Repository Size ============================================================================================ Updating: ntp x86_64 4.2.6p5-5.el6.centos.4 updates 595 k Updating for dependencies: ntpdate x86_64 4.2.6p5-5.el6.centos.4 updates 77 k Transaction Summary ============================================================================================ Upgrade 2 Package(s) Total download size: 672 k Downloading Packages: (1/2): ntp-4.2.6p5-5.el6.centos.4.x86_64.rpm | 595 kB 00:00 (2/2): ntpdate-4.2.6p5-5.el6.centos.4.x86_64.rpm | 77 kB 00:00 -------------------------------------------------------------------------------------------- Total 261 kB/s | 672 kB 00:02 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Updating : ntpdate-4.2.6p5-5.el6.centos.4.x86_64 1/4 Updating : ntp-4.2.6p5-5.el6.centos.4.x86_64 2/4 Cleanup : ntp-4.2.6p5-5.el6.centos.x86_64 3/4 Cleanup : ntpdate-4.2.6p5-5.el6.centos.x86_64 4/4 Verifying : ntp-4.2.6p5-5.el6.centos.4.x86_64 1/4 Verifying : ntpdate-4.2.6p5-5.el6.centos.4.x86_64 2/4 Verifying : ntpdate-4.2.6p5-5.el6.centos.x86_64 3/4 Verifying : ntp-4.2.6p5-5.el6.centos.x86_64 4/4 Updated: ntp.x86_64 0:4.2.6p5-5.el6.centos.4 Dependency Updated: ntpdate.x86_64 0:4.2.6p5-5.el6.centos.4 Complete!
配置NTP伺服器
如果我們的環境中有很多伺服器和桌面機器,那麼我們應該擁有NTP伺服器,以便所有伺服器都可以聯絡並更新由ISP或位於ntp.org的公共時間提供的NTP伺服器。然後,伺服器允許網路中的其他機器請求時間日期。
演示環境詳情
192.167.87.150 Local NTPD server 81.6.42.224 ISP NTP server 192.168.87.0/24 NTP clients network
開啟配置檔案並新增以下行:
restrict default ignore
以上將拒絕任何機器、伺服器或客戶端的所有訪問。我們需要向設定新增特定的授權策略。
restrict 81.6.42.224 mask 255.255.255.245 nomodify notrap noquery server 81.6.42.224
配置NTP客戶端以訪問本地
NTP伺服器現在我們需要允許NTP客戶端訪問伺服器,對於我們的網路,我們允許192.168.87.0/24網路同步位於自身環境中的網路時間。
192.168.87.150
開啟/etc/ntp.conf檔案並新增以下所有行,使其看起來像本地NTP伺服器。
# Hosts on local network are less restricted. restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
儲存配置檔案並重啟NTP服務。
# service ntpd start
使用我們的本地伺服器配置NTP客戶端
開啟/etc/ntp.conf並編輯該檔案。
# vi /etc/ntp.conf
並確保存在以下行:
# Server ntp.server.com
其中ntp.server.com是站點的主機名/IP地址。如果您的NTP伺服器位於192.168.87.156,請輸入server 192.168.87.156;如果我們有公共NTP伺服器,則提供伺服器IP地址。
配置Cron以更新伺服器
執行以下命令以更新cron中的ntpd檔案,它將指示crond執行ntpd並設定時鐘,並使用使用者名稱ntp進行時鐘更改。
# echo ’30 * * * * root /usr/sbin/ntpd -q -u ntp:ntp’ > /etc/cron.d/ntpd
在成功完成上述步驟的配置後,我們可以配置獨立的NTP客戶端,也可以在本地環境中配置NTP伺服器到NTP伺服器,以服務所有本地客戶端和伺服器。