Pergunta

My application must run on a server with time synchronized. In order to do that I installed ntpd and I check the return value of ntpstat. It appears that it takes a very long time to get a zero return value from ntpstat, especially after reboot. Why does it take ntpd such a long period to get synchronized and how can I make it happen few seconds after reboot?

Foi útil?

Solução

Yes, as it was found - start up script invokes ntpd.service, which waiting for ntpdate.service to run. While ntpdate isn't service, but application - it running once via nice wrapper script, located at /usr/libexec/ntpdate-wrapper , which is checking conditions, where one of them - if there any servers, described at /etc/ntp/step-tickers . If yes - it invokes ntpdate with those servers, which is set up by default by fedora installer to 0.fedoratime.smth.ininternet , that is not fits closed environment.

So there 3 choices: 1. Modify ntpdate wrapper, that he will look first for ntp.conf 2. Modify /etc/ntp/step-tickers to have your own ntp server 3. Modify ntpdate.service to not invoke wrapper, but just invoke command with adding/replacing lines in its configuration:

[Service]
Type=oneshot
ExecStart=/usr/bin/ntpd -q -g -x
RemainAfterExit=yes

Outras dicas

To synchronize ntp immediately after reboot, ntpdate has to run before ntpd.

To do this in systemd, you can run

systemctl -a | grep ntp

to see if ntpdate is enabled. To enable it is just run

systemctl enable ntpdate

For Linuxes that use lsb scripts, you can enable ntpdate via chkconfig.

chkconfig ntpdate on

In 2021-05-07-raspios-buster-armhf-lite

there is no ntp binary and no ntpd.

I have an installation script that needs to run after the first boot. I also had these synchronization problems. To make sure that my installation script waits for time sync I did this:

STATE="no_time_sync"

while [ $STATE != "yes" ]; do
        # Check the current ntp sync status
        STATE=$(timedatectl status | grep "System clock synchronized" | awk '{print $4}')

        # Sleep for 3 seconds and wait for ntp sync
        sleep 3

done

echo "System clock synchronized"

Works like a charm for me:

May 07 16:00:34 raspberrypi setup[534]: Waiting for internet connection
May 07 16:00:39 raspberrypi setup[534]: Internet connection
Nov 05 08:32:32 raspberrypi setup[534]: System clock synchronized

At least in this Raspbian version, I'm not quite sure if the synchronization can be boosted anyhow. It already takes some time to setup the ethernet interface in order to reach out into the internet. And the ntp service is already enabled by default. I could decrease my sleep duration during my loops, though. Waiting for internet and waiting for clock sync have sleep times of 3 seconds. ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top