天天看點

ntp redhat

http://yuebaibai222.blog.51cto.com/2535988/1317928              

先簡單介紹幾個名詞

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的時間值而不會提供時區(因為它不知道你在哪裡).是以當我們設定系統時間的時候,設定好時區是首先要做的工作

1

2

# date

Thu Oct 31 11:22:45 EDT 2013

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

2.檢查 NTP 是否安裝

3

# 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

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

4

5

6

7

8

9

10

11

12

# 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

# date

Thu Oct 31 21:42:33 CST 2013

上一篇: redhat安裝
下一篇: redhat training

繼續閱讀