How to use ntp to Set the Time and Date on Ubuntu

The proper way to set time is to use a time server. The one problem is that ntpd won’t update the time if the clock has drifted too far. This article explains how to get around that problem.

apt install ntp
service ntp stop
ntpd -q
service ntp start
systemctl enable ntp.service

The magic command is “ntpd -q”. That sets the system time from ntp, and then exits.  It must be run when ntpd isn’t running.

It must be run when network congestion is low!  If you’re doing videos or huge downloads and uploads, it probably won’t work.

Why does NTP sometimes fail to update the time?

ntpd’s normal mode of operation is to check the time on a network time server, and then adjust the local clock to match the server.

This adjustment isn’t done by setting the current time. Rather, it tracks the difference between the current system time, and the network’s time, and then adds additional microseconds, or withholds system ticks, in tiny amounts, until the OS’s clock matches up with the time server. It also changes the time on the hardware clock on the motherboard periodically.

However, if the clocks are more than 1000 seconds (17 minutes) away from the network time, ntpd will not update the clocks.

When the time differs substantially from the network time, the system admin needs to manually set the clock to the current time.  However, this is difficult, because you need to set the time to within a few seconds of the actual time, because the little adjustments ntp makes are so small that it takes many minutes to move the clock one second.

Time differences between can cause problems with network services that rely on relatively accurate time. So, while ntp can correct a few minutes difference, servers that have time differences that large are basically going to have problems.

So the system admin needs to set the time to within a few seconds of the actual time.

It’s a physically tedious, if not difficult, task.

Older folks will remember setting the time based on what the telephone time service that said “at the tone, the time will be…”. We’d sit there waiting for the top of the minute, set our watch to that time, and when we heard the tone, pressed the button that would start the clock (or zero out the seconds).

That’s why we need to use “ntpd -q”: it sets the time very close to the network time.

See Also

The ntpd manual mage.

A description of the clock discipline algorithm at the NTP reference site.