生活随笔
收集整理的這篇文章主要介紹了
配置树莓派3和局域网NTP服务器实现内网时间校准
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、配置局域網NTP服務器 1.安裝ntp-4.2.8p5-win32-setup.exe 下載地址:https://www.meinbergglobal.com/english/sw/ntp.htm 按默認步驟安裝即可。
2.修改配置文件ntp.conf 配置文件默認路徑為:C:\Program Files (x86)\NTP\etc\ntp.conf 去掉
[plain] ?view plaincopy
#server?127.127.1.0?? #fudge?127.127.1.0?stratum?12?? 前的# 保存 筆者改完后的配置文件內容如下
[plain] ?view plaincopy
#?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服務器進程存在
[plain] ?view plaincopy
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.再次重啟服務 步驟同第三步
二、配置樹莓派同步時間 首先熟悉幾個關于時間命令
[plain] ?view plaincopy
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?? ?
1.安裝ntpdate [plain] ?view plaincopy
sudo?apt-get?install?ntpdate?? ?
2.對時 [plain] ?view plaincopy
sudo?ntpdate?172.26.69.87?? 其中172.26.69.87為局域網NTP服務器的IP地址 對時后可用date命令查看時間 tip1:如果遇到the NTP socket is in use, exiting的提示,這是因為ntpd也是用的UDP123端口更新時間,我們先將ntp這個服務關掉
[plain] ?view plaincopy
sudo?service?ntp?stop?? 然后再執行
[plain] ?view plaincopy
sudo?ntpdate?172.26.69.87?? tip2:如果遇到no server suitable for synchronization found的提示,多半是因為網絡不通或者對應的NTP服務器沒有啟動。
3.修改時區 默認情況下樹莓派使用的是UTC時間,與北京時間相差8小時,所以需要修改時區 tzselect命令并不能真正的修改 正確的做法是替換掉時區文件
[plain] ?view plaincopy
sudo?cp?/usr/share/zoneinfo/Asia/Shanghai?/etc/localtime?? 修改之后用date查看到的是CST時間
4.添加局域網NTP服務器地址 修改配置文件ntp.conf
[plain] ?view plaincopy
sudo?nano?/etc/ntp.conf?? 在server項目前面添加如下內容
[plain] ?view plaincopy
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分鐘。
5.查看時間 ?
[plain] ?view plaincopy
date?? ?
三、配置開機自啟 在實際運行中,如果計算機的時間與網絡時間相差超過30分鐘,那么ntpd就不會自動同步了,筆者處于每天斷電7~8個小時的校園網,所以每次開機必須先用ntpdate強制同步時間,但這個進程不能執行得太早,太早的話可能還沒聯網。所以加了個延遲40秒啟動。
編輯
[plain] ?view plaincopy
sudo?nano?/usr/bin/synctime?? 內容
[plain] ?view plaincopy
#!?/bin/sh?? ?? #延遲40秒啟動?? sleep?40s?? ?? #停止ntpd服務?? killall?ntpd?? ?? #對時?? ntpdate?-u?202.199.131.1?? ?? #開啟ntpd服務器?? ntpd?-c?/etc/ntp.conf?? 保存 修改腳本執行權限
[plain] ?view plaincopy
sudo?chmod?a+x?/usr/bin/synctime?? 加入開機啟動
[plain] ?view plaincopy
sudo?nano?/etc/rc.local?? #在exit 0前面添加
[plain] ?view plaincopy
sudo?/usr/bin/synctime?>?/dev/null?2>&1?? 保存 修改執行權限
[plain] ?view plaincopy
sudo?chmod?+x?/etc/rc.local???? 重啟
[plain] ?view plaincopy
reboot?? ?
?
附:
DJTU內網推薦NTP配置
?
[plain] ?view plaincopy
server?222.26.224.216?prefer?? server?202.199.131.1?iburst?? server?202.120.2.100?iburst?? server?cn.pool.ntp.org?iburst ?
轉載于:https://www.cnblogs.com/Pond-ZZC/p/6680257.html
總結
以上是生活随笔 為你收集整理的配置树莓派3和局域网NTP服务器实现内网时间校准 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。