简体   繁体   中英

Ubuntu force date and time if NTP can't update

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?

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 thislink

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM