標籤: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
去掉
- #server 127.127.1.0
- #fudge 127.127.1.0 stratum 12
前的#
儲存
筆者改完後的設定檔內容如下
- # NTP Network Time Protocol
- # **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes*
- # PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED
- # Configuration File created by Windows Binary Distribution Installer Rev.: 1.27 mbg
- # please check http://www.ntp.org for additional documentation and background information
- # restrict access to avoid abuse of NTP for traffic amplification attacks
- # see http://news.meinberg.de/244 for details
- restrict default noquery nopeer nomodify notrap
- restrict -6 default noquery nopeer nomodify notrap
-
- # allow status queries and everything else from localhost
- restrict 127.0.0.1
- restrict -6 ::1
-
- # if you need to allow access from a remote host, you can add lines like this:
- # restrict <IP OF REMOTE HOST>
-
- # Use drift file
- driftfile "D:\Program Files (x86)\NTP\etc\ntp.drift"
-
- # your local system clock, could be used as a backup
- # (this is only useful if you need to distribute time no matter how good or bad it is)
- server 127.127.1.0
- # but it should operate at a high stratum level to let the clients know and force them to
- # use any other timesource they may have.
- fudge 127.127.1.0 stratum 12
-
- # Use a NTP server from the ntp pool project (see http://www.pool.ntp.org)
- # Please note that you need at least four different servers to be at least protected against
- # one falseticker. If you only rely on internet time, it is highly recommended to add
- # additional servers here.
- # The ‘iburst‘ keyword speeds up initial synchronization, please check the documentation for more details!
- server 0.asia.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 1.asia.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 2.asia.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 0.us.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 1.us.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 2.us.pool.ntp.org iburst minpoll 6 maxpoll 7
-
-
- # 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伺服器處理序存在
- Microsoft Windows [版本 6.3.9600]
- (c) 2013 Microsoft Corporation。著作權所有,並保留一切權利。
-
- C:\Windows\system32>ntpq -p
- remote refid st t when poll reach delay offset jitter
- ==============================================================================
- LOCAL(0) .LOCL. 12 l 201 64 10 0.000 0.000 0.000
- +ntp2.aliyun.com 10.137.38.86 2 u 61 64 7 55.655 1.918 7.038
- *118.189.211.186 .PPS. 1 u 62 64 7 117.009 10.206 3.703
- +shim.active-app 218.186.3.36 2 u 124 64 2 105.239 -4.597 2.797
- -104.156.99.226 192.12.19.20 2 u 61 64 5 256.067 -2.231 7.130
- -y.ns.gin.ntt.ne 249.224.99.213 2 u 10 64 5 138.076 11.235 8.025
-
- C:\Windows\system32>
5、添加防火牆例外
將ntp.exe添加到防火牆例外或者將UDP的123連接埠添加到防火牆例外
6、再次重啟伺服器
7、配置樹莓派同步時間
首先熟悉幾個關於時間命令
- date #查看目前時間
- date -s "2016-03-31 10:18:00" #設定目前時間為2016年3月31日10:18:00
- date -s 2016-03-31 #設定當前日期為2016年3月31日0:00:00
- 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項目前面添加如下內容
- server 172.26.69.87 prefer
- server 192.168.42.254 iburst
- server cn.pool.ntp.org iburst
- server asia.pool.ntp.org iburst
- server pool.ntp.org iburst
這個設定檔用於ntpd程式同步時間,每次樹莓派開機啟動後都會啟動這個程式,同步的時間需要5分鐘。
12、查看時間
date
13、配置開機自啟
在實際運行中,如果電腦的時間與網路時間相差超過30分鐘,那麼ntpd就不會自動同步了,筆者處於每天斷電7~8個小時的校園網,所以每次開機必須先用ntpdate強制同步時間,但這個進程不能執行得太早,太早的話可能還沒連網。所以加了個延遲40秒啟動。
編輯
sudo nano /usr/bin/synctime
內容
- #! /bin/sh
-
- #延遲40秒啟動
- sleep 40s
-
- #停止ntpd服務
- killall ntpd
-
- #對時
- ntpdate -u 202.199.131.1
-
- #開啟ntpd伺服器
- 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配置
- server 222.26.224.216 prefer
- server 202.199.131.1 iburst
- server 202.120.2.100 iburst
- server cn.pool.ntp.org iburst
Raspberry Pi開發之旅-同步時間