1. 程式人生 > >A Case of NTP Exception

A Case of NTP Exception

最近虛擬化伺服器出現時鐘同步異常:定時同步,但發現時間比個人電腦時間相差10幾個小 時。看了下鳥哥的私房菜,覺得沒太大問題,於是決定測試NTP服務以排查問題。因網路限 制,無法連線到網際網路,就採用其中的一臺虛擬機器作為NTP伺服器,並以本機時間為準。測 試後才知道原來是系統安裝時沒有選擇時區(預設為美國紐約),而使用的NTP伺服器時區 為中國,所以導致虛擬化伺服器時間相差13個小時。

從這次經歷,再次得到一些經驗教訓:

  • 安裝Linux時注意選擇合適的時區;
  • 仔細閱讀官方文件,這些才是最權威的文件;
  • 結合理論進行測試,絕知此事要躬行;
  • 要掌握一個理論,不是一朝一夕之事;

搭建NTP伺服器

主要配置

啟用本機時鐘作為NTP基準時間,主要需取消註釋server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10

完整配置如下:

[[email protected] ~]# 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.

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

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. 
server	127.127.1.0	# local clock
fudge	127.127.1.0 stratum 10	

# 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

開啟服務

開啟ntp服務

[[email protected] ~]# service ntpd restart
Shutting down ntpd:                                        [  OK  ]
Starting ntpd:                                             [  OK  ]

注意:

每次重啟NTP服務之後大約要5分鐘客戶端才能建立正常的NTP通訊連線,否則在執行ntpdate時候將返回:

[[email protected] ~]# ntpdate -d 192.168.1.162
... ...
192.168.1.162: Server dropped: strata too high
server 192.168.1.162, port 123
stratum 16, precision -23, leap 11, trust 000
... ...
8 Jan 19:28:07 ntpdate[17795]: no server suitable for synchronization found

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

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

同步測試

1. NTP伺服器(192.168.1.162)

當前時間:

[[email protected] ~]# date
Tue Jan  8 19:32:42 EST 2013

2. NTP客戶端時間(192.168.1.163):

當前時間:

[[email protected] ~]# date
Tue Jan  8 19:33:37 EST 2013

與192.168.1.162同步:

[[email protected] ~]# ntpdate 192.168.1.162
 8 Jan 19:33:51 ntpdate[24174]: adjust time server 192.168.1.162 offset -0.000002 sec

與172.31.1.1同步,時間滯後,大概是13個小時:

[[email protected] ~]# ntpdate 172.31.1.1
 8 Jan 06:34:10 ntpdate[24270]: step time server 172.31.1.1 offset -46801.549132 sec

在與172.31.1.1同步時,出現時鐘同步異常,因與192.168.1.162同步的時鐘相差太大。導致這種情況大致推測有兩個原因:1)172.31.1.1上時間不對;2)192.168.1.162、163時間不對。不過這兩點很快都被排除了,經過同事提點,檢視鳥哥的私房菜,對比世界時差表,才知道是192.168.1.162、163系統的時區不正確。由於系統安裝時沒有選擇時區(預設為美國紐約),與中國上海大概相差13個小時。

3.調整時區

系統當前時區

[[email protected] ~]# cat /etc/sysconfig/clock.20130109 
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="America/New York"

調整後系統時區

[[email protected] ~]# cat /etc/sysconfig/clock
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Shanghai"

調整後時,再次與172.31.1.1同步:

[[email protected] ~]# date
Wed Jan  9 21:59:23 CST 2013
[[email protected] ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
cp: overwrite `/etc/localtime'? y
[[email protected] ~]# clock -w
[[email protected] ~]# date
Wed Jan  9 21:59:53 CST 2013
[[email protected] ~]# ntpdate 172.31.1.1
 9 Jan 09:04:46 ntpdate[5898]: step time server 172.31.1.1 offset -46801.989143 sec

設定定時同步

[[email protected] ~]# crontab -l
00 07 * * *  /usr/sbin/ntpdate 172.31.1.1 && /sbin/hwclock -w

鳥哥的講解

例題:

假設你的筆記型電腦安裝 CentOS 這套系統,而且選擇的時區為臺灣。現在,你將有一個月的時間要出差到美國的紐約去, 你會帶著這個筆電,那麼到了美國之後,時間會不一致啊!你該如何手動的調整時間引數呢?

答:

因為時區資料檔在 /usr/share/zoneinfo 內,在該目錄內會找到 /usr/share/zoneinfo/America/New_York 這個時區檔。 而時區設定檔在 /etc/sysconfig/clock ,且目前的時間格式在 /etc/localtime ,所以你應該這樣做:

[[email protected] ~]# date
Thu Jul 28 15:08:39 CST 2011  <==重點是 CST 這個時區喔!

[[email protected] ~]# vim /etc/sysconfig/clock
ZONE="America/New_York"       <==改的是這裡啦!

[[email protected] ~]# cp /usr/share/zoneinfo/America/New_York /etc/localtime
[[email protected] ~]# date
Thu Jul 28 03:09:21 EDT 2011  <==時區與時間都改變了!

這個範例做完之後,記得將這兩個檔案改回來!不然以後你的時間都是美國時間啦!

參考

blog comments powered by Disqus