
我有一個用於灌溉系統的樹莓派。問題是它沒有硬體時鐘,所以每次重新啟動時,系統時間都是錯誤的,我無法將其設定為自動同步。
我發現當系統時間錯誤時,dns 請求不起作用,所以每次我嘗試發送請求時,我都會得到:
[root@alarmpi me]# ntpdate 3.es.pool.ntp.org
Error resolving 3.es.pool.ntp.org: Name or service not known (-2)
12 Aug 15:37:15 ntpdate[2385]: Can't find host 3.es.pool.ntp.org: Name or service not known (-2)
12 Aug 15:37:15 ntpdate[2385]: no servers can be used, exiting
順便說一句,如果我嘗試使用 pacman、wget、python/requests,甚至 ping,我也會遇到相同的連接錯誤...所以這肯定是 dns 的事情。
[me@alarmpi ~]$ ping google.com
ping: google.com: Name or service not known
如果我手動設定時間和日期,它會再次工作,但我住的地方距離這個東西有 300 公里,而且我不能每天手動設定停電。
[root@alarmpi me]# timedatectl
Local time: Mon 2019-08-12 15:47:40 CEST
Universal time: Mon 2019-08-12 13:47:40 UTC
RTC time: n/a
Time zone: Europe/Madrid (CEST, +0200)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
[root@alarmpi me]# timedatectl set-ntp false
[root@alarmpi me]# timedatectl set-time "2019-08.13 10:45:45 CEST"
[root@alarmpi me]# ping google.com
PING google.com (172.217.168.174) 56(84) bytes of data.
64 bytes from 172.217.168.174 (172.217.168.174): icmp_seq=1 ttl=54 time=22.7 ms
64 bytes from 172.217.168.174 (172.217.168.174): icmp_seq=2 ttl=54 time=31.6 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 292ms
rtt min/avg/max/mdev = 22.739/27.161/31.583/4.422 ms
[root@alarmpi me]# ntpdate 3.es.pool.ntp.org
13 Aug 10:49:54 ntpdate[2498]: adjust time server 37.139.121.60 offset 0.004894 sec
所以我在這裡有點迷失。如果因為不同步而不允許我連接到服務,我該如何自動同步時鐘?
答案1
有些 NTP 伺服器直接偵聽 IP 位址並以此方式通告自己。例如,美國國家標準技術研究院為每個 NTP 伺服器提供 IP,因此您可以選擇一個並ntpdate <IP>
在 init 上執行操作。這不需要 DNS 解析,應該可以立即生效。
或者,您也可以嘗試使用該fake-hwclock
軟體包,它會保留重新啟動之間的大致時間。它並不完美,也不能提供準確的時間,但它可能足以解析 NTP 的網域。