centos6.5安裝配置NTP,集群各機器間時間同步


試驗環境

提君博客原創

>>提君博客原創  http://www.cnblogs.com/tijun/  <<

IP 主機名 角色 描述 同步方式
192.168.11.11 ltt1.bg.cn

NTPD服務

1、負責與外部公共NTPD服務同步標准時間

 2、作為內外網絡的NTPD服務

NTPD服務平滑同步
192.168.11.12 ltt2.bg.cn  內外NTP客戶端  內網設備與192.168.11.11同步時間 NTPD服務平滑同步
192.168.11.13 ltt3.bg.cn  內外NTP客戶端  內網設備與192.168.11.11同步時間 NTPD服務平滑同步
192.168.11.14 ltt4.bg.cn  內外NTP客戶端  內網設備與192.168.11.11同步時間 NTPD服務平滑同步
192.168.11.15 ltt5.bg.cn  內外NTP客戶端  內網設備與192.168.11.11同步時間 NTPD服務平滑同步

 

 

 

 

 

 

 

 

第一步,以上所有的機器都安裝ntp

[root@ltt1 ~]# yum install ntp

第二步,設置開機自啟動

[root@ltt1 conf]# chkconfig ntpd on
[root@ltt1 conf]# chkconfig --list ntpd
ntpd               0:off    1:off    2:on    3:on    4:on    5:on    6:off

第一、第二,以上兩步,在各機器上都要執行

提君博客原創

第三步,配置內網NTP-server(192.168.11.11)

此步是在你要設置成NTP服務的機器上配置,它將給內網所有的機器進行授時,即內網所有機器都是以它的時間為准。

我這里選擇的是192.168.11.11。

先使用ntpdate手動同步下內網中作為 NTPD 服務器的時間,免得本機與外部時間服務器時間差距太大,讓ntpd不能正常同步。即在 192.168.11.11 上執行 

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

然后要找到可以正常提供時間服務的外部主機,執行以下命令

[root@ltt1 ~]# ntpdate -q 1.cn.pool.ntp.org
server 51.15.41.135, stratum 3, offset -0.648578, delay 0.37547
server 193.228.143.12, stratum 2, offset -0.652907, delay 0.36064
server 202.118.1.130, stratum 2, offset -0.650668, delay 0.05075
server 120.25.115.20, stratum 2, offset -0.650029, delay 0.06900
29 Sep 17:51:21 ntpdate[1500]: step time server 202.118.1.130 offset -0.650668 sec

這里可以看到列出了四個外部主機,以及和本機時間的時間差異。

最后一行的末尾,time server 202.118.1.130 offset -0.650668 sec,這說明時間差異最小的一個外部主機為202.118.1.130

必要的信息已經獲取,現在正是開始配置NTP

[root@ltt1 ~]# vi /etc/ntp.conf 

這里列出的只是注釋掉內容和添加的內容,

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#設置對內網主機提供NTP服務。我的集群網關是192.168.11.2
restrict
192.168.11.2 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). #注釋掉centos默認的對時服務器,如果主機能訪問外網,最好做這一步
#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
51.15.41.135 server 193.228.143.12 server 202.118.1.130 perfer server 120.25.115.20 #修改上層服務器對本機的權限 restrict 51.15.41.135 nomodify notrap noquery restrict 193.228.143.12 nomodify notrap noquery restrict 202.118.1.130 nomodify notrap noquery restrict 120.25.115.20 nomodify notrap noquery #外部時間服務器不可用時,設置以本地時間作為時間服務 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10

 第四步,同步硬件時間

提君博客原創

在 Linux 下系統時間在開機的時候會和硬件時間同步(synchronization),之后也就各自獨立運行了,那么既然兩個時鍾獨自運行,那么時間久了必然就會產生誤差了,而 NTP 默認又只更新系統時間,因此我們需要設置硬件時鍾進行同步調整

[root@ltt1 ~]# vi /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
SYNC_HWCLOCK=yes
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"

第五步,啟動ntp服務,查看相關狀態,驗證

>>提君博客原創  http://www.cnblogs.com/tijun/  <<

[root@ltt1 ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

查看服務連接和監聽

[root@ltt1 ~]# netstat -tlunp | grep ntp
udp        0      0 192.168.11.11:123           0.0.0.0:*                               1327/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1327/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1327/ntpd           
udp        0      0 fe80::20c:29ff:fec5:88c5:123 :::*                                    1327/ntpd           
udp        0      0 ::1:123                     :::*                                    1327/ntpd           
udp        0      0 :::123                      :::*                                    1327/ntpd           

查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關系

[root@ltt1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp3.itcomplian 5.103.128.88     3 u   25   64   37  446.548  -700.47   1.006
 ntp1.flashdance 193.11.166.20    2 u   21   64   17  432.794  -705.08  12.690
*202.118.1.130   202.118.1.47     2 u   23   64   37   24.599  -653.77   1.470
 120.25.115.20   10.137.38.86     2 u   18   64   37   42.456  -651.64   1.038
 LOCAL(0)        .LOCL.          10 l   90   64   36    0.000    0.000   0.000

查看時間同步狀態

提君博客原創

[root@ltt1 ~]# ntpstat
synchronised to NTP server (202.118.1.130) at stratum 3 
   time correct to within 1658 ms
   polling server every 64 s

第六步,配置NTP-client,這是在除192.168.11.11的其他機器上進行

[root@ltt2 ~]# vi /etc/ntp.conf 

注意,這里的配置是和192.168.11.11有區別的

#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

#對時服務器,配置成ntp-server的機器
server
192.168.11.11 restrict 192.168.11.11 nomodify notrap noquery server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10

配置同步硬件時間

[root@ltt2 ~]# vi /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
SYNC_HWCLOCK=yes OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"

第七步,啟動ntp-client,手動更新時間,查看相關狀態,驗證,這里以ltt2為例

[root@ltt2 ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

手動向192.168.11.11更新一次時間

ntpdate -u 192.168.11.11

查看服務連接和監聽

[root@ltt2 ~]# netstat -tlunp | grep ntp
udp        0      0 192.168.11.12:123           0.0.0.0:*                               10776/ntpd          
udp        0      0 127.0.0.1:123               0.0.0.0:*                               10776/ntpd          
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               10776/ntpd          
udp        0      0 ::1:123                     :::*                                    10776/ntpd          
udp        0      0 fe80::20c:29ff:fef9:26cb:123 :::*                                    10776/ntpd          
udp        0      0 :::123                      :::*                                    10776/ntpd       

查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關系

[root@ltt2 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ltt1.bg.cn      202.118.1.130    3 u  880 1024  377    1.184    0.242   1.190
 LOCAL(0)        .LOCL.          10 l  14h   64    0    0.000    0.000   0.000

查看時間同步狀態

>>提君博客原創  http://www.cnblogs.com/tijun/  <<

[root@ltt2 ~]# ntpstat
synchronised to NTP server (192.168.11.11) at stratum 4 
   time correct to within 85 ms
   polling server every 1024 s

到此,機器的同步時間就配置完成了。

提君博客原創

 

附:ntpq -p指令,顯示的各項含義

提君博客原創

位置 標志 含義
符號 * 響應的NTP服務器和最精確的服務器
+ 響應這個查詢請求的NTP服務器
blank(空格) 沒有響應的NTP服務器
標題 remote 響應這個請求的NTP服務器的名稱
refid NTP服務器使用的更高一級服務器的名稱
st 正在響應請求的NTP服務器的級別
when 上一次成功請求之后到現在的秒數
poll 本地和遠程服務器多少時間進行一次同步,單位秒,在一開始運行NTP的時候這個poll值會比較小,服務器同步的頻率大,可以盡快調整到正確的時間范圍,之后poll值會逐漸增大,同步的頻率也就會相應減小
reach 用來測試能否和服務器連接,是一個八進制值,每成功連接一次它的值就會增加
delay 從本地機發送同步要求到ntp服務器的往返時間
offset 主機通過NTP時鍾同步與所同步時間源的時間偏移量,單位為毫秒,offset越接近於0,主機和ntp服務器的時間越接近
jitter 統計了在特定個連續的連接數里offset的分布情況。簡單地說這個數值的絕對值越小,主機的時間就越精確

 

 

 

 

 

 

 

 

 

 

 

 

提君博客原創

 

 

>>提君博客原創  http://www.cnblogs.com/tijun/  <<

 


免責聲明!

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



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