11

I need to set ntpd to sync time as often as possible, how to do that?

UPD I need to make ntpd adjust system clock more frequently, not poll servers. It's necessary because system clock of my virtual PC drifts too much
maxpoll doesn't seem to change anything, because I set minpoll/maxpoll to 4 and 5, and time adjustments seem to be made only once in several minutes

user626528
  • 220
  • 2
  • 3
  • 12

3 Answers3

21

ntpd doesn't sync time periodically the way you think it does, see here for more information (read the whole thing, it's all important. Short version: ntpd syncs the time and tells the system how fast it's drifting away from "true" time, the system then adjusts its clock frequency to properly track "true" time. Within a day most systems are tracking to within 5ms of the reference).

Absent an EXCELLENT reason you should not be mucking about with the way ntpd adjusts and disciplines your clock - these algorithms have been thoroughly tested and debugged, and given an adequate time reference will produce very small error rates.
Frankly if you have such an EXCELLENT reason you already know what it is, and you have a substantial budget to hire an expert to make these modifications for you ("you wouldn't be asking this question if you really needed this").

If you require better time accuracy than what ntpd can get you on its own synchronizing against a machine over the network you need a specialized precision hardware reference clock attached locally to the machine. High-Frequency trading and picosecond-critical scientific experiments are about the only things I can think of that require this kind of precision.

voretaq7
  • 79,879
  • 17
  • 130
  • 214
  • @ChrisS that's a pretty wide band - you may have temperature control problems in your datacenter – voretaq7 Apr 10 '12 at 15:21
  • 3
    We use Precision Time Protocol when it's really critical... http://en.wikipedia.org/wiki/Precision_Time_Protocol – ewwhite Apr 10 '12 at 15:26
  • 7
    -1 `algorithm or program that requires high precision and accuracy` != `mega corp with super massive budget`. – TheLQ Apr 16 '12 at 17:30
  • @TheLQ A GPS receiver (like Chris S mentioned in his answer) is also a precision reference, and does not require anything resembling a "super massive budget" (you can expect to spend anywhere from a hundred to a thousand bucks for one). Precision goes up from there, and cost increases along with it, typically on an exponential curve. Precise timekeeping has never been free/ – voretaq7 Apr 16 '12 at 17:41
  • 1
    One other use for having a local reference clock: high precision one-way network performance measurement. http://www.internet2.edu/performance/owamp/ – Dan Pritts Feb 15 '13 at 22:17
  • Actually it not always does. Virtual servers don't have any stable time "drift", it changes chaotically. – user626528 Feb 20 '13 at 05:51
  • @user626528 Virtual servers are a special case (the clock on a VM should be set and disciplined by the hypervisor) – voretaq7 Feb 20 '13 at 16:08
  • @voretaq7, it's not. – user626528 Jun 06 '13 at 04:21
  • The defaults do not work on a Raspberry Pi :( - In India where there are frequent power/internet loss, my date is January 1970 hours after Internet is restored. NTPd appears to do nothing about it and I am looking for a way to force it to update once Internet is restored, or just more frequently. – Roman Gaufman Jan 28 '16 at 12:24
  • 1
    @RomanGaufman You're asking ntpd to do something it's not designed to do (fix a clock that's wildly incorrect). You should run the `ntpdate` command (with the `-b` option) to set the clock initially, and then start the NTP daemon to *maintain* it (this can be part of your internet up/down scripts). If your connection or power are *wildly* unreliable you can also run `ntpdate` out of cron every 5 minutes, which may give "Good Enough" clock accuracy. (You may also consider investing in a hardware clock for your Raspberry Pi as an alternate solution. Google can help you find resources for that.) – voretaq7 Jan 28 '16 at 19:11
  • @voretaq7 I'm just giving an example where the defaults are not so sensible. In fact any environment where your system boots up after a power failure without initial access to the internet may leave your clock in an inaccurate state. Making ntpd sync time more frequently is a sensible thing to ask/change.There are no up/down scripts, it's just the gateway isn't providing internet yet at that point (and may not for a little while). Cron is an option but if NTPd could sync every hour or so, that would be a better and more sensible solution. – Roman Gaufman Jan 29 '16 at 20:08
  • @RomanGaufman and I'm pointing out that you're asking ntpd to do things it's not designed to: The ntp daemon disciplines the local clock. It assumes your network connection will be stable and your local clock will remain within a sane window if the network dies. It re-synchronizes with the time servers after a network failure *if the time delta between the local clock & the time server is still sane*, but if not it will fail because It doesn't (and can't) know if your clock is bad or the time server is. If ntpd behaved as you suggest it could do a huge amount of harm in the pathological case. – voretaq7 Jan 29 '16 at 20:49
  • @voretaq7 what harm would it be to tell it to always trust the time server and to check with the time server every hour or so? - Why is this something ntp was not designed to do? – Roman Gaufman Jan 31 '16 at 20:01
  • -1 there is battery stuff that makes your computer clock to be incorrect after sleep – Nick Jan 27 '20 at 23:02
12

The iburst option does several checks quickly as soon as the daemon is started and whenever the server is unreachable if you have that in your configuration. The burst option does several checks quickly whenever the server is reachable.

By default ntpd then follows an exponential backoff where the time between checks progressively increases, starting at 64 seconds and doubling each time, up to a maximum of 1024 seconds.

The maxpoll option allows you to set the maximum value higher or lower. This value is the power of 2 in seconds between checks, so for 1024 seconds, maxpoll is 10. There is also minpoll which is 6 by default and can be lowered to 4.

If you want anything more frequent than every 16 seconds you will probably need to use ntpdate instead of ntpd and run your own loop.

Please don't aim this at public time servers without contacting the maintainers first.

Ladadadada
  • 26,337
  • 7
  • 59
  • 90
  • 4
    Probably best that he doesn't ever use NTPdate, as it doesn't obey KOD or ICMP messages. – Chris S Apr 10 '12 at 14:59
  • Further reading on KoD and how to run a good NTP client in general: http://en.wikipedia.org/wiki/NTP_server_misuse_and_abuse – Ladadadada Apr 10 '12 at 15:08
  • What @ChrisS said -- `ntpdate` is great for synchronizing the clock once, or for force-synchronizing against your own NTP server every second on machines where the local clock drift is atrocious. If you're in the latter situation, replace the machine (a clock *that* bad will be drifting by several seconds every minute -- you'll never have accurate time). – voretaq7 Apr 10 '12 at 15:12
  • 11
    Please, please, please **DO NOT** be that guy that changes the defaults on NTP. I have to look at my public-facing NTP server weekly and nuke the guys sending 1 packet every second. If your server doesn't listen to ICMP message saying "administratively prohibited," I **will** call your ISP's NOC and you will have a bad day. NTP will establish accurate to within milliseconds on its own and will adjust querying speed up and down as needed. Fiddling doesn't help. – Jeff Ferland Apr 10 '12 at 16:16
8

Get a GPS time receiver if you need highly accurate time. It'll check your clock every second. If you don't want to spend much, you can get a Garmin 18x LVC on eBay or Amazon for <$100 and wire it to a COM port. Directions around the Internet, but does require a minimum level of wiring skill. Complete solutions are available that cost more, if you're not into DIY projects.

voretaq7
  • 79,879
  • 17
  • 130
  • 214
Chris S
  • 77,945
  • 11
  • 124
  • 216