linux時間同步,ntpd、ntpdate

在Windwos中,系統時間的設置很簡單,界面操做,通俗易懂。並且設置後,重啓,關機都不要緊。系統時間會自動保存在Bios的時鐘裏面,啓動計算機的時候,系統會自動在Bios裏面取硬件時間,以保證時間的不間斷。html

    但在Linux下,默認狀況下,系統時間和硬件時間,並不會自動同步。在Linux運行過程當中,系統時間和硬件時間以異步的方式運行,互不干擾。硬件時間的運行,是靠Bios電池來維持,而系統時間,是用CPU tick來維持的。linux

    在系統開機的時候,會自動從Bios中取得硬件時間,設置爲系統時間。ios

 

.Linux系統時間的設置數據庫

    在Linux中設置系統時間,能夠用date命令:安全

//查看時間服務器

[root@localhost ~]# date
2008年 12月 12日星期五 14:44:12 CST網絡

//修改時間
[root@localhost ~]# date -set  "2013-12-24 00:01" <== (年/月/日 時:分【:秒】)
2009年 01月 01日星期四 00:01:00 CSTapp

//date 有幾種時間格式可接受,這樣也能夠設置時間:less

[root@localhost ~]# date 012501012009.30  <== 月日時分年.秒
2009年 01月 25日星期日 01:01:30 CST異步

 

 .Linux硬件時間的設置

    硬件時間的設置,能夠用hwclock或者clock命令。其中,clock和hwclock用法相近,只用一個就行,只不過clock命令除了支持x86硬件體系外,還支持Alpha硬件體系。

//查看硬件時間能夠是用hwclock,hwclock --show 或者hwclock -r

[root@localhost ~]# hwclock --show
2008年12月12日星期五 06時52分07秒  -0.376932 seconds

//設置硬件時間

[root@localhost ~]# hwclock --set --date="1/25/09 00:00" <== 月/日/年時:分:秒
[root@localhost ~]# hwclock
2009年01月25日星期日 00時00分06秒  -0.870868 seconds

[root@localhost ~]# hwclock  -w   根據系統時間設置硬件時間

 

.系統時間和硬件時間的同步

    同步系統時間和硬件時間,可使用hwclock命令。

//以系統時間爲基準,修改硬件時間

[root@localhost ~]# hwclock --systohc<== sys(系統時間)to(寫到)hc(Hard Clock)
[root@localhost ~]# hwclock -w

//以硬件時間爲基準,修改系統時間

[root@localhost ~]# hwclock --hctosys
[root@localhost ~]# hwclock -s

 

.不一樣機器之間的時間同步(重點)

    爲了不主機時間由於長期運做下所致使的時間誤差,進行時間同步(synchronize)的工做是很是必要的。Linux系統下,通常使用ntp服務器來同步不一樣機器的時間。一臺機器,能夠同時是ntp服務器和ntp客戶機。在網絡中,推薦使用像DNS服務器同樣分層的時間服務器來同步時間。

    同步時間,可使用ntpdate命令,也可使用ntpd服務(debian中ntp服務器爲ntp服務,須要安裝apt-get install ntp,同時對上手動同步時間須要用到ntpdate,故還需安裝apt-get install  ntpdate)。

    使用ntpdate比較簡單。格式以下:

[root@linux ~]# ntpdate [-nv] [NTP IP/hostname]
[root@linux ~]# ntpdate 192.168.0.2
[root@linux ~]# ntpdate time.ntp.org

    但這樣的同步,只是強制性的將系統時間設置爲ntp服務器時間。若是cpu tick有問題,只是治標不治本。因此,通常配合cron命令,來進行按期同步設置。好比,在crontab中添加:

0 12 * * * * /usr/sbin/ntpdate 192.168.0.1

     這樣,會在天天的12點整,同步一次時間。ntp服務器爲192.168.0.1。

    使用ntpd服務,要好於ntpdate加cron的組合。由於,ntpdate同步時間,會形成時間的跳躍,對一些依賴時間的程序和服務會形成影響。好比sleep,timer等。並且,ntpd服務能夠在修正時間的同時,修正cpu tick。理想的作法爲,在開機的時候,使用ntpdate強制同步時間,在其餘時候使用ntpd服務來同步時間。

    要注意的是,ntpd有一個自我保護設置: 若是本機與上源時間相差太大, ntpd不運行. 因此新設置的時間服務器必定要先ntpdate從上源取得時間初值, 而後啓動ntpd服務。ntpd服務運行後, 先是每64秒與上源服務器同步一次, 根據每次同步時測得的偏差值經複雜計算逐步調整本身的時間, 隨着偏差減少, 逐步增長同步的間隔. 每次跳動, 都會重複這個調整的過程.

 

.ntpd服務的設置

    ntpd服務的相關設置文件以下:

1./etc/ntp.conf:這個是NTP daemon的主要設文件,也是 NTP 惟一的設定文件。

2./usr /share/zoneinfo/:在這個目錄下的文件實際上是規定了各主要時區的時間設定文件,例如北京地區的時區設定文件在/usr/share/zoneinfo/Asia/Beijing 就是了。這個目錄裏面的文件與底下要談的兩個文件(clock 與localtime)是有關係的。

3./etc/sysconfig/clock:這個文件其實也不包含在NTP 的 daemon 當中,由於這個是linux的主要時區設定文件。每次開機後,Linux 會自動的讀取這個文件來設定本身系統所默認要顯示的時間。

4./etc /localtime:這個文件就是「本地端的時間配置文件」。剛剛那個clock 文件裏面規定了使用的時間設置文件(ZONE) 爲/usr/share/zoneinfo/Asia/Beijing ,因此說,這就是本地端的時間了,此時, Linux系統就會將Beijing那個文件另存爲一份/etc/localtime文件,因此將來咱們的時間顯示就會以Beijing那個時間設定文件爲準。

5. /etc/timezone:系統時區文件

 

    下面重點說說 /etc/ntp.conf文件的設置。在 NTP Server 的設定上面,其實最好不要對 Internet 無限制的開放,儘可能僅提供您本身內部的 Client 端聯機進行網絡校時就好。此外, NTP Server 總也是須要網絡上面較爲準確的主機來自行更新本身的時間啊,因此在咱們的 NTP Server 上面也要找一部最靠近本身的 Time Server 來進行自我校訂。事實上, NTP 這個服務也是Server/Client 的一種模式。

[root@linux ~]# vi /etc/ntp.conf
# 1. 關於權限設定部分
#  權限的設定主要以 restrict 這個參數來設定,主要的語法爲:
#   restrict IP mask netmask_IP parameter
#   其中 IP 能夠是軟件地址,也能夠是 default ,default 就相似 0.0.0.0
#  至於paramter則有:
#   ignore :關閉全部的 NTP 聯機服務
#   nomodify:表示 Client 端不能更改 Server 端的時間參數,不過,

#   Client 端仍然能夠透過 Server 端來進行網絡校時。
#   notrust:該 Client 除非經過認證,不然該 Client 來源將被視爲不信任網域
#   noquery:不提供 Client 端的時間查詢

#   notrap:不提供trap這個遠程事件登入

#  若是paramter徹底沒有設定,那就表示該 IP (或網域)「沒有任何限制」

restrict default nomodifynotrapnoquery # 關閉全部的 NTP 要求封包
restrict 127.0.0.1    #這是容許本級查詢
restrict 192.168.0.1 mask 255.255.255.0 nomodify
#在192.168.0.1/24網段內的服務器就能夠經過這臺NTP Server進行時間同步了
# 2. 上層主機的設定
#  要設定上層主機主要以 server 這個參數來設定,語法爲:
#  server [IP|HOST Name] [prefer]
#  Server 後面接的就是咱們上層 Time Server 囉!而若是 Server 參數
#  後面加上perfer的話,那表示咱們的 NTP 主機主要以該部主機來做爲
#  時間校訂的對應。另外,爲了解決更新時間封包的傳送延遲動做,
#  因此可使用driftfile來規定咱們的主機
#  在與 Time Server 溝通時所花費的時間,能夠記錄在driftfile 
#  後面接的文件內,例以下面的範例中,咱們的 NTP server 與 
#  cn.pool.ntp.org聯機時所花費的時間會記錄在 /etc/ntp/drift文件內
server 0.pool.ntp.org

server 1.pool.ntp.org

server 2.pool.ntp.org

server cn.pool.ntp.org prefer

#其餘設置值,以系統默認值便可

server  127.127.1.0     # local clock

fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys /etc/ntp/keys

總結一下,restrict用來設置訪問權限,server用來設置上層時間服務器,driftfile用來設置保存漂移時間的文件。

 

.ntp服務的啓動與觀察

在啓動NTP服務前,先對提供服務的這臺主機手動的校訂一次時間咯。(由於啓動服務器,端口會被服務端佔用,就不能手動同步時間了)

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

25 Apr 14:33:51 ntpdate[8310]: step time server 80.85.129.2 offset 6.655976 sec

而後,啓動ntpd服務:

[root@linux ~] # service ntpd start

或 [root@linux ~] # /etc/init.d/ntpd start

查看端口:

[root@linux ~] # netstat -ln|grep 123

udp        0      0 192.168.228.153:123        0.0.0.0:*

udp        0      0 127.0.0.1:123               0.0.0.0:*

udp        0      0 0.0.0.0:123                  0.0.0.0:*

udp        0      0 :::123                       :::*

如何確認咱們的NTP服務器已經更新了本身的時間呢?

[root@linux ~] # ntpstat

synchronized to NTP server(127.127.1.0) at stratum 11

time correct to within 950ms

polling server every 64 s

#改指令可列出NTP服務器是否與上層聯機。由上述輸出結果可知,時間校訂約

#爲950*10(-6)秒。且每隔64秒會主動更新時間。

常見的錯誤:

25 Apr 15:30:17 ntpdate[11520]: no server suitable for synchronization found

其實,這不是一個錯誤。而是因爲每次重啓NTP服務器以後大約要3-5分鐘客戶端才能與server創建正常的通信鏈接。當此時用客戶端鏈接服務端就會報這樣的信息。通常等待幾分鐘就能夠了。

[root@linux ~] # ntptrace –n 127.0.0.1

127.0.0.1:stratum 11, offset 0.000000,synch distance 0.950951

222.73.214.125:stratum 2,offset –0.000787,synch distance 0.108575

209.81.9.7:stratum 1,offset 0.000028,synch distance 0.00436,refid ‘GPS’

#這個指令能夠列出目前NTP服務器(第一層)與上層NTP服務器(第二層)彼此之間的

#關係

[root@linux ~] # ntpq –p

 

指令「ntpq -p」能夠列出目前咱們的NTP與相關的上層NTP的狀態,以上的幾個字段的意義以下:

remote:即NTP主機的IP或主機名稱。注意最左邊的符號,若是由「+」則表明目前正在做用鐘的上層NTP,若是是「*」則表示也有連上線,不過是做爲次要聯機的NTP主機。

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

st:即stratum階層

when:幾秒前曾作過期間同步更新的操做

poll:下次更新在幾秒以後

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

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

offset:時間補償的結果

jitter:Linux系統時間與BIOS硬件時間的差別時間

 

    最後說起一點,ntp服務,默認只會同步系統時間。若是想要讓ntp同時同步硬件時間,能夠設置/etc/sysconfig/ntpd文件。

在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可讓硬件時間與系統時間一塊兒同步。

轉自:http://blog.sina.com.cn/s/blog_636a55070101u1mg.html

 

其它:

NTP的配置

 

 

 

A: 配置/etc/ntp.conf

 

NTP Server的主要配置文件爲/etc/ntp.conf ,沒有修改過的ntp.conf文件內容以下所示,配置選項都有相關注釋信息(Linux 版本爲Red Hat Enterprise Linux Server release 6.6 )

[root@localhost ~]# more /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.
restrict 127.0.0.1 
restrict -6 ::1
 
# 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.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
 
 
#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
 
# 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

各個選項信息:

#系統時間與BIOS事件的誤差記錄

driftfile /etc/ntp/drift

 

restrict 控制相關權限。

語法爲: restrict IP地址 mask 子網掩碼 參數

其中IP地址也能夠是default ,default 就是指全部的IP

參數有如下幾個:

ignore  :關閉全部的 NTP 聯機服務

nomodify:客戶端不能更改服務端的時間參數,可是客戶端能夠經過服務端進行網絡校時。

notrust :客戶端除非經過認證,不然該客戶端來源將被視爲不信任子網

noquery :不提供客戶端的時間查詢:用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器

notrap :不提供trap遠端登錄:拒絕爲匹配的主機提供模式 6 控制消息陷阱服務。陷阱服務是 ntpdq 控制消息協議的子系統,用於遠程事件日誌記錄程序。

nopeer :用於阻止主機嘗試與服務器對等,並容許欺詐性服務器控制時鐘

kod : 訪問違規時發送 KoD 包。

restrict -6 表示IPV6地址的權限設置。

1:設定NTP主機來源(其中prefer表示優先主機),192.168.7.49是本地的NTP服務器,因此優先指定從該主機同步時間。

server 192.168.7.49 prefer 
 
server 0.rhel.pool.ntp.org iburst
 
server 1.rhel.pool.ntp.org iburst
 
server 2.rhel.pool.ntp.org iburst
 
server 3.rhel.pool.ntp.org iburst

clip_image001

 

2:限制你容許的這些服務器的訪問類型,在這個例子中的服務器是不允許修改運行時配置或查詢您的Linux NTP服務器

 

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

在上例中,掩碼地址擴展爲255,所以從192.168.0.1-192.168.0.254的服務器均可以使用咱們的NTP服務器來同步時間

 

#此時表示限制向從192.168.0.1-192.168.0.254這些IP段的服務器提供NTP服務。

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery

 

#設置默認策略爲容許任何主機進行時間同步

restrict default ignore

 

3:確保localhost(這個經常使用的IP地址用來指Linux服務器自己)有足夠權限.使用沒有任何限制關鍵詞的語法:

restrict 127.0.0.1

restrict -6 ::1

 

B:配置/etc/ntp/stpe-tickers文件

修改/etc/ntp/stpe-tickers文件,內容以下(當ntpd服務啓動時,會自動與該文件中記錄的上層NTP服務進行時間校對)

[root@localhost ntp]# more /etc/ntp/step-tickers 
# List of servers used for initial synchronization.
[root@localhost ntp]# vi /etc/ntp/step-tickers 
# List of servers used for initial synchronization.
server 192.168.7.49 prefer
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org 
server 2.rhel.pool.ntp.org 
server 3.rhel.pool.ntp.org 

關於ntp.conf and step-tickers區別:

step-tickers is used by ntpdate where as ntp.conf is the configuration file for the ntpd daemon. ntpdate is initially run to set the clock before ntpd to make sure time is within 1000 sec. ntp will not run if the time difference between the server and client by more then 1000 sec ( or there about). The start up script will read step-tickers for servers to be polled by ntpdate.

C:配置/etc/sysconfig/ntpd文件

ntp服務,默認只會同步系統時間。若是想要讓ntp同時同步硬件時間,能夠設置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可讓硬件時間與系統時間一塊兒同步。

#容許BIOS與系統時間同步,也能夠經過hwclock -w 命令

SYNC_HWCLOCK=yes

 

IPTABLES 配置

因爲NTP服務須要使用到UDP端口號123,因此當系統的防火牆(Iptables)啓動的狀況下,必須開放UDP端口號123。

[root@localhost ~]#  /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
 
[root@localhost ~]# /sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT
[root@localhost ~]#  /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:123 
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
 
[root@localhost ~]# 

clip_image002

若是防火牆沒有開放UDP端口號123,有可能出現下面狀況。

[root@localhost ~]# /usr/sbin/ntpq -c rv | grep stratum

stratum=16, precision=-24, rootdelay=0.000, rootdisp=3.525, refid=INIT,

[root@localhost~]#

A stratum level of 16 indicates that NTP is not synchronizing correctly.If a stratum level of 16 is detected, wait 15 minutes and issue the command again. It may take this long for the NTP server to stabilize.If NTP continues to detect a stratum level of 16, verify that the NTP port (UDP Port 123) is open on all firewalls between the cluster and the remote machine you are attempting to synchronize to.

 

 

啓動NTP服務

 

 

[root@localhost ~]# service ntpd status
ntpd is stopped
[root@localhost ~]# service ntpd start
Starting ntpd: [  OK  ]
[root@localhost ~]# 
 
service ntpd status      #查看ntpd服務狀態
service ntpd start           #啓動ntpd服務
service ntpd stop            #中止ntpd服務
service ntpd restart         #重啓ntpd服務

檢查ntp服務是否開機啓動,將其設置爲開機啓動。

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

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

[root@localhost ~]# runlevel

N 3

[root@localhost ~]# chkconfig ntpd on #在運行級別二、三、四、5上設置爲自動運行

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

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

[root@localhost ~]#

若是要設置在運行級別上自動運行,可使用下面命令

chkconfig --level 345 ntpd on

能夠用下面命令檢測NTP服務是否運行

[root@localhost ~]# pgrep ntpd 
2639
2641
[root@localhost ~]# netstat -tlunp | grep ntp   #若是看到123端口,說明ntp服務成功啓動。
udp        0      0 192.168.7.224:123           0.0.0.0:*                               2639/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2639/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2639/ntpd           
udp        0      0 fe80::250:56ff:feb3:b5:123  :::*                                    2639/ntpd           
udp        0      0 ::1:123                     :::*                                    2639/ntpd           
udp        0      0 :::123                      :::*                                    2639/ntpd           
[root@localhost ~]# 

clip_image003

查看ntp服務器有無和上層ntp連通

 

[root@localhost ~]# ntpstat
synchronised to NTP server (192.168.7.49) at stratum 6 
   time correct to within 440 ms
   polling server every 128 s
[root@localhost ~]# 

查看ntp服務器與上層ntp的狀態

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u   13   64    3    5.853  1137178   2.696
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u   17   64    3    5.853  1137178   2.696
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u    1   64    1    0.937   -9.570   0.000

clip_image004

remote   - 本機和上層ntp的ip或主機名,「+」表示優先,「*」表示次優先

refid    - 參考上一層ntp主機地址

st       - stratum階層

when     - 多少秒前曾經同步過期間

poll     - 下次更新在多少秒後

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

delay    - 網絡延遲

offset   - 時間補償

jitter   - 系統時間與bios時間差

要查看 ntpd 進程的狀態,請運行如下命令,按 Ctrl+C 中止查看進程。

clip_image005

第一列中的字符指示源的質量。星號 ( * ) 表示該源是當前引用。

remote 列出源的 IP 地址或主機名。

when   指出從輪詢源開始已過去的時間(秒)。

poll   指出輪詢間隔時間。該值會根據本地時鐘的精度相應增長。

reach  是一個八進制數字,指出源的可存取性。值 377 表示源已應答了前八個連續輪詢。

offset 是源時鐘與本地時鐘的時間差(毫秒)。

 

ntpd、ntpdate的區別

下面是網上關於ntpd與ntpdate區別的相關資料。以下所示所示:

使用以前得弄清楚一個問題,ntpd與ntpdate在更新時間時有什麼區別。ntpd不只僅是時間同步服務器,它還能夠作客戶端與標準時間服務器進行同步時間,並且是平滑同步,並不是ntpdate當即同步,在生產環境中慎用ntpdate,也正如此二者不可同時運行。

時鐘的躍變,對於某些程序會致使很嚴重的問題。許多應用程序依賴連續的時鐘——畢竟,這是一項常見的假定,即,取得的時間是線性的,一些操做,例如數據庫事務,一般會地依賴這樣的事實:時間不會往回跳躍。不幸的是,ntpdate調整時間的方式就是咱們所說的」躍變「:在得到一個時間以後,ntpdate使用settimeofday(2)設置系統時間,這有幾個很是明顯的問題:

第一,這樣作不安全。ntpdate的設置依賴於ntp服務器的安全性,攻擊者能夠利用一些軟件設計上的缺陷,拿下ntp服務器並令與其同步的服務器執行某些消耗性的任務。因爲ntpdate採用的方式是跳變,跟隨它的服務器沒法知道是否發生了異常(時間不同的時候,惟一的辦法是以服務器爲準)。

第二,這樣作不精確。一旦ntp服務器宕機,跟隨它的服務器也就會沒法同步時間。與此不一樣,ntpd不只可以校準計算機的時間,並且可以校準計算機的時鐘。

第三,這樣作不夠優雅。因爲是跳變,而不是使時間變快或變慢,依賴時序的程序會出錯(例如,若是ntpdate發現你的時間快了,則可能會經歷兩個相同的時刻,對某些應用而言,這是致命的)。於是,惟一一個能夠令時間發生跳變的點,是計算機剛剛啓動,但尚未啓動不少服務的那個時候。其他的時候,理想的作法是使用ntpd來校準時鐘,而不是調整計算機時鐘上的時間。

NTPD 在和時間服務器的同步過程當中,會把 BIOS 計時器的振盪頻率誤差——或者說 Local Clock 的天然漂移(drift)——記錄下來。這樣即便網絡有問題,本機仍然能維持一個至關精確的走時。

轉自:https://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html

相關文章
相關標籤/搜索