无网条件下同步时间 - kernel panic

无网条件下同步时间

这多少有点标题党了,指的是没有因特网或者UDP被阻断的情况下同步时间。
因为大多数HTTPS协议要用到TLS,时间差异太大会握手失败(timestamp is error),校园网在没登录时不能连接到NTP服务器同步时间,导致树莓派上很多服务'会谢'(entered failed state),经过摸索想到用Http中的字段来同步时间。

HTTP响应中的时间

HTTP请求中有Date字段,是Web服务器的时间,比如请求百度

C:\Users\lyl>curl  --head www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Thu, 31 Mar 2022 16:03:20 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

* Connection #0 to host www.baidu.com left intact

内网服务器上的时间

内网的时间不一定准确,比如我学校内网的1.1.1.1(不是CF的DNS,学校劫持作为登录页面),与标准时间差了几分钟,大概这台电脑是不联网同步时间的

C:\Users\lyl>curl -v --head 1.1.1.1
* Rebuilt URL to: 1.1.1.1/
*   Trying 1.1.1.1...
* TCP_NODELAY set
* Connected to 1.1.1.1 (1.1.1.1) port 80 (#0)
> HEAD / HTTP/1.1
> Host: 1.1.1.1
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
HTTP/1.1 302 Moved Temporarily
< Server: axe_bras/1.0
Server: axe_bras/1.0
< Date: Thu, 31 Mar 2022 15:48:26 GMT
Date: Thu, 31 Mar 2022 15:48:26 GMT

解决方法,再找一台时间比较准的机器,比如10.2.132.70

C:\Users\lyl>curl -v --head 10.2.132.70
* Rebuilt URL to: 10.2.132.70/
*   Trying 10.2.132.70...
* TCP_NODELAY set
* Connected to 10.2.132.70 (10.2.132.70) port 80 (#0)
> HEAD / HTTP/1.1
> Host: 10.2.132.70
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
HTTP/1.1 404 Not Found
< Content-Length: 0
Content-Length: 0
< Server: Microsoft-IIS/10.0
Server: Microsoft-IIS/10.0
< X-Powered-By: ASP.NET
X-Powered-By: ASP.NET
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< Date: Thu, 31 Mar 2022 15:56:10 GMT
Date: Thu, 31 Mar 2022 15:56:10 GMT
< Connection: close

设置系统时间

date -s "`curl --head -s 10.2.132.70 | grep -i "Date: " | cut -d' ' -f2-`"

添加新评论

电子邮件地址不会被公开,评论内容可能需要管理员审核后显示。