9.linux ntp服務器搭建

一、先介紹幾個與時間相關常見的概念

GMT:Greenwich Mean Time    格林威治時間

    理論上來說,格林尼治標準時間的正午是指當太陽橫穿格林尼治子午線時(也就是在格林尼治上空最高點時)的時間。由於地球在它的橢圓軌道里的運動速度不均勻,這個時刻可能和實際的太陽時相差16分鐘。

UTC:Coordinated Universal Time  協調世界時  

     世界標準時間 

CST:(Chinese Standard Time)

    CST卻同時可以代表如下 4 個不同的時區:

        Central Standard Time (USA)       UT-6:00
        Central Standard Time (Australia) UT+9:30
        China Standard Time               UT+8:00
        Cuba Standard Time UT-4:00


二、操作系統上的兩種時間


軟件時鐘:也叫系統時間,根據1970/01/01開始計算的總秒數

查看系統時間:
    [root@node1 ~]# date            #查看當前時間
    Wed Jun 28 19:57:53 CST 2017

    [root@node1 ~]#  date +"%Y-%m-%d" #設置當前時間格式
    2017-06-28
    
    [root@node1 ~]# date +"%Y-%m-%d %H:%M:%S"    #設置當前時間格式
    2017-06-28 19:58:42
    
    [root@node1 ~]# date +%s         #從1970/01/01到現在的總秒數
    1498651211
設置時間:


date  -s  "20120523 01:01:01"   #設置當前系統時間,一般不建議直接在生產主機上執行。
date  -s "dd/mm/yyyy hh:mm:ss"


硬件時鐘:主機硬件系統上的時鐘。主板上BIOS中的時間,由主板電池供電來維持運行,系統開機時要讀取這個時間,並根據它來設定系統時間.

查看硬件時間: hwclock 或clock
設置: hwclock --set --date="06/18/14 14:55"


硬件時間與系統時間相互轉換:

clock --hctosys      #將系統時間調整爲目前的硬件時間一致;當Linux啓動時,系統時鐘會去讀取硬件時鐘的設定,之後系統時鐘即獨立運作.
clock --systohc      #將硬件時鐘調整爲與目前的系統時鐘一致;將系統時間存回硬件時鐘內


三、NTP服務器安裝與設置


1.所需要的軟件

    ntp      主要軟件  

    tzdata   提供各時區對應的顯示格式


2.相關文件

     /etc/ntp.conf     主配置文件

     /usr/share/zoneinfo/    由tzdata提供,爲各時區的時間格式對應文件。

     /etc/sysconfig/clock    設置時區是否使用UTC時鐘的配置文件。

 [root@node1 zoneinfo]# cat /etc/sysconfig/clock 
  ZONE="Asia/Shanghai"
 #表明我們的時間配置文件要使用/usr/share/zoneinfo/Asia/Shanghai那個文件

    /etc/localtime     本地端的時間配置文件


簡單的一個例子:
目前在上海,假如到了美國,怎麼調整時間?
首先    vim /etc/sysconfig/clock  
        ZONE="America/New_York"
然後    cp  /usr/share/zoneinfo/America/New_York    /etc/localtime 
最後    date


3.主配置文件

[root@node1 ~]# cat /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     

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::1

# 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.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst

server 1.cn.pool.ntp.org

#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

# 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

參數解釋:

(1)driftfile  記錄時間差異

(2)restrict   管理權限控制

restrict   [ip]  mask [netmask_ip]  [parameter]
    parameter主要有:
       ignore:拒絕所有類型的NTP連接
       nomodify:客戶端不能使用ntpc/ntpq來修改服務器的時間參數,但客戶端可以進行網絡校時
       noquery:客戶端不能用ntpq/ntpc來查詢時間服務器,等於不提供NTP的網絡校時
       notrap:不提供trap這個遠程事件登陸
       notrust:拒絕沒有認證的客戶端
       
如果在[parameter] 沒有設置,則表示該網段不受任何限制。


(3)server:設置上層的NTP服務器

server [ip或主機名]  [prefer]


四、服務器NTP服務的啓動

#/etc/init.d/ntpd start    #啓動服務
#chkconfig ntpd on         #開機自啓

#tail /var/log/messages    #查看日誌


查看端口:
[root@node1 ~]# netstat -tulnp | grep ntp
udp        0      0 192.168.209.128:123         0.0.0.0:*                               2766/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2766/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2766/ntpd           
udp        0      0 fe80::20c:29ff:fe67:be62:123 :::*                                    2766/ntpd           
udp        0      0 ::1:123                     :::*                                    2766/ntpd           
udp        0      0 :::123                      :::*                                    2766/ntpd

通常啓動NTP15分鐘後纔會和上層NTP服務器順利連接上。


#ntpstat 查看ntp服務器是否與上層連接。
synchronised to NTP server (xx.xx.xx.xx) at stratum 3 
   time correct to within 538 ms
   polling server every 128 s

   
      
[root@node1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 85.199.214.101  .GPS.            1 u   55   64    1  245.097  179.415   0.000

 
remote: NTP主機的IP或者主機名
    * 代表目前正在作用當中的上層NTP
    + 代表已經連接成功,可以作爲下一個提供更新時間的候選者
refid:NTP服務器使用的更高一級服務器的名稱
st: stratum階層
when:幾秒前曾經做過時間同步化更新的操作.上一次成功請求之後到現在的秒數
poll:下一次更新在幾秒鐘之後.本地和遠程服務器多少時間進行一次同步,單位秒,在一開始運行NTP的時候這個poll值會比較小,服務器同步的頻率大,可以儘快調整到正確的時間範圍,之後poll值會逐漸增大,同步的頻率也就會相應減小
reach:已經向上層NTP服務器要求更新的次數.用來測試能否和服務器連接,是一個八進制值,每成功連接一次它的值就會增加
delay:網絡傳輸過程當中延遲的時間.從本地機發送同步要求到ntp服務器的往返時間
offset:時間補償的結果.主機通過NTP時鐘同步與所同步時間源的時間偏移量,單位爲毫秒,offset越接近於0,主機和ntp服務器的時間越接近
jitter:Linux系統時間與硬件時間的差異時間。這個數值的絕對值越小,主機的時間就越精確




五、客戶端同步NTP服務器的時間

方法1:
    ntpdate 192.168.209.128
    編輯定時任務crontab,每隔30分鐘自動同步,並寫入硬件時鐘。
    */30  * * * * ( /usr/sbin/ntpdate 192.168.209.128 && hwclock -w ) &>/dev/null
    
方法2:
    修改/etc/ntp.conf配置文件,開啓ntpd服務,使其自動去同步上層ntp服務器。
     添加server 1.cn.pool.ntp.org    
     
     /etc/init.d/ntpd start
     chkconfig ntpd on




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章