LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sync network time before starting DHCP service (https://www.linuxquestions.org/questions/linux-software-2/sync-network-time-before-starting-dhcp-service-4175607125/)

MALDATA 06-01-2017 01:44 PM

Sync network time before starting DHCP service
 
Hi all,

I'm writing an application that runs on a Raspberry Pi (Arch), which has no realtime clock. I'm having an issue using the Pi as a DHCP server. On boot, the Pi always thinks it's February 1, 2017. Shortly thereafter, the systemd-timesyncd daemon starts, and it sets the clock to the last time it knew about before its last shutdown. So, if I had the machine powered down for the weekend, the system clock would be a couple days behind. Not long after that, systemd-timesyncd syncs to an NTP server to actually set the system clock.

The problem is that the Pi's DHCP server can start independently of these things, and if it creates a DHCP lease when the Pi's time is behind by a few days (or all the way back to February) and then the time gets updated, the DHCP leases all expire.

In order to fix this, I've added an override to the DHCP systemd unit such that it starts AFTER time-sync.target. Upon doing this, I found out that systemd-timesyncd considers itself "started" after restoring the time from the last shutdown. In other words, if the Pi boots up after a weekend, it may still start systemd-timesyncd, set the time to two days ago, start DHCP, issue a DHCP lease, do an NTP sync, and then all the leases expire immediately.

What I want is to wait for the sync to an NTP server to actually occur after booting, and then start the DHCP server after that. Is anyone aware of a way to do this, or at the very least, a hacky way to make it work?

Thanks!

kentyler 06-01-2017 03:15 PM

You could append to the network startup script, just add the command:

ntpdate ip.of.time.server

Then once network starts it runs a time sync

michaelk 06-01-2017 05:33 PM

ntpdate is depreceated. Changing to ntp instead of timesyncd might fix your timing problem.

Just to confirm that you configured the Pi with a static IP address and that no DHCP client is running.

MALDATA 06-01-2017 09:53 PM

Thanks for your input!

I read up on ntpdate a bit more, and all it really does is call ntpd with an option to sync immediately and then quit. When both ntpd and ntpdate are enabled as services, by default it runs ntpdate (sync immediately and then quit), then ntpd (as a daemon) after that. So, I get the result I wanted by enabling them both and then overriding a few things in the unit files to adjust the time-sync.target to depend on ntpd instead of timesyncd.

Quote:

ntpdate is depreceated. Changing to ntp instead of timesyncd might fix your timing problem.
Yeah, timesyncd doesn't have a lot of configuration options. ntpd is needed. The documentation does indicate that ntpdate is deprecated and due to be replaced by a shell script, but it's good enough for now.

Quote:

Just to confirm that you configured the Pi with a static IP address and that no DHCP client is running.
Our Pi has two ethernet ports, one of which connects to the WAN and may have a static IP or it may get one via DHCP. The other ethernet port is connected to a LAN on which the Pi is the DHCP server.

Thanks, all!


All times are GMT -5. The time now is 04:07 PM.