6

I think there are two solutions for keeping the time in sync on my machine. One is to run an NTP service, another is to run the ntpdate regularly by putting it in a crontab.

This is how I understand it - is it correct? if yes, which solution is better?

gertvdijk
  • 67,947
William
  • 61
  • 1
  • 2

3 Answers3

6

About ntpdate:

  • Should be used to sync the time at that moment only. It will leave the time alone after it has been run.
  • Limits to one remote peer to sync with.
  • Does not account for skew caused by your system clock.

About the ntp (service):

  • Checks multiple remote peers.
  • Ability to check for remote peers that provide wrong time. They will be ignored. Run ntpq -p to list the remote peers and the "score" (+, -, *) ntp gives it.
  • Will keep track of the time of the machine and adjusts time by very small increments to compensate for skew caused by your system clock.
  • Does not apply a large offset. If your system clock is off for a few hours, ntp won't touch it. See Unable to synchronize time using NTP

Conclusion: Use ntpdate the first time you use the system (done by the Ubuntu installer if you're connected to the internet), then configure ntp correctly and it should keep your clock in sync. That is the best option in my opinion.

gertvdijk
  • 67,947
  • So, what's the exact cycle period for NTP to query the time from server ? – William Aug 27 '13 at 09:07
  • @William Look at the output of ntpq -p. It shows the next contact moment in seconds for each server configured (default four from the volunteer pool). This time will increase after some time and NTP will "learn" about the clock skew on your system. This depends all on your configuration and the behaviour of your clock. It's a complicated but very efficient algorithm. Therefore it also takes some time before it will function as designed. – gertvdijk Aug 27 '13 at 10:21
  • If the problem is a clock that is off by too much upon startup you could also add tinker panic 0 to ntp.conf or add -g to command line options in /etc/default/ntp. This is much cleaner than sopping ntpd, ntpdate/sntp, starting ntpd. – dfc Jan 19 '14 at 17:00
1

The best solution is to run ntpd or ntpclient. Both of these will continually adjust your clock to keep it in sync. My clocks are typically withing a few milliseconds of their best time source. Overhead is quite low and once synchronization is achieved, there is is only one query every 1024 seconds (or longer). With a GPS or radio source they can be used even when disconnected from the Internet.

ntpdate can be used to perform first time synchronization, although newer versions of the daemons will do this step too. ntpdate is useful for debugging access to time servers, and discovering time sources. I use it to verify if my router or DNS servers provide a good time source.

When I need two or more servers to have synchronized clocks, I use ntpd and peer the servers. Even when disconnected, you can enable use of the local clock on one or three servers and let them choose the best time source.

The daemons can provide good logs to verify the quality of the time sources and how well you have been synchronized. If you fudge the stratum of the hardware clock you can provide a backup time source, and log how well it tracks reality.

BillThor
  • 4,698
0

I agree with above answers, but it is overkill in most circumstances to have the average user or server box running a full NTP server/service/daemon.
And why install extra system software? If it's good enough for the Ubuntu system to set system time (see next paragraph) being brought up, it's fine for most syncs needed by the avg user.

Unless your system clock is constantly off (like some of my machines) or you need very precise time-keeping round-the-clock, a sync is done each time the network interface is brought up ( see: /etc/network/if-up.d/ntpdate ).

If you do need system clock updates occasionally, but do not want to run a full NTPd service, there is nothing wrong with using ntpdate to update your system when you like. I do this via cron. You may wish to do a root crontab or crond hourly (or daily) using ntpdate:

sudo nano /etc/cron.hourly/synctime

Note: The filename of the script called by run-parts should have NO extension or odd characters. Your script will not get picked up if this is the case. I choose the single lowercase name above ("synctime" - no extension).

Then add this to that script:

#!/bin/sh
/usr/sbin/ntpdate -s 192.168.1.1  # < local or remote (name or IP) for NTP server here

Save. Then make executable:

sudo chmod 755 /etc/cron.hourly/synctime

Every hour ntpdate will sync to server of your choice. (Default cron.hourly time on mine is processed ~17 minutes after the hour) ..in addition to the system default sync done when interface comes up (usually when system comes up after a reboot).

I personally run the ntpd server on my main Ubuntu server (stays sync'd from accurate ntp server pools on Internet) - which in turn feeds all of my LAN machines via ntpdate as per above - as the client. This way my LAN is synchronized from one local point using Ubuntu's default ntpdate utility.

Syslog at 17 minutes past hour..

B. Shea
  • 1,188
  • 14
  • 17