歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> CentOS中NTP服務器的搭建

CentOS中NTP服務器的搭建

日期:2017/3/3 16:14:25   编辑:關於Linux

先簡單介紹幾個名詞

Atomic Clock: 現在計算時間最准確的是使用 原子震蕩周期 所計算的物理時鐘(Atomic Clock),因此也被定義為標准時間(International Atomic Time)

UTC(coordinated Universal Time): 協和標准時間 就是利用 Atomic Clock 為基准定義出來的正確時間

硬件時鐘: 硬件時鐘是指嵌在主板上的特殊的電路, 它的存在就是平時我們關機之後還可以計算時間的原因

系統時鐘: 就是操作系統的kernel所用來計算時間的時鐘. 它從1970年1月1日00:00:00 UTC時間到目前為止秒數總和的值

搭建 NTP 服務之前的准備

1.檢查系統時區是否正確

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

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

20:00 CST

12:00 UTC

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

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

# date

Thu Oct 31 11:22:45 EDT 2013

如果發現時區不正確,修改方法見本文 附1

2.檢查 NTP 是否安裝

# rpm -qa |grep ^ntp

ntp-4.2.4p8-3.el6.centos.x86_64

ntpdate-4.2.4p8-3.el6.centos.x86_64

如上所示就是正確安裝了

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

直接使用國家對時服務器

1.cn.pool.ntp.org

2.cn.pool.ntp.org

為了防止出現上層對時服務器故障,無法正確提供時間同步,一般建議配置兩個或以上時間同步服務器.下面分別測試兩個時鐘服務是否正常可用

# ntpdate -q 1.cn.pool.ntp.org

server 202.112.10.36, stratum 2, offset -1.883582, delay 0.18350

server 202.112.29.82, stratum 2, offset -1.860654, delay 0.23692

server 202.112.31.197, stratum 2, offset -1.812637, delay 0.13278

server 218.75.4.130, stratum 2, offset -1.837221, delay 0.04099

31 Oct 09:07:03 ntpdate[1625]: step time server 218.75.4.130 offset -1.837221 sec

# ntpdate -q 2.cn.pool.ntp.org

server 202.112.10.36, stratum 2, offset -1.834295, delay 0.07317

server 202.112.29.82, stratum 2, offset -1.810122, delay 0.12784

server 202.112.31.197, stratum 2, offset -1.838527, delay 0.07227

server 218.75.4.130, stratum 2, offset -1.833332, delay 0.04346

31 Oct 09:12:36 ntpdate[22558]: step time server 218.75.4.130 offset -1.833332 sec

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

server 10.32.196.60, stratum 0, offset 0.000000, delay 0.00000

26 Oct 15:01:17 ntpdate[23338]: no server suitable for synchronization found

4.手工校正當前時間

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

兩個對時服務器都可以用,隨便挑一個就行

# ntpdate 2.cn.pool.ntp.org

31 Oct 09:14:18 ntpdate[22563]: step time server 218.75.4.130 offset -1.830097 sec

調整好後,用 date 命令檢查一下,時間是否正確了

# date

Thu Oct 31 09:14:50 CST 2013

如果時間還是不對,建議用 date 命令手工調整一下,我在實驗過程中沒有遇到過,但有的網友說會說現這種情況

設置 NTP 的配置文件

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

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

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

在我的實驗環境中,內網使用 192.168.80.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.80.0 mask 255.255.255.0 nomodify notrap

幾點說明:

restrict default kod nomodify notrap nopeer noquery

kod kod技術可以阻止 "Kiss of Death"包(一種DOS攻擊)對服務器的破壞

nomodify 用戶端不能更改NTP服務端的時間參數,但可以通過NTP服務端進行時間校對

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

nopeer 不與其它同一層的NTP服務器進行時間同步

noquery 不提供NTP服務

restrict 還可以使用的其它參數

ignore 關閉所有NTP服務

notrust 拒絕沒有通過認證的客戶端

restrict 127.0.0.1默認對本機無限制

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

3.設置同步更新本地hwclock

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

# vim /etc/sysconfig/ntpd

# Drop root to id 'ntp:ntp' by default.

OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"

SYNC_HWCLOCK="yes"

添加SYNC_HWCLOCK="yes"

啟動NTP服務

# service ntpd start

Starting ntpd: [ OK ]

啟動後的觀察

1.確認端口監聽啟動

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

# netstat -nlpu |grep ntpd

udp 0 0 192.168.80.3:123 0.0.0.0:* 22816/ntpd

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

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

udp 0 0 fe80::20c:29ff:fed4:315f:123 :::* 22816/ntpd

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

udp 0 0 :::123 :::* 22816/ntpd

2.查看系統日志,確定啟動過程無報錯

# tail -n 20 -f /var/log/messages

Oct 31 10:56:29 CentOS1 ntpd[22815]: ntpd [email protected] Fri Feb 22 11:23:27 UTC 2013 (1)

Oct 31 10:56:29 CentOS1 ntpd[22816]: precision = 0.052 usec

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #0 wildcard, 0.0.0.0#123 Disabled

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #1 wildcard, ::#123 Disabled

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #2 lo, ::1#123 Enabled

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #3 eth0, fe80::20c:29ff:fed4:315f#123 Enabled

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #4 lo, 127.0.0.1#123 Enabled

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #5 eth0, 192.168.80.3#123 Enabled

Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on routing socket on fd #22 for interface updates

Oct 31 10:56:29 CentOS1 ntpd[22816]: kernel time sync status 2040

Oct 31 10:56:29 CentOS1 ntpd[22816]: frequency initialized 23.140 PPM from /var/lib/ntp/drift

3.確認已進行時間同步

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

# ntpstat

synchronised to NTP server (202.112.31.197) at stratum 3

time correct to within 80 ms

polling server every 128 s

#進行時間校對的NTP服務器

#本地主機與上層NTP服務器的時間差

#下次同步時間

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

# ntpq -p

remote refid st t when poll reach delay offset jitter

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

+dns1.synet.edu. 202.118.1.46 2 u 13 64 77 567.019 132.808 32.883

*dns2.synet.edu. 202.118.1.46 2 u 58 64 37 209.322 -47.016 36.082

remote 本地主機所連接的上層NTP服務器

*目前正在使用的上層NTP

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

refid 給上層NTP服務器提供時間校對的服務器

st 就是stratum 上層NTP的級別

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

poll 下一次更新在幾秒後

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

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

offset 本地主機與上層NTP服務器的時間差,該值的絕對值越接近0,與上層服務器的時間就越接近

jitter 一個統計值,這個值的絕對值越小,本地主機的時間就越精確

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

# chkconfig --list ntpd

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

# chkconfig ntpd on

# chkconfig --list ntpd

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

 

客戶端設置

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

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

# vim /etc/crontab

*/3 * * * * root (/usr/sbin/ntpdate 192.168.80.3 && /sbin/hwclock -w) & >/dev/null

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

# tail -f /var/log/cron

Oct 31 11:37:01 CentOS2 crond[1522]: (*system*) RELOAD (/etc/crontab)

Oct 31 11:39:01 CentOS2 CROND[1688]: (root) CMD ((/usr/sbin/ntpdate 192.168.80.3 && /sbin/hwclock -w) & >/dev/null)

2.啟動NTP服務自動同步

如果內網服務器比較多

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

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

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

附1:

1.查看當前時區

# date

Thu Oct 31 09:37:09 EDT 2013

2.修改 /etc/sysconfig/clock

將原來的時區改為

# vi /etc/sysconfig/clock

ZONE="America/New_York"

#改為

ZONE="Asia/Shanghai"

3.覆蓋 /etc/localtime

# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp: overwrite `/etc/localtime'? y

4.查看修改後的時區

# date

Thu Oct 31 21:42:33 CST 2013

本文出自 “月白白” 博客,請務必保留此出處http://yuebaibai222.blog.51cto.com/2535988/1317928

Copyright © Linux教程網 All Rights Reserved