我在基于 ARM 的嵌入式系统上运行 Ubuntu,该系统缺少电池支持的 RTC。唤醒时间在 1970 年的某个时间。因此,我使用 NTP 服务将时间更新为当前时间。
我将以下行添加到/etc/rc.local
文件中:
sudo ntpdate -s time.nist.gov
Run Code Online (Sandbox Code Playgroud)
但是,启动后,仍然需要几分钟才能更新时间,在此期间我无法有效地使用tar
和make
。
如何在任何给定时间强制更新时钟?
更新 1:以下(感谢 Eric 和 Stephan)在命令行中工作正常,但在放入时无法更新时钟/etc/rc.local
:
$ date ; sudo service ntp stop ; sudo ntpdate -s time.nist.gov ; sudo service ntp start ; date
Thu Jan 1 00:00:58 UTC 1970
* Stopping NTP server ntpd [ OK ]
* Starting NTP server [ OK ]
Thu Feb 14 18:52:21 UTC 2013
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
更新 2:我尝试遵循针对第一次更新而提出的一些建议,但似乎实际上没有按要求完成这项工作。这是我尝试过的:
us.pool.ntp.org
ntp
干脆服务和刚刚离开sudo ntpdate ...
的rc.local
sudo
从上面的命令中删除rc.local
使用上面的方法,机器仍然从 1970 年开始。但是,一旦登录(通过ssh
)从命令行执行此操作时,一旦我调用 ,时钟就会更新ntpdate
。
我做的最后一件事是从我的文件中删除它rc.local
并调用它。这确实按预期更新了时钟,一旦命令提示符可用,我就会得到真实的当前时间。ntpdate
.bashrc
然而,这意味着如果机器打开并且没有用户登录,那么时间永远不会得到更新。当然,我可以重新安装该ntp
服务,以便至少在启动后的几分钟内更新时钟,但随后我们又回到了第 1 方格。
那么,是否有理由将ntpdate
命令放入rc.local
并不能执行所需的任务,而在.bashrc
正常情况下这样做呢?
Mar*_*der 543
sudo service ntp stop
sudo ntpd -gq
sudo service ntp start
Run Code Online (Sandbox Code Playgroud)
该-gq
告诉NTP守护进程来校正时间无论偏移(的g
立即)和出口(q
设定时间后)。
Eri*_*lho 358
可能ntp
服务正在运行,这就是为什么ntpdate
无法打开套接字(端口 123 UDP)并连接到 ntp 服务器的原因。
从命令行尝试:
sudo service ntp stop
sudo ntpdate -s time.nist.gov
sudo service ntp start
Run Code Online (Sandbox Code Playgroud)
如果您想/etc/rc.local
使用它,请执行以下操作:
( /etc/init.d/ntp stop
until ping -nq -c3 8.8.8.8; do
echo "Waiting for network..."
done
ntpdate -s time.nist.gov
/etc/init.d/ntp start )&
Run Code Online (Sandbox Code Playgroud)
小智 66
使用 sntp 立即设置时间。其手册页中的一些示例:
USAGE
sntp ntpserver.somewhere
is the simplest use of this program and can be run as an unprivileged command to check the current time and error in the local clock.
sntp -Ss -M 128 ntpserver.somewhere
With suitable privilege, run as a command or from a cron(8) job, sntp -Ss -M 128 ntpserver.somewhere will request the time from the server, and if that server reports that it is
synchronized then if the offset adjustment is less than 128 milliseconds the correction will be slewed, and if the correction is more than 128 milliseconds the correction will
be stepped.
sntp -S ntpserver.somewhere
With suitable privilege, run as a command or from a cron(8) job, sntp -S ntpserver.somewhere will set (step) the local clock from a synchronized specified server, like the (dep?
recated) ntpdate(8), or rdate(8) commands.
Run Code Online (Sandbox Code Playgroud)
它适用于任何 ntp 时间服务器。可以在 ntppool.org 上找到方便的服务器列表。
您需要sudo权限,例如:
sudo sntp -Ss -M 128 0.de.pool.ntp.org
Run Code Online (Sandbox Code Playgroud)
小智 44
使用timedatectl
(systemd service unit) 设置时间。ntp
已弃用。
sudo systemctl restart systemd-timesyncd.service
Run Code Online (Sandbox Code Playgroud)
您可以通过阅读日志来检查更新时间journalctl -xe | tail
。
中的状态 timedatectl status
和配置/etc/systemd/timesyncd.conf
。
man timesyncd.conf
dfc*_*dfc 42
正如其他人指出的那样,最好的解决方案是指示 ntpd 忽略恐慌阈值,默认情况下为 1000 秒。您可以通过以下两种方式之一配置恐慌阈值:
/etc/default/ntp
并确保存在 -g 选项。tinker panic 0
在顶部到目前为止,这基本上是其他人推荐的,但是我认为您应该再采取一步。安装 fake-hwclock 程序:
# apt-get install fake-hwclock
fake-hwclock: Save/restore system clock on machines without working RTC hardware
Some machines don't have a working realtime clock (RTC) unit, or no
driver for the hardware that does exist. fake-hwclock is a simple set
of scripts to save the kernel's current clock periodically (including
at shutdown) and restore it at boot so that the system clock keeps at
least close to realtime. This will stop some of the problems that may
be caused by a system believing it has travelled in time back to
1970, such as needing to perform filesystem checks at every boot.
On top of this, use of NTP is still recommended to deal with the fake
clock "drifting" while the hardware is halted or rebooting.
Run Code Online (Sandbox Code Playgroud)
安装了 fake-hwclock 后,您的机器将不会以为又是 1970 年了。当您的机器启动时,它会将其时钟设置为上次重新启动/关闭期间写入的时间戳 fake-hwclock。这意味着您可以有一个稍微正确的时钟,以防启动时出现网络问题。
Ste*_*han 13
ntpdate 是一个不同于 net dameon 的程序。NTPDate 可能在启动时出错,因为 ntpd 正在该套接字上运行。
从命令行,运行
# sudo service ntp stop ; sudo ntpdate -s time.nist.gov ; sudo service ntp start
Run Code Online (Sandbox Code Playgroud)
您也可以一起卸载ntpd(apt-get remove ntp)并添加一个cron脚本以每小时左右使用ntpdate。
ntp 服务在此系统上可能对您没有任何意义,因此请先将其删除。
# sudo apt-get remove ntp
Run Code Online (Sandbox Code Playgroud)
现在添加命令:
ntpdate -sb time.nist.gov
Run Code Online (Sandbox Code Playgroud)
到 /etc/rclocal
重启。那个时候应该不错。
请注意,某些当前基于 Ubuntu 的系统现在默认情况下甚至不使用 NTP 服务。在我的 Linux Mint 19 (Ubuntu 18.04) 机器上,时间由systemd-timesyncd
.
因此,为了在失去同步后获得最新时间,我只需运行
sudo systemctl restart systemd-timesyncd
Run Code Online (Sandbox Code Playgroud)
从 15.04 开始,Ubuntu 默认使用 systemd。因此,像时间这样的关键系统是通过 systemd 进行管理的。要查找您的系统正在使用的服务,请运行类似
systemctl list-unit-files | grep -E 'ntp|timesyncd'
Run Code Online (Sandbox Code Playgroud)
对于 16.04 上的 TechJS,该服务是ntp
. 对于我自己在 Ubuntu 18.04(Mint 19)上的服务是systemd-timesyncd
. 有趣的是,我登录了我拥有的 16.04 服务器并且它systemd-timesyncd
也在使用。
小智 7
在 Debian / Mint / Ubuntu(或其他 Debian 衍生产品)系统上执行此操作的正确方法是使用该行
NTPD_OPTS="-g"
Run Code Online (Sandbox Code Playgroud)
在文件中
/etc/default/ntp
Run Code Online (Sandbox Code Playgroud)
这确保了当从 /etc/init.d/ntp 脚本启动 ntpd 时,它会使用“-g”选项运行,即
start-stop-daemon --start --quiet --oknodo --pidfile /var/run/ntpd.pid --startas /usr/sbin/ntpd -- -p /var/run/ntpd.pid -g -u 124:128
Run Code Online (Sandbox Code Playgroud)
当系统时间超过 1000 秒时,允许 ntpd 纠正系统时间,例如当系统时间是 1970 年 1 月 1 日启动时,因为没有硬件 RTC。
tlsdate
通过使用 TLS 安全地连接到远程服务器并从安全握手中提取远程时间来设置本地时钟。与 不同
ntpdate
,tlsdate
使用 TCP,例如连接到远程 HTTPS 或 TLS 启用的服务,并提供一些保护,防止攻击者试图向您提供恶意时间信息。
$ tlsdate -V -n -H encrypted.google.com
Run Code Online (Sandbox Code Playgroud)
看看这个(没有合适的,没有服务):
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
Run Code Online (Sandbox Code Playgroud)
为解决这个问题的Prashant 干杯