Sysntpd, client side, how to get server address from dhcp lease?

Dear all,
I'm using openwrt 21.02.5 with built in sysntpd
my configuration is the following

config system
option hostname 'OpenWrt'
option timezone 'UTC'
option ttylogin '0'
option log_size '64'
option urandom_seed '0'

config timeserver 'ntp'
option enabled '1'
option enable_server '0'

i have removed the list of ntp servers because i want to use the ntp server broadcasted by my DHCP server.

I have no problems to sync ntp with internet, for example

'1.openwrt.pool.ntp.org'

but it does not sync if i remove the server list.

Edit:
to force sync, i use the following command

/etc/init.d/sysntpd restart

it works when internet ntp servers are configured.

Can anyone can help me please ? I'm not a king of network configurations ...

Thank's in advance.

Nico

Does the NTP server advertised from DHCP change too often?
I think the easiest way is to read the NTP server field in the DHCP packet and use it directly in the server list.

2 Likes

Dear Trendy
Thank's a lot for your response. Very interesting response !
Can you help me to acheieve this ?

I think the easiest way is to read the NTP server field in the DHCP packet and use it directly in the server list.

I know how to set the server address in the list with the uci command, but i have absolutely no idea how can i read the address in the DHCP network packet ...

Thank's in advance for your response.

Log into the router using ssh and install tcpdump.

opkg update; opkg install tcpdump

Run these commands (copy/paste the entire block and press Enter).

iface=$(ifstatus wan | grep \"device | awk '{ print $2 }' | sed 's/[",]//g'); \
tcpdump -nnvvti $iface src port 67 and dst port 68 | grep 'NTP Option' & PID=`pidof udhcpc`; \
kill -SIGUSR1 $PID; sleep 3; killall tcpdump

If there is an NTP server advertised by DHCP, you should be able to see its IP address.

root@OpenWrt:~# iface=$(ifstatus wan | grep \"device | awk '{ print $2 }' | sed 's/[",]//g'); \
> tcpdump -nnvvti $iface src port 67 and dst port 68 | grep 'NTP Option' & PID=`pidof udhcpc`; \
> kill -SIGUSR1 $PID; sleep 3; killall tcpdump
tcpdump: listening on eth0.2, link-type EN10MB (Ethernet), capture size 262144 bytes
1 packet captured
1 packet received by filter
0 packets dropped by kernel
            NTP Option 42, length 4: 192.168.1.1
4 Likes

Ouah! Great and very detailed explanation!
It works great! Thanks a lot

I will add the address in the sysntpd pool list and for sure it will work.

Do you know if an other NTP client deamon like chrony for exemple is able to synchronise automatically on DHCP broadcasted NTP address?

Best regards.

If I connect a 2nd OWRT router as client to my main OWRT router, the chrony on the 2nd router sees and uses the DHCP ntp info.

Following the output of "chronyc sources":

MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^+ 25000-036.cloud.services>     2   7   377    10   -626us[ -626us] +/-   35ms
^+ ernie.gerger-net.de           2   7   377   140  -1870us[-1630us] +/-   25ms
^+ srv.hueske-edv.de             2   7   377    12   -451us[ -451us] +/-   34ms
^+ odin.weavernet.at             2   7   377   206  -6318us[-6077us] +/-   59ms
^* 172.22.60.1                   3   7   377   111  +2241us[+2481us] +/-   30ms

172.22.60.1 is the IP of the main router.

2 Likes

The NTP servers should be fetched in the init script using ubus:

So, it looks like an error in the init script or a race condition.

2 Likes