Ubuntu力量日期和时间,如果NTP可以更新

发布于 2025-02-09 17:29:03 字数 709 浏览 2 评论 0原文

我在Raspberry Pi 4上运行Ubuntu 20.04。由于它没有RTC,因此除非在网络上并可以到达NTP服务器,否则日期/时间无法更新。

有时,该系统会在网络上启动到可以进入NTP服务器的网络,但有时会启动并与网络断开并断开连接,或者在没有NTP服务器的封闭网络上。

有没有一种方法可以手动 force 当前日期和时间,但是保持NTP活动性,因此,如果下次启动系统时,如果它可以到达NTP服务器,它将同步?如果我尝试使用NTP Active设置时间(使用timeDatectl),我会得到错误:

Failed to set time: Automatic time synchronization is enabled

如果关闭NTP,我可以设置时间,但是一旦我重新启用NTP,重置上次同步的时间,失去了我刚刚设定的时间。

有什么办法解决的吗?保持NTP的活动状态(即使通过IT 当前也无法进入服务器),但手动使系统更准确地日期/时间?

更新

我认为我发现文件NTP正在使用该文件启动的日期/时间。看起来它是从/var/lib/systemd/timessync/clock中拉出的。如果关闭NTP,请设置我想要的日期/时间,touch此文件,然后重新启动NTP,它可以保留我设置的时间。

这是完成我想要的最佳方法吗?

I am running Ubuntu 20.04 on a Raspberry Pi 4. Since it doesn't have a RTC, the date/time can't update unless it is on the network and can get to an NTP server.

Sometimes this system is booted up on a network where it can get to an NTP server, but sometimes it is booted up and disconnected from a network, or on a closed network with no NTP servers.

Is there a way to manually force the current date and time, but keep NTP active so if next time the system is booted it will sync if it can get to an NTP server? If I try to set the time (using timedatectl) with NTP active I get the error:

Failed to set time: Automatic time synchronization is enabled

If I turn off NTP, I can set the time, but as soon as I re-enable NTP it seems to reset the time to when it was last synced, losing the time I just set.

Is there any way around this? Keep NTP active (even through it currently can't get to a server) but manually give the system a more accurate date/time?

UPDATE

I think I found the file NTP is using to get the date/time when it is started. It looks like it pulls this from /var/lib/systemd/timesync/clock. If I turn off NTP, set the date/time I want, touch this file, and then restart NTP, it keeps the time I set.

Is this the best way to accomplish what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2025-02-16 17:29:04

如果要同步日期:

#! /bin/bash

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

从此

Try this script (or standalone command) if you want to synchronize the date:

#! /bin/bash

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

Taken from this link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文