如何使用 ntp 强制更新时钟?

ysa*_*sap 421 ntp

我在基于 ARM 的嵌入式系统上运行 Ubuntu,该系统缺少电池支持的 RTC。唤醒时间在 1970 年的某个时间。因此,我使用 NTP 服务将时间更新为当前时间。

我将以下行添加到/etc/rc.local文件中:

sudo ntpdate -s time.nist.gov
Run Code Online (Sandbox Code Playgroud)

但是,启动后,仍然需要几分钟才能更新时间,在此期间我无法有效地使用tarmake

如何在任何给定时间强制更新时钟?


更新 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:我尝试遵循针对第一次更新而提出的一些建议,但似乎实际上没有按要求完成这项工作。这是我尝试过的:

  1. 将服务器更换为 us.pool.ntp.org
  2. 使用程序的显式路径
  3. 取出ntp干脆服务和刚刚离开sudo ntpdate ...rc.local
  4. sudo从上面的命令中删除rc.local

使用上面的方法,机器仍然从 1970 年开始。但是,一旦登录(通过ssh)从命令行执行此操作时,一旦我调用 ,时钟就会更新ntpdate

我做的最后一件事是从我的文件中删除它rc.local并调用它。这确实按预期更新了时钟,一旦命令提示符可用,我就会得到真实的当前时间。ntpdate.bashrc

然而,这意味着如果机器打开并且没有用户登录,那么时间永远不会得到更新。当然,我可以重新安装该ntp服务,以便至少在启动后的几分钟内更新时钟,但随后我们又回到了第 1 方格。

那么,是否有理由将ntpdate命令放入rc.local并不能执行所需的任务,而在.bashrc正常情况下这样做呢?

Mar*_*der 543

代替 ntpdate (已弃用),使用ntpd

sudo service ntp stop
sudo ntpd -gq
sudo service ntp start
Run Code Online (Sandbox Code Playgroud)

-gq告诉NTP守护进程来校正时间无论偏移(的g立即)和出口(q设定时间后)。

  • 这个答案应该放在最上面,因为它是正确的:ntpdate 已被弃用,安装它是一个坏主意,因为它与 ntp 冲突。如果时钟偏离,您需要执行此手动步骤,否则 ntp 不会更改您的时钟,也不会告诉您原因。 (39认同)
  • 对我来说,`sudo ntpd -gq` 不会退出!我在 14.10 上,我必须按 CTRL+C 才能继续……或者需要多长时间? (34认同)
  • “-q”选项告诉 NTP 守护进程启动、设置时间并立即退出。“-g”选项允许它纠正大于 1000 秒的时间差异。从长远来看,您应该简单地将 NTP 守护程序配置为始终运行。 (12认同)
  • 仅供参考,在我的系统(CentOS 6.6)上,我需要将 `sudo service ntp...` 的两个实例更改为 `sudo service ntpd...`。 (6认同)
  • 谢谢。执行此命令后仍显示 1970(带 sudo)。阅读 `ntpd` 联机帮助页,我不确定这是如何强制更新的? (3认同)
  • 不幸的是,ntpdates 弃用是在有功能性替代 https://support.ntp.org/bin/view/Dev/DeprecatingNtpdate 之前宣布的,并且 ntpd -gq 在许多系统上失败。 (3认同)
  • 对我来说和@YanickRochon 一样;大约 3 分钟过去了,我不断收到“请求池服务器”消息 (2认同)
  • 注意 - 如果服务不存在 - 运行 ```sudo apt-get install ntp``` :-) (2认同)

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)

  • 使用 `-u` 选项,您不需要停止 ntp 服务:`sudo ntpdate -u time.nist.gov` (10认同)
  • 我确实发现 `us.pool.ntp.org` 响应更快。 (4认同)
  • 我真的不知道。:-) 我曾经尝试从 rc.local 和 cron 运行 `service` 时遇到了麻烦,但我设法使用 /etc/init.d/xxx 来修复它。实际上,我认为您不必提供 `ntpdate` 的完整路径,我喜欢在脚本中使用完整路径以确保找到正确的文件。 (2认同)
  • 好吧,这显然是问题所在。现在,一旦建立网络连接,就会更新时钟。谢谢。 (2认同)

小智 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)

  • 这在“sudo ntpd -gq”不起作用时起作用。 (8认同)

小智 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

参考

  • 其他答案非常不推荐使用,而且大多不起作用。这个答案应该是新接受的答案,因为它是最新的和最好的。唯一需要另一个答案的是使用 systemd 操作系统的用户。 (8认同)
  • `ntp` 未安装在 Ubuntu 18.04 及更高版本上,不应安装 (3认同)
  • 最佳答案!其他的已经过时了 (3认同)
  • 这适用于 Ubuntu 19.04。谢谢! (2认同)

dfc*_*dfc 42

正如其他人指出的那样,最好的解决方案是指示 ntpd 忽略恐慌阈值,默认情况下为 1000 秒。您可以通过以下两种方式之一配置恐慌阈值:

  • 编辑/etc/default/ntp并确保存在 -g 选项。
  • 编辑 /etc/ntp.conf 并放置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

重启。那个时候应该不错。

  • 是的,ntpdate 正在被淘汰。首选使用“ntpd -q”(两种变体都要求先停止 ntpd)。 (3认同)

小智 10

rdate -s tick.greyware.com
Run Code Online (Sandbox Code Playgroud)

如果您只想设置一次时钟,那么简单


Aar*_*ain 8

请注意,某些当前基于 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。


Kok*_*zzu 5

tlsdate通过使用 TLS 安全地连接到远程服务器并从安全握手中提取远程时间来设置本地时钟。与 不同 ntpdatetlsdate使用 TCP,例如连接到远程 HTTPS 或 TLS 启用的服务,并提供一些保护,防止攻击者试图向您提供恶意时间信息。

$ tlsdate -V -n -H encrypted.google.com
Run Code Online (Sandbox Code Playgroud)


Joa*_*him 5

看看这个(没有合适的,没有服务):

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 干杯