Raspberry Pi開發之旅-同步時間

來源:互聯網
上載者:User

標籤:delay   content   ora   lock   rate   mon   更新時間   ==   應用   

使用htpdate同步時間

由於樹莓派板子上沒有 RTC 硬體和電池,因此樹莓派上的系統時間重啟是儲存不了的。網上已經有人想到應對 NTP 被防火牆封掉類似的需求了,開源的 htpdate 命令直接使用 HTTP 協議來進行系統時間同步,項目首頁在這裡:

https://github.com/iridium77/htpdate

htpdate 的原理也非常簡單,直接解析 HTTP 協議頭中的伺服器時間資訊,然後設定本地時間,我們來看百度返回的 HTTP 頭:

HTTP/1.1 200 OKDate Mon, 13 Oct 2014 16:05:18 GMTContent-Type text/htmlTransfer-Encoding chunkedConnection Keep-AliveCache-Control privateExpires Mon, 13 Oct 2014 16:05:18 GMTServer BWS/1.1BDPAGETYPE 2BDQID 0x8b40c1f700000bd4BDUSERID 13923551Set-Cookie BDSVRTM=133; path=/Set-Cookie BD_HOME=1; path=/

上面的 Date Mon, 13 Oct 2014 16:05:18 GMT 就是百度的 Web 服務器上的系統時間了。

htpdate 命令做時間同步會有 0.5 秒左右的誤差(看看 HTTP 頭就知道裡面很多時間都是以秒為單位哈),對於我的樹莓派來說就完全沒有關係,如果對這個比較在意的話只能盡量用 NTP 時間同步了。

htpdate 使用上也很簡單,簡單編譯安裝之後把這條命令加到 Raspberry Pi Raspbian 系統的 /etc/rc.local 檔案中(不加 -t 參數基本不能同步成功哦):

htpdate -t -s ntp.neu.edu.cn

然後重啟樹莓派就可以正常同步系統時間了。

使用區域網路NTP伺服器同步時間

1、配置區域網路NTP伺服器

:https://www.meinbergglobal.com/english/sw/ntp.htm
按預設步驟安裝即可。

 

2、修改設定檔ntp.conf

設定檔預設路徑為:C:\Program Files (x86)\NTP\etc\ntp.conf
去掉

 

  1. #server 127.127.1.0  
  2. #fudge 127.127.1.0 stratum 12 

前的#
儲存

筆者改完後的設定檔內容如下

  1. # NTP Network Time Protocol   
  2. # **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes*   
  3. # PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED   
  4. # Configuration File created by Windows Binary Distribution Installer Rev.: 1.27  mbg  
  5. # please check http://www.ntp.org for additional documentation and background information  
  6. # restrict access to avoid abuse of NTP for traffic amplification attacks   
  7. # see http://news.meinberg.de/244 for details    
  8. restrict default noquery nopeer nomodify notrap    
  9. restrict -6 default noquery nopeer nomodify notrap    
  10.    
  11. # allow status queries and everything else from localhost   
  12. restrict 127.0.0.1   
  13. restrict -6 ::1   
  14.    
  15. # if you need to allow access from a remote host, you can add lines like this:   
  16. # restrict <IP OF REMOTE HOST>   
  17.    
  18. # Use drift file   
  19. driftfile "D:\Program Files (x86)\NTP\etc\ntp.drift"  
  20.   
  21. # your local system clock, could be used as a backup  
  22. # (this is only useful if you need to distribute time no matter how good or bad it is)  
  23. server 127.127.1.0  
  24. # but it should operate at a high stratum level to let the clients know and force them to  
  25. # use any other timesource they may have.  
  26. fudge 127.127.1.0 stratum 12  
  27.   
  28. # Use a NTP server from the ntp pool project (see http://www.pool.ntp.org)  
  29. # Please note that you need at least four different servers to be at least protected against  
  30. # one falseticker. If you only rely on internet time, it is highly recommended to add  
  31. # additional servers here.   
  32. # The ‘iburst‘ keyword speeds up initial synchronization, please check the documentation for more details!  
  33.  server 0.asia.pool.ntp.org iburst minpoll 6 maxpoll 7  
  34.  server 1.asia.pool.ntp.org iburst minpoll 6 maxpoll 7  
  35.  server 2.asia.pool.ntp.org iburst minpoll 6 maxpoll 7  
  36.  server 0.us.pool.ntp.org iburst minpoll 6 maxpoll 7  
  37.  server 1.us.pool.ntp.org iburst minpoll 6 maxpoll 7  
  38.  server 2.us.pool.ntp.org iburst minpoll 6 maxpoll 7  
  39.   
  40.   
  41. # End of generated ntp.conf --- Please edit this to suite your needs 

3、重啟服務

電腦--右鍵 管理--服務與應用程式--服務,找到Network Time Protocol Daemon,右鍵重啟
或者通過開始菜單重啟。兩者作用一樣
開始--Meinberg--Network Time Protocol--Service Control--Restart NTP Service

4、本地測試

命令列輸入
ntpq -p
結果中的第一行如果出現LOCAL,說明NTP伺服器處理序存在

  1. Microsoft Windows [版本 6.3.9600]  
  2. (c) 2013 Microsoft Corporation。著作權所有,並保留一切權利。  
  3.   
  4. C:\Windows\system32>ntpq -p  
  5.      remote           refid      st t when poll reach   delay   offset  jitter  
  6. ==============================================================================  
  7.  LOCAL(0)        .LOCL.          12 l  201   64   10    0.000    0.000   0.000  
  8. +ntp2.aliyun.com 10.137.38.86     2 u   61   64    7   55.655    1.918   7.038  
  9. *118.189.211.186 .PPS.            1 u   62   64    7  117.009   10.206   3.703  
  10. +shim.active-app 218.186.3.36     2 u  124   64    2  105.239   -4.597   2.797  
  11. -104.156.99.226  192.12.19.20     2 u   61   64    5  256.067   -2.231   7.130  
  12. -y.ns.gin.ntt.ne 249.224.99.213   2 u   10   64    5  138.076   11.235   8.025  
  13.   
  14. C:\Windows\system32> 

5、添加防火牆例外

將ntp.exe添加到防火牆例外或者將UDP的123連接埠添加到防火牆例外

6、再次重啟伺服器

7、配置樹莓派同步時間

首先熟悉幾個關於時間命令

  1. date #查看目前時間  
  2. date -s "2016-03-31 10:18:00" #設定目前時間為2016年3月31日10:18:00  
  3. date -s 2016-03-31 #設定當前日期為2016年3月31日0:00:00  
  4. date -s 10:18:00 #設定目前時間為10:18:00

8、安裝ntpdate

sudo apt-get install ntpdate

9、對時

sudo ntpdate 172.26.69.87

其中172.26.69.87為區域網路NTP伺服器的IP地址
對時後可用date命令查看時間

tip1:如果遇到the NTP socket is in use, exiting的提示,這是因為ntpd也是用的UDP123連接埠更新時間,我們先將ntp這個服務關掉

sudo service ntp stop

然後再執行

sudo ntpdate 172.26.69.87 

tip2:如果遇到no server suitable for synchronization found的提示,多半是因為網路不通或者對應的NTP伺服器沒有啟動。

10、修改時區

預設情況下樹莓派使用的是UTC時間,與北京時間相差8小時,所以需要修改時區
tzselect命令並不能真正的修改
正確的做法是替換掉時區檔案

sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 

修改之後用date查看到的是CST時間

11、添加區域網路NTP伺服器位址

修改設定檔ntp.conf

sudo nano /etc/ntp.conf

在server項目前面添加如下內容

  1. server 172.26.69.87 prefer  
  2. server 192.168.42.254 iburst  
  3. server cn.pool.ntp.org iburst  
  4. server asia.pool.ntp.org iburst  
  5. server pool.ntp.org iburst 

這個設定檔用於ntpd程式同步時間,每次樹莓派開機啟動後都會啟動這個程式,同步的時間需要5分鐘。

12、查看時間

date

13、配置開機自啟

在實際運行中,如果電腦的時間與網路時間相差超過30分鐘,那麼ntpd就不會自動同步了,筆者處於每天斷電7~8個小時的校園網,所以每次開機必須先用ntpdate強制同步時間,但這個進程不能執行得太早,太早的話可能還沒連網。所以加了個延遲40秒啟動。

編輯

sudo nano /usr/bin/synctime 

內容

  1. #! /bin/sh  
  2.   
  3. #延遲40秒啟動  
  4. sleep 40s  
  5.   
  6. #停止ntpd服務  
  7. killall ntpd  
  8.   
  9. #對時  
  10. ntpdate -u 202.199.131.1  
  11.   
  12. #開啟ntpd伺服器  
  13. ntpd -c /etc/ntp.conf 

儲存
修改指令碼執行許可權

sudo chmod a+x /usr/bin/synctime 

加入開機啟動

sudo nano /etc/rc.local

#在exit 0前面添加

sudo /usr/bin/synctime > /dev/null 2>&1

儲存
修改執行許可權

sudo chmod +x /etc/rc.local

重啟

reboot

附:

DJTU內網推薦NTP配置

  1. server 222.26.224.216 prefer  
  2. server 202.199.131.1 iburst  
  3. server 202.120.2.100 iburst  
  4. server cn.pool.ntp.org iburst 

 

Raspberry Pi開發之旅-同步時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.