解決linux上的時間同步問題:NTP__linux

來源:互聯網
上載者:User

本篇文章主要講的是自己在工作中遇到的問題及如何解決,對於什麼是NTP和如何使用NTP這些概念都沒有涉及,因為這些都是網上有的內容,我也是參考的這些內容。

在windows下自己下載了ntp-4.2.4p4.tar.gz壓縮包,按照http://www.51testing.com/html/28/116228-202637.html這篇文章,在Ubuntu虛擬機器中解壓,編譯和安裝之後,但是並沒有在/etc目錄下產生ntp.conf設定檔,因此也就無從談起來修改設定檔了。

於是按照這篇文章的方法:http://www.latelee.org/using-gnu-linux/ubuntu-ntp-note.html

伺服器端的NTP:

在公司的生產環境下操作:

在想要作為伺服器的機器上執行sudo apt-get install ntp命令來安裝ntp,安裝完成後就發現在/etc/目錄下產生了

ntp.conf設定檔,接著對於NTP伺服器端的設定就按照這篇文章。

我的/etc/ntp.conf檔案的內容為:

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org

server 127.127.1.0
fudge 127.127.1.0 stratum 8

# Use Ubuntu's ntp server as a fallback.
server ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
#modified by zhangjianjun
#restrict -4 default kod notrap nomodify nopeer noquery
restrict -4 default noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust

# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient


NTP用戶端:

將自己的Ubuntu虛擬機器作為NTP的用戶端,然後在虛擬機器上解壓,編譯,安裝ntp-4.2.4p4.tar.gz之後就可以使用

ntpdate -d (10.0.xx.xx)(伺服器端的ip地址),想要執行成功的話,用戶端必須能夠ping通伺服器端的ip地址,

到此發現可以執行成功了。

linux@ubuntu:~$sudo ntpdate -d 10.0.24.25
29 Jan 14:33:10 ntpdate[6515]: ntpdate 4.2.6p3@1.2290-o Tue Jun  5 20:12:12 UTC 2012 (1)
Looking for host 10.0.24.25 and service ntp
host found : 10.0.24.25
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
server 10.0.24.25, port 123
stratum 3, precision -22, leap 00, trust 000
refid [10.0.24.25], delay 0.02652, dispersion 0.00002
transmitted 4, in filter 4
reference time:    de193b90.11525693  Mon, Jan 29 2018 14:20:00.067
originate timestamp: de193e0f.43fffb69  Mon, Jan 29 2018 14:30:39.265
transmit timestamp:  de193ebb.8f7b7817  Mon, Jan 29 2018 14:33:31.560
filter delay:  0.02652  0.02654  0.02654  0.02658 
         0.00000  0.00000  0.00000  0.00000 
filter offset: -172.295 -172.295 -172.295 -172.295
         0.000000 0.000000 0.000000 0.000000
delay 0.02652, dispersion 0.00002
offset -172.295461


29 Jan 14:33:33 ntpdate[6515]: step time server 10.0.24.25 offset -172.295461 sec

但是發現,用戶端的時間根本沒有發生變化,把用戶端的時間故意調錯,雖然執行成功的話,用戶端的時間也沒有矯正過來,
經過多次嘗試終於發現:按照如下的方法執行的話,就沒有問題了
先執行:sudontpdate -u 10.0.24.25
再執行:sudontpdate -d 10.0.24.25

執行完第二行命令後輸出的列印資訊有一些變化,見紅色的部分
29 Jan 15:13:04 ntpdate[6581]: ntpdate 4.2.6p3@1.2290-o Tue Jun  5 20:12:12 UTC 2012 (1)
Looking for host 10.0.24.25 and service ntp
host found : 10.0.24.25
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
receive(10.0.24.25)
transmit(10.0.24.25)
server 10.0.24.25, port 123
stratum 3, precision -22, leap 00, trust 000
refid [10.0.24.25], delay 0.02647, dispersion 0.00005
transmitted 4, in filter 4
reference time:    de1947b9.084ab653  Mon, Jan 29 2018 15:11:53.032
originate timestamp: de194815.5e2b1c2a  Mon, Jan 29 2018 15:13:25.367
transmit timestamp:  de194815.5ddecfc4  Mon, Jan 29 2018 15:13:25.366
filter delay:  0.02657  0.02655  0.02649  0.02647 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 0.000530 0.000550 0.000625 0.000664
         0.000000 0.000000 0.000000 0.000000
delay 0.02647, dispersion 0.00005
offset 0.000664

29 Jan 15:13:27 ntpdate[6581]: adjust time server10.0.24.25 offset 0.000664 sec

通過對比發現只有輸出列印資訊為adjust time server的時候才會生效。

具體為什麼這麼操作,我也不知道,這僅僅是無意間這麼操作的時候發現確實用戶端的時間跟用戶端進行了同步

在項目中又遇到一個問題:每一台機器上跑的linux系統的顯示的時區是不一樣的,但是因此即使我們時間通過成功了,但是

在顯示的時候時間也是不一樣的,因此就需要在linux下進行時區的轉換,我們先把每個linux系統上的時區都修改為相同的時區,然後

在進行時間同步即可。

參考連結:http://blog.51cto.com/13178102/2055701


到此本以為就改結束了,但是有一個問題,那就是我們的程式是運行在目標版上(mips架構)的,不是運行在x86架構上的。因此就不能使用

上述的方法來設定ntp server,因為不能再mips架構上的linux使用apt-get命令來安裝ntp server程式,必須找到源碼然後進行交叉編譯才行。

因此必須使用源碼來交叉編譯,通過實驗可知,使用之前下載的ntp-4.2.4p4.tar.gz(最好使用這個版本,因為我們實驗過好幾個版本,不是配置時出錯,就是編譯時間出錯,只有這個版本沒有任何問題)就可以。

正如前面說的,使用解壓,編譯,安裝的方法是得不到ntp.conf設定檔的,因此我就把之前使用sudo apt-get install ntp產生的設定檔保留了一份(如果使用apt-get remove來卸載已安裝的軟體時,會把所有的設定檔都給刪除了,之前我把/etc目錄下的ntp.conf拷貝了一份ntp.conf.bak,結果在卸載軟體時還是被刪除了),在這時拷貝的/etc目錄下,

安裝成功之後就會在指定的目錄下的bin目錄下產生一些可執行檔,比如ntpd, ntpdate.....,此時並不代表已經執行了ntp server服務,需要我們自己手動來啟動ntp server

執行如下命令:

/usr/local/ntp/bin/ntpd -c /etc/ntp.conf

/usr/local/ntp/bin/ntpd表示ntpd的絕對路徑,-c參數是指定設定檔,具體資訊的參數資訊可以使用-h參數來查看。

執行完執行使用ps -e | grep "ntpd" 就可以看到ntpd進行已經在後台運行了,注意,伺服器啟動後大約10分鐘才可以串連,否則用戶端同步時就會出現no server suitable for synchronization found。此時用戶端就可以進行同步時間了

僅參考這篇文章的伺服器部分,連結:http://www.51testing.com/html/28/116228-202637.html

其實我們也可以不使用ntp用戶端,也可以自己寫一個用戶端程式,然後進行編譯,直接運行也可以與ntp server進行時間同步。

下載地址:http://download.csdn.net/download/zhangjianjun521/10233029



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.