CentOS 6.5 下搭建NTP服務器


參考網站:

http://www.iyunv.com/thread-64847-1-1.html

http://acooly.iteye.com/blog/1993484

1         檢查系統時區是否正確

在中國,正確的時區應為 CST(Chinese Standard Time),也就是我們通常所說的北京時間.

那么中國當地的時間晚上8點的話,我們可以有下面兩種表示方式

20:00 CST

12:00 UTC

因為中國處在UTC+8時區,依次類推,在UTC標准時間,就是12:00.

不管通過任何渠道我們想要同步系統的時間,通常提供方只會給出UTC+0的時間值而不會提供時區.所以當我們設置系統時間的時候,設置好時區是首先要做的工作

[root@xldwhj ~]# date

Fri Jul 22 22:11:21 CST 2016

如果發現時區不正確,修改時區,修改時區方法如下:

1.修改 /etc/sysconfig/clock

將原來的時區改為

[root@xldwhj ~]# vi /etc/sysconfig/clock

ZONE="America/New_York"

改為
ZONE="Asia/Shanghai"

2.覆蓋 /etc/localtime

[root@xldwhj ~]# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@xldwhj ~]#cp: overwrite `/etc/localtime'? y

3.查看修改后的時區

[root@xldwhj ~]# date

Fri Jul 22 22:11:21 CST 2016

 

2         檢查 NTP 是否安裝

[root@xldwhj ~]# rpm -qa |grep ntp
[root@xldwhj ~]#ntp-4.2.4p8-3.el6.centos.x86_64
[root@xldwhj ~]#ntpdate-4.2.4p8-3.el6.centos.x86_64

如上所示就是正確安裝了

如果未安裝,則使用yum安裝

[root@xldwhj ~]# yum -y install ntp

3         檢查上層 NTP 服務器是否正常連通

直接使用國家對時服務器

1.cn.pool.ntp.org

2.cn.pool.ntp.org

3.ch.pool.ntp.org

ntp.api.bz 是一組NTP服務器集群,目前有6台服務器,位於上海電信。這項服務是 api.bz 繼移動飛信免費短信發送接口之后的第二項免費 API 服務

測試兩個時鍾服務是否正常可用

[root@xldwhj ~]# ntpdate -q 1.cn.pool.ntp.org

server 202.112.29.82, stratum 2, offset 169580.733614, delay 0.05197

22 Jul 22:18:22 ntpdate[2671]: step time server 202.112.29.82 offset 169580.733614 sec

[root@xldwhj ~]# ntpdate -q 2.cn.pool.ntp.org

server 202.112.29.82, stratum 2, offset 169580.734524, delay 0.05310

22 Jul 22:20:09 ntpdate[2694]: step time server 202.112.29.82 offset 169580.734524 sec

如上所示,就是正常的,如果上層服務器不可用,一般如下顯示

1.server 202.112.29.82, stratum 0, offset 0.000000, delay 0.00000
2. 22 Jul 22:20:09 ntpdate[2694]: no server suitable for synchronization found

4         手工校正當前時間

當系統時間與標准時間相差太大時,NTP通常無法正常同步時間,因此我們一般要手工先對當前時間進行調整

兩個對時服務器都可以用,隨便使用一個既可

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

24 Jul 21:27:47 ntpdate[2697]: step time server 202.112.29.82 offset 169580.736829 sec
注意:如果NTPD服務已經運行了,這個命令將無法使用,要先關閉NTPD服務,就可以使用,調整好后,用 date 命令檢查一下,時間是否正確了

[root@xldwhj ~]# date

Sun Jul 24 21:27:56 CST 2016

5         NTP配置

如果時間還是不對,建議用 date 命令手工調整一下。

NTP 服務器是起一個承上啟下的作用,尋找上層服務器獲取正確的時間,同時為下層服務器提供網絡對時服務,所以對它的設置,也主要由兩方面組成.

5.1         指定上層對時服務器

加#號注釋掉centos默認的對時服務器,如果主機可以訪問外網,這一步最好做一下.

添加我們剛才測試的國家對時服務器.

prefer 表示優先使用
# vim /etc/ntp.conf

# 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

#server 1.centos.pool.ntp.org

#server 2.centos.pool.ntp.org

server 1.cn.pool.ntp.org

server 2.cn.pool.ntp.org prefer

5.2         設置對內網主機提供NTP服務

在我的機子中,內網使用 192.168.178.0/24 網段


# 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

restrict 192.168.178.0

mask 255.255.255.0  

nomodify notrap

幾點說明:

restrict default kod nomodify notrap nopeer noqueryNTP服務默認拒絕所有NTP連線

kod 當server的服務超過限制,server不是直接丟棄服務要求,而是傳送一個信號,讓client轉向要求其它server服務,或等一段時間再要求server服務

nomodify 用戶端不能更改NTP服務端的時間參數

notrap 不提供trap 這個遠程事件登錄功能

nopeer 避免其他人使用此ntpd作為time server

noquery 用戶端不能使用ntpq,ntpc等指令來查詢時間

restrict 127.0.0.1默認對本機無限制

restrict 192.168.178.0 mask 255.255.255.0 nomodify notrap這是我添加的,允許192.168.178.0/24 網段的主機來進行對時,但不允許客戶端來修改,登錄我的NTP服務器

#允許任何IP的客戶機都可以進行時間同步
restrict default nomodify

5.3         設置同步更新本地hwclock

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

1.# vim /etc/sysconfig/ntpd
2.# Drop root to id 'ntp:ntp' by default.
3.OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
4.SYNC_HWCLOCK="yes"

添加SYNC_HWCLOCK="yes"

1.啟動NTP服務

[root@xldwhj ~]# service ntpd start

Starting ntpd:                                             [  OK  ]

啟動后的觀察

2.確認端口監聽啟動

ntpd啟動 123 端口 通過UDP協議對外提供服務

[root@xldwhj ~]# netstat -nlpu | grep ntpd

udp        0      0 192.168.178.2:123           0.0.0.0:*                               2812/ntpd          

udp        0      0 192.168.254.128:123         0.0.0.0:*                               2812/ntpd          

udp        0      0 127.0.0.1:123               0.0.0.0:*                               2812/ntpd          

udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2812/ntpd          

udp        0      0 fe80::20c:29ff:feef:76b2:123 :::*                                    2812/ntpd          

udp        0      0 fe80::20c:29ff:feef:76a8:123 :::*                                    2812/ntpd          

udp        0      0 ::1:123                     :::*                                    2812/ntpd          

udp        0      0 :::123                      :::*                                    2812/ntpd

3.確認已進行時間同步

該命令執行,通常要等10到15分鍾,才會顯示同步成功

[root@xldwhj ~]# ntpstat

4.查看與上層服務器連接情況

 [root@xldwhj ~]# ntpq -p 

 remote遠程主機的主機名或IP

+已連線,可提供時間更新的候補服務器

*目前正在使用的上層NTP

refid 上級NTP的IP

st 就是stratum 上層NTP的層級

when 幾秒鍾前曾做過時間同步更新

poll 下一次更新在幾秒后

reach 已經向上層服務器要求更新的次數

delay 網絡傳輸過程中的延遲時間

jitter linux 系統時間與bios硬件時鍾之間的差異

5.確認無問題后,將服務設為開機啟動

[root@xldwhj ~]# chkconfig --list ntpd

ntpd              0:off       1:off       2:off       3:off       4:off       5:off       6:off

[root@xldwhj ~]# chkconfig ntpd on

[root@xldwhj ~]# chkconfig --list ntpd

ntpd              0:off       1:off       2:on 3:on 4:on 5:on 6:off

5.4         客戶端測試

客戶端到時鍾服務器同步時間,有兩種常見的用法:

首先可以測試同步:ntpdate 服務器ip地址

1.如果主機比較少,可以通過 cron 來同步時間

1.# vim /etc/crontab
2.*/3 * * * * root (/usr/sbin/ntpdate 192.168.178.2 && /sbin/hwclock -w) & >/dev/null

為了盡快看到執行效果,所以設置為每三分鍾同步一次,實際生產中不用這么頻繁,每天同步一次就差不多了

1.# tail -f /var/log/cron
2.Oct 31 11:37:01 CentOS2 crond[1522]: (*system*) RELOAD (/etc/crontab)
3.Oct 31 11:39:01 CentOS2 CROND[1688]: (root) CMD ((/usr/sbin/ntpdate 192.168.178.3 && /sbin/hwclock -w) & >/dev/null)

2.啟動NTP服務自動同步(未檢測)

如果內網服務器比較多

可以通過架設NTP來實現時間同步

因為該NTP只需向上層來同步時間,而不需對下層提供對時服務,就比較簡單了

只要重復上面NTP架設步驟,去除restrict 步驟即可

注意:

1 ntp服務啟動后5到10分鍾才會進行同步,所以別着急
2 當你的時間設置和NTP服務器的時間相差很大的時候,NTP同步會失敗

5.5         客戶端常見錯誤解決方法

錯誤1.Server dropped: Strata too high

在ntp客戶端運行ntpdate serverIP,出現no server suitable for synchronization found的錯誤。

在ntp客戶端用ntpdate –d serverIP查看,發現有“Server dropped: strata too high”的錯誤,並且顯示“stratum 16”。而正常情況下stratum這個值得范圍是“0~15”。

這是因為NTP server還沒有和其自身或者它的server同步上。

以下的定義是讓NTP Server和其自身保持同步,如果在/ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務提供給ntp客戶端。

server 127.127.1.0
fudge 127.127.1.0 stratum 8

在ntp server上重新啟動ntp服務后,ntp server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鍾,在這個時間之內在客戶端運行ntpdate命令時會產生no server suitable for synchronization found的錯誤。

那么如何知道何時ntp server完成了和自身同步的過程呢?

在ntp server上使用命令:

#watch ntpq -p

注意reach這個值,在啟動ntp server服務后,這個值就從0開始不斷增加,當增加到17的時候,從0到17是5次的變更,每一次是poll的值的秒數,是64秒*5=320秒的時間。

如果之后從ntp客戶端同步ntp server還失敗的話,用ntpdate –d來查詢詳細錯誤信息,再做判斷。

錯誤2.Server dropped:no data

從客戶端執行netdate –d時有錯誤信息如下:

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

192.168.30.22: Server dropped: no data

server 192.168.30.22, port 123

.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found

出現這個問題的原因可能有2:

1。檢查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定義中使用了notrust的話,會導致以上錯誤。

使用以下命令檢查ntp的版本:

#ntpq –c version


下面是來自ntp官方網站的說明:
The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".

In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd

 

解決:

把notrust去掉。


2。檢查ntp server的防火牆。可能是server的防火牆屏蔽了upd 123端口。

可以用命令

#service iptables stop


來關掉iptables服務后再嘗試從ntp客戶端的同步,如果成功,證明是防火牆的問題,需要更改iptables的設置。


免責聲明!

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



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