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

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

我在/etc/rc.local文件中添加了以下行:

 sudo ntpdate -s time.nist.gov 

然而,在启动之后,它仍然需要几分钟才能更新,在此期间我无法有效地使用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 

我究竟做错了什么?


更新2:我尝试遵循响应第一次更新时提出的一些建议,但似乎没有任何实际按要求完成工作。 这是我尝试过的:

  1. 将服务器替换为us.pool.ntp.org
  2. 使用程序的显式路径
  3. 完全删除ntp服务,并在rc.local保留sudo ntpdate ...
  4. rc.local的上述命令中删除sudo

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

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

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

那么,有没有理由为什么在rc.local中放置ntpdate命令不执行所需的任务,而在.bashrc这样做可以正常工作?

可能ntp服务正在运行,这就是ntpdate无法打开套接字(端口123 UDP)并连接到ntp服务器的原因。

从命令行尝试:

 sudo service ntp stop sudo ntpdate -s time.nist.gov sudo service ntp start 

如果要将其放在/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 )& 

而不是使用ntpdate( 不推荐使用 )

 sudo service ntp stop sudo ntpd -gq sudo service ntp start 

-gq告诉ntp守护进程无论偏移量( g )如何都要更正时间,并在设置时间后立即退出( q )。

使用sntp立即设置时间。 例如:

 sudo sntp -s 24.56.178.140 

-s之后的数字可以是任何ntp时间服务器,一个是Ft中的NIST。 柯林斯,科罗拉多州。

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

  • 编辑/etc/default/ntp并确保存在-g选项。
  • 编辑/etc/ntp.conf并将tinker panic 0置于顶部

到目前为止,这基本上是其他人推荐的,但我认为你应该采取更多的步骤。 安装假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. 

安装了假hwclock后,你的机器将无法重新开始考虑它是1970年。 当你的机器启动时,它会将其时钟设置为在上次重启/关闭期间写入的时间戳false-hwclock。 这意味着如果在启动时出现网络问题,您可以使用一个稍微正确的时钟。

ntpdate是一个与net dameon不同的程序。 NTPDate可能在启动时出错,因为ntpd正在该套接字上运行。

从命令行运行

 # sudo service ntp stop ; sudo ntpdate -s time.nist.gov ; sudo service ntp start 

你也可以一起卸载ntpd(apt-get remove ntp)并添加一个cron脚本,每小时左右使用一次ntpdate。

UPDATE

ntp服务在这个系统上可能没有任何有意义的价值,所以先删除它。

 # sudo apt-get remove ntp 

现在添加命令:

 ntpdate -sb time.nist.gov 

/etc/rclocal

重启。 应该擅长那一点。

 rdate -s tick.greyware.com 

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

在Debian / Mint / Ubuntu(或其他Debian衍生系统)系统上执行此操作的正确方法是使用该行

 NTPD_OPTS="-g" 

在文件中

 /etc/default/ntp 

这确保了当从/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 

允许ntpd在超过1000秒时更正系统时间,例如,当系统时间是1970年1月1日启动时,因为没有硬件RTC。

尝试使用-b选项来分步时间。

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

 $ tlsdate -V -n -H encrypted.google.com 

https://help.ubuntu.com/lts/serverguide/NTP.html#timedatectl

使用timedatectl设置时间,不推荐使用ntp。

ntpd算法丢弃超过128 ms的样本偏移,除非样本偏移的[绝对值]小于128 ms的间隔超过900 s。 之后的第一个样本,无论偏移是什么,都将时钟步进到指定的时间。 在实践中,这降低了误报率,其中时钟错误地步进到低的发生率。

通常,如果偏移量超过理智限制(默认值为1000秒),则ntpd将退出。 可以使用-g选项关闭此选项:

-g通常,如果偏移量超过理智限制(默认值为1000秒),则ntpd将退出。 如果将完整性限制设置为零,则不执行完整性检查,并且任何偏移都是可接受的。 此选项会覆盖限制并允许将时间设置为任何值而不受限制; 但是,这只会发生一次。 之后,如果超出限制,ntpd将退出。 此选项可与-q选项一起使用。

均来自http://doc.ntp.org/4.1.0/ntpd.htm

-Jonathan Natale

好,

我正在我的覆盆子pi上运行一个raspbian(debian wheezy),它没有hwclock。 我发现编写一个小脚本并在我的互联网接口启动后运行它是很方便的,所以我确信当网络可用时,时钟会更新。

首先通过运行检查您是否拥有ntpdate

 sudo apt-get update sudo apt-get install ntpdate 

您需要将以下内容添加到/etc/network/interfaces (这里的eth0只是一个示例):

 auto eth0 iface eth0 inet dhcp post-up /usr/local/sbin/update-time.sh 

并在/usr/local/sbin/update-time.sh创建以下脚本(不要忘记通过chmod使其可执行):

 #!/bin/bash # This script checks if the ntp process is running, stops it, updates the system time, starts it again ps cax | grep -c ntpd > /dev/null onoff=$? if [ "$onoff" -gt 0 ]; then echo "stopping ntpd..." service ntp stop echo "ntpd stopped" else echo "ntpd not running, ready to update the date" fi isinstalled=$(dpkg-query -l | grep -c ntpdate) if [ "$isinstalled" -gt 0 ]; then ntpdate -t 3 -s ntp4.stratum2.ru echo "date and time update executed" else echo "ntpdate package not installed, can't update using ntp" fi echo "restarting ntpd..." service ntp start echo "ntpd running" echo "printing current date and time:" date exit 

ntpdntpdate默认使用受限端口(UDP 123)运行。 如果您在防火墙后面, ntpd将永远不会工作,但ntpdate可以使用-u选项。 例如: ntpdate -u 0.ubuntu.pool.ntp.orgntpdate -u time.nist.gov应该都可以正常工作。

如果你有足够的时间等待系统同步所需的时间,你可以使用ntp-wait命令:

 ntp-wait || exit -1 # i believe this would exit the script if it fails, but not sure of ntp-wait return codes, learn this first. echo Time is synced, go ahead with backup tar rsync etc. 

如果您使用的是systemd,则可以使用此命令:

 sudo systemctl restart ntp # or sudo systemctl restart ntp.service 

然后时间在近10-15秒内更新。
在ubuntu伙伴16.04上测试过