1. 程式人生 > >NTP 時間同步

NTP 時間同步

部署NTP伺服器進行時間同步

NTP服務端:linl_S    IP:10.0.0.15

NTP客戶端:lin_C    IP:10.0.0.16

NTP服務概述

1、原理

NTP(Network TimeProtocol,網路時間協議)是用來使計算機時間同步的一種協議。它可以使計算機對其伺服器或時鐘源做同步化,它可以提供高精準度的時間校正(LAN上與標準間差小於1毫秒,WAN上幾十毫秒),切可介由加密確認的方式來防止惡意的協議攻擊。

 

模式:C/S模式

執行模式:

 

2、埠(123)

1 [[email protected]
_S ~]# vim /etc/services 2 nntp 119/tcp readnews untp # USENET News Transfer Protocol 3 nntp 119/udp readnews untp # USENET News Transfer Protocol 4 ntp 123/tcp 5 ntp 123/udp # Network Time Protocol

拓展:

NNTP(Network News Transport Protocol),中文釋義:(RFC-977)網路新聞傳輸協議。這是一個主要用於閱讀和釋出新聞文章(俗稱為“帖子”,比較正式的名字是“新聞組郵件”)到Usenet 上的Internet應用協議,也負責新聞在伺服器間的傳送。

 

安裝NTP

預設是已安裝,如果是最小化安裝系統,則需要手動安裝。

1)NTP相關軟體包

1 [[email protected]_S ~]# ls /mnt/Packages/ntp*
2 /mnt/Packages/ntp-4.2.6p5-1.el6.x86_64.rpm         #NTP服務端軟體包
3 /mnt/Packages/ntpdate-4.2.6p5-1.el6.x86_64.rpm     #NTP客戶端軟體包

2)安裝NTP服務端軟體包:

複製程式碼

1 [[email protected]_S ~]# yum -y install ntp         #NTP兩個軟體包都會安裝上
2 ...
3 Installed:
4   ntp.x86_64 0:4.2.6p5-1.el6                                      
5 Dependency Installed:
6   ntpdate.x86_64 0:4.2.6p5-1.el6                                  
7 Complete!

複製程式碼

    安裝客戶端:

1 [[email protected]_C ~]# rpm -ivh /mnt/Packages/ntpdate-4.2.6p5-1.el6.x86_64.rpm     #只安裝ntpdate安裝包
2 warning: /mnt/Packages/ntpdate-4.2.6p5-1.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
3 Preparing...                ########################################### [100%]
4     package ntpdate-4.2.6p5-1.el6.x86_64 is already installed

    NTP配置檔案所在位置

1 [[email protected]_S ~]# ls /etc/ntp.conf 
2 /etc/ntp.conf

3)啟動NTP服務

複製程式碼

 1 [[email protected]_S ~]# service ntpd start          #啟動ntp服務
 2 Starting ntpd:                                             [  OK  ]
 3 [[email protected]_S ~]# netstat -anptu |grep 123    #檢視埠123是否開放
 4 udp        0      0 10.0.0.15:123               0.0.0.0:*                               5846/ntpd           
 5 udp        0      0 127.0.0.1:123               0.0.0.0:*                               5846/ntpd           
 6 udp        0      0 0.0.0.0:123                 0.0.0.0:*                               5846/ntpd           
 7 udp        0      0 fe80::20c:29ff:fea9:c1ae:123 :::*                                    5846/ntpd           
 8 udp        0      0 ::1:123                     :::*                                    5846/ntpd           
 9 udp        0      0 :::123                      :::*                                    5846/ntpd           
10 [[email protected]_S ~]# chkconfig ntpd on           #開機自啟動

複製程式碼

 

實戰1:手動同步NTP時間伺服器

1)在lin_S伺服器上檢視可用的NTP時間伺服器

複製程式碼

1 [[email protected]_S ~]# vi /etc/ntp.conf 
2 # Use public servers from the pool.ntp.org project.
3 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
4 server 0.rhel.pool.ntp.org iburst       #找一個可以使用的NTP時間伺服器
5 server 1.rhel.pool.ntp.org iburst
6 server 2.rhel.pool.ntp.org iburst
7 server 3.rhel.pool.ntp.org iburst

複製程式碼

2)在lin_C客戶端上與NTP時間伺服器進行同步

1 [[email protected]_C ~]# ntpdate 0.rhel.pool.ntp.org
2 22 May 22:04:41 ntpdate[5733]: adjust time server 202.118.1.130 offset 0.027428 sec

 

拓展:

在lin_S服務端(已啟動NTP服務)與NTP時間伺服器進行同步會報錯。

1 [[email protected]_S ~]# ntpdate 0.rhel.pool.ntp.org
2 22 May 22:06:17 ntpdate[2464]: the NTP socket is in use, exiting

問題分析:

出現該錯誤的原因是lin_S伺服器正在執行ntpd服務,就不能用ntpdate來手動更新時間了。

解決方法:

關閉ntpd服務

1 [[email protected]_S ~]# service ntpd stop
2 Shutting down ntpd:                                        [  OK  ]
3 [[email protected]_S ~]# ntpdate 0.rhel.pool.ntp.org
4 22 May 22:13:27 ntpdate[2495]: adjust time server 202.118.1.130 offset -0.068183 sec

 

實戰2:搭建內網NTP伺服器,內網伺服器通過此NTP伺服器進行時間同步

1)修改NTP配置檔案

      藍色底紋部分為增加項

複製程式碼

 1 [[email protected]_S ~]# vi /etc/ntp.conf 
 2 # Permit all access over the loopback interface.  This could
 3 # be tightened as well, but to do so would effect some of
 4 # the administrative functions.
 5 restrict 127.0.0.1
 6 restrict -6 ::1
 7 restrict 10.0.0.0 mask 255.255.255.0       #允許10.0.0.0 網段中的伺服器訪問本ntp伺服器進行時間同步
 8 restrict 10.0.0.16                         #允許單個IP地址訪問本ntp伺服器
 9 # Use public servers from the pool.ntp.org project.
10 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
11 server 210.72.145.44    #指定本ntp伺服器的上游ntp伺服器為210.72.145.44,並且設定為首先伺服器。同步時間為:從上到下,寫的越靠上,優先順序越高。(此伺服器同步不了時間,尋找下一個ntp伺服器)。此IP地址是中國國家授時中心ntp伺服器。                    
12 server 133.100.11.8     #當上面伺服器同步不了,則尋找第二個。此IP地址是日本福岡大學ntp伺服器。
13 server 0.rhel.pool.ntp.org iburst
14 server 1.rhel.pool.ntp.org iburst
15 server 2.rhel.pool.ntp.org iburst
16 server 3.rhel.pool.ntp.org iburst
17 server 127.127.1.0           #local clock 如果上面的伺服器都無法同步時間,就和本地系統時間同步。127.127.1.0在這裡是一個IP地址,不是網段。
18 fudge 127.127.1.0 stratum 10    #127.127.1.0 為第10層。ntp 和127.127.1.0同步完後,就變成了11層。  ntp是層次階級的。同步上層伺服器的stratum 大小不能超過或等於16。

複製程式碼

2)啟動ntpd服務

複製程式碼

1 [[email protected]_S ~]# /etc/init.d/ntpd start
2 Starting ntpd:                                             [  OK  ]
3 [[email protected]_S ~]# netstat -ln |grep 123
4 udp        0      0 10.0.0.15:123               0.0.0.0:*                               
5 udp        0      0 127.0.0.1:123               0.0.0.0:*                               
6 udp        0      0 0.0.0.0:123                 0.0.0.0:*                               
7 udp        0      0 fe80::20c:29ff:fea9:c1ae:123 :::*                                    
8 udp        0      0 ::1:123                     :::*                                    
9 udp        0      0 :::123                      :::*  

複製程式碼

3)檢視ntpd服務狀態

指令“ntpq -p”可以列出目前我們的NTP與相關的上層NTP的狀態,以上的幾個欄位的意義如下:

remote:即remote - 本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先。

refid:參考的上一層NTP主機的地址

st:即stratum階層

poll:下次更新在幾秒之後

offset:時間補償的結果

複製程式碼

 1 [[email protected]_S ~]# ntpq -p         #列出本NTP伺服器與上游伺服器的連線狀態
 2      remote           refid      st t when poll reach   delay   offset  jitter
 3 ==============================================================================
 4  210.72.145.44   .INIT.          16 u    -   64    0    0.000    0.000   0.000
 5  133.100.11.8    .INIT.          16 u    -   64    0    0.000    0.000   0.000
 6 *dns1.synet.edu. 202.118.1.47     2 u   32   64    3   60.058    7.261   6.860
 7  news.neu.edu.cn .INIT.          16 u    -   64    0    0.000    0.000   0.000
 8  202.118.1.130   .INIT.          16 u    -   64    0    0.000    0.000   0.000
 9  42.96.167.209   .INIT.          16 u    -   64    0    0.000    0.000   0.000
10  LOCAL(0)        .LOCL.          10 l   96   64    2    0.000    0.000   0.000
11 [[email protected]_S ~]# ntpstat         #列出是否與上游伺服器連線。需要過5分鐘
12 synchronised to NTP server (202.112.29.82) at stratum 3     #可以看到我們當前在3層
13    time correct to within 257 ms    #ms 毫秒是一種較為微小的時間單位,是一秒的千分之一。
14    polling server every 64 s

複製程式碼

4)客戶端同步時間

1 [[email protected]_C ~]# date -s "2015-5-23 11:30"    #先設定一個錯誤的時間點
2 Sat May 23 11:30:00 CST 2015
3 [[email protected]_C ~]# ntpdate lin_S    #進行ntp時間同步
4 23 May 11:34:10 ntpdate[6686]: step time server 10.0.0.15 offset 31622622.275270 sec
5 [[email protected]_C ~]# date             
6 Mon May 23 11:34:16 CST 2016      #時間同步成功

常見錯誤:如下所示。

其實,這不是一個錯誤。而是由於每次重啟NTP伺服器之後大約要3-5分鐘客戶端才能與server建立正常的通訊連線。當此時用客戶端連線服務端就會報這樣的資訊。一般等待幾分鐘就可以了。

1 [[email protected]_C ~]# ntpdate lin_S
2 23 May 11:38:02 ntpdate[6694]: no server suitable for synchronization found

5)和NTP相關的配置檔案

/etc/sysconfig/clock        #這個是linux 的主要時區設定檔案。每次開機後,Linux會自動的讀取這個檔案來設定自己系統所預設要顯示的時間。

1 [[email protected]_S ~]# cat /etc/sysconfig/clock
2 # The time zone of the system is defined by the contents of /etc/localtime.
3 # This file is only for evaluation by system-config-date, do not rely on its
4 # contents elsewhere.
5 ZONE="Asia/Shanghai"

 

拓展1:linux系統時間和BIOS時間是不是一定一樣?

檢視硬體BIOS時間:

hwclock -r    讀出BIOS的時間引數

hwclock -w    將當前系統時間寫入BIOS中

複製程式碼

1 [[email protected]_C ~]# date -s "2016-5-22 13:50"
2 Sun May 22 13:50:00 CST 2016
3 [[email protected]_C ~]# hwclock -r
4 Mon 23 May 2016 01:53:50 PM CST  -0.110948 seconds
5 [[email protected]_C ~]# hwclock -w
6 [[email protected]_C ~]# hwclock -r
7 Sun 22 May 2016 01:50:48 PM CST  -0.783098 seconds     #已將系統date時間寫入到BIOS時間

複製程式碼

 

拓展2:不同機器之間的時間同步

為了避免主機時間因為長期運作下所導致的時間偏差,進行時間同步(synchronize)的工作是非常必要的。

同步時間,可以使用ntpdate命令,也可以使用ntp服務。

方法一:使用ntpdate 比較簡單。格式如下:

1 [[email protected]_C ~]# ntpdate lin_S
2 23 May 19:50:44 ntpdate[7507]: step time server 10.0.0.15 offset 1.239826 sec

但這樣的同步,只是強制性的將系統時間設定為ntp 伺服器時間。只是治標不治本。所以,一般配合cron命令,來進行定期同步設定。比如,在crontab 中新增:

1 [[email protected]_C ~]# crontab -e
2 0 12 * * * /usr/sbin/ntpdate lin_S

方法二:使用ntpd 服務進行同步。

要注意的是,ntpd 有一個自我保護設定:如果本機與上源時間相差太大,ntpd 不執行。所以新設定的時間伺服器一定要先ntpdate 從上源取得時間初值。然後啟動ntpd 服務。