天天看點

NTP伺服器的搭建

一、服務介紹

NTP(Network Time Protocol,網絡時間協定)是用來使計算機時間同步化的一種協定。它可以使計

算機對其伺服器或時鐘源做同步化,它可以提供高精準度的時間校正(LAN上與标準間差小于1毫秒,

WAN上幾十毫秒),且可介由加密确認的方式來防止惡毒的協定攻擊。

二、運作模式

模式: C/S 模式

運作模式:

<a href="http://s2.51cto.com/wyfs02/M00/75/F9/wKiom1ZF8xDzKFVvAABPhWp3Z_k567.png" target="_blank"></a>

三、NTP伺服器環境的配置

1.實驗環境

NTP服務端:localhost1.cn   IP:192.168.10.63

NTP用戶端:localhost2.cn   IP:192.168.10.64

<a href="http://s4.51cto.com/wyfs02/M00/75/F9/wKiom1ZF81CxL_P1AAAtl3auOfM248.png" target="_blank"></a>

 2.NTP伺服器的配置

[root@localhost ~]# rpm -ivh/mnt/Packages/ntpdate-4.2.6p5-1.el6.x86_64.rpm

[root@localhost ~]# /etc/init.d/ntpd  restart

[root@localhost ~]# vim /etc/ntp.conf            ntp 的配置檔案

[root@localhost ~]# ntpdate server0.rhel.pool.ntp.org         同步ntp時間報錯

13 Nov 18:33:47 ntpdate[8145]: the NTPsocket is in use, exiting

原因

出現該錯誤的原因是系統ntpd伺服器正在運作中,通過 ps aux | grep ntpd 檢視,會看到ntpd正在運作。

<a href="http://s3.51cto.com/wyfs02/M01/75/F9/wKiom1ZF86WgroAhAAAnHl-fhuM895.png" target="_blank"></a>

解決辦法

[root@localhost ~]# /etc/init.d/ntpd stop  停止服務

<a href="http://s1.51cto.com/wyfs02/M02/75/F9/wKiom1ZF89DDeBd2AAA0hMAjgfo819.png" target="_blank"></a>

備注:這裡需要虛拟機可以連接配接到外網

搭建一個内網NTP伺服器,讓内網伺服器通過此NTP伺服器進行時間同步

[root@localhost ~]# vim /etc/ntp.conf

11 # Permit all access over the loopbackinterface.  This could

 12 #be tightened as well, but to do so would effect some of

 13 #the administrative functions.

 14restrict 127.0.0.1  如果無同步時間,就和本地系統時間同步。

 15restrict -6 ::1

 16

 17 #Hosts on local network are less restricted.

 18 restrict 192.168.10.0mask 255.255.255.0 nomodify notrap

允許同步的本地區域網路時間

 19

 20 # Use public serversfrom the pool.ntp.org project.

 21 # Please considerjoining the pool (http://www.pool.ntp.org/join.html).

 22 server 128.138.141.172

23 server 1.rhel.pool.ntp.org iburst

 24server 2.rhel.pool.ntp.org iburst

 25server 3.rhel.pool.ntp.org iburst

在啟動NTP服務前,先對提供服務的這台主機手動的校正一次時間。 ntpd 有一個自我保護設定: 如果本機與上源時間相差太大, ntpd 不運作.

[root@localhost ~]# ntpdate cn.pool.ntp.org

13 Nov 18:58:31 ntpdate[8214]: adjust timeserver 202.112.29.82 offset 0.000931 sec

[root@localhost ~]# /etc/init.d/ntpd start  啟動服務

<a href="http://s4.51cto.com/wyfs02/M02/75/F6/wKioL1ZF9GHQsAMxAABUCt8NvpM546.png" target="_blank"></a>

[root@localhost ~]# ntpstat

synchronised to NTP server(128.138.141.172) at stratum 2   #可以看到我們目前在2層。

  time correct to within 470 ms   # ms毫秒是一種較為微小的

  polling server every 128 s     輪詢時間

[root@localhost ~]# ntpq –p  列出我們的NTP伺服器與上遊伺服器的連接配接狀态

    remote           refid      st t when poll reach   delay  offset  jitter

==============================================================================

*utcnist2.colora .ACTS.           1 u 466  128  150 337.011   14.735  60.291

 dns.sjtu.edu.cn .INIT.          16 u    - 1024   0    0.000    0.000  0.000

+dns1.synet.edu. 202.118.1.46     2 u 233  128  176 115.893   99.310  68.327

參數說明

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

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

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

st:即stratum階層             poll:下次更新在幾秒之後          offset:時間補償的結果

用戶端同步時間

說明:在啟動NTP服務前,先對提供服務的這台主機手動的校正一次時間。 ntpd 有一個自我保護設定: 如果本機與上源時間相差太大, ntpd 不運作.

[root@localhost ~]# date

2015年 11月 13日 星期五 20:11:34 CST

[root@localhost1 ~]# date  同步時間前

2015年 11月 13日 星期五 20:09:39 CST

[root@localhost1 ~]# ntpdate 192.168.10.63  同步時間

13 Nov 20:12:53 ntpdate[7737]: step timeserver 192.168.10.63 offset 130.847558 sec

[root@localhost1 ~]# date

2015年 11月 13日 星期五 20:13:00 CST

拓展

linux系統時間和BIOS時間是不是一定一樣?

檢視硬體BIOS時間:

hwclock-r    :讀出BIOS的時間參數

hwclock-w    :将目前系統時間寫入BIOS中。

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

同步時間,可以使用ntpdate指令,也可以使用ntpd服務。

通常采用crontab 做計劃任務

0 12 *  * * /usr/sbin/ntpdate 0.rhel.pool.ntp.org

本文轉自 水滴石川1 51CTO部落格,原文連結:http://blog.51cto.com/sdsca/1712636,如需轉載請自行聯系原作者

繼續閱讀