The Network Time Protocol (NTP) is a protocol for synchronising the clocks of computers over TCP/IP networks. I don't go into detail here,1) but NTP is helpful if you need a really2) exact system time (e.g. on special Database servers) or just to get a correct clock setting on your common desktop. There are two ways to use NTP to set your system's clock:
ntpdate – Simple NTP client, syncs your system's clock instantly by polling specified NTP server(s) every time it is called.ntpd – NTP server daemon, calculates the drift of your system's hardware clock and continuously adjusts it. Can act as NTP server for other NTP servers and clients.
ntpdate syncs your system's clock instantly. It is installed by default on Ubuntu (if not: install the ntpdate (main) package) and the operating system will execute it once at boot time to set up your time according to the ntp.ubuntu.com server. This is a sane and good setup for common desktops and so you normally don't have to care about your clock.
However, all common mainboard hardware clocks are working more or less imprecisely. So if your PC is running a few days without a break, the clock may drift a few seconds between reboots. If you want to sync the time of such machines automatically, setup a cronjob.
For a sync once a day:
/etc/cron.daily/ntpdate file with root privileges and your favorite editor, e.g.sudo nano /etc/cron.daily/ntpdategksudo gedit /etc/cron.daily/ntpdatentpdate -u ntp.ubuntu.com pool.ntp.org
I added pool.ntp.org to increase accuracy and resilience.
sudo chmod 755 /etc/cron.daily/ntpdate
If you got a really bad hardware clock, you may want to sync every hour:
/etc/cron.hourly/ntpdate file with root privileges and your favorite editor, e.g.sudo nano /etc/cron.hourly/ntpdategksudo gedit /etc/cron.hourly/ntpdatentpdate -u ntp.ubuntu.com pool.ntp.org
I added pool.ntp.org to increase accuracy and resilience.
sudo chmod 755 /etc/cron.hourly/ntpdate
The advantage of ntpdate is its simplicity. No daemon is running and no configuration file is needed. The disadvantage is its lack of intelligence. Because your system's time will be changed immediately, there may be ugly side effects3) like doubled entries in logfiles of server daemons. But this should not affect a common desktop (or did you ever had any problems when changing your systems clock manually?). Please note that ntpdate will decline work if ntpd is running on the same host to prevent disturbing the daemon. Therefore it makes no sense to run ntpd and define an additional ntpdate cronjob. But you can use ntpdate when the systems boots to get a good initial time before the daemon starts.
The NTP daemon ntpd is a background service. It calculates the drift of your system clock and continuously adjusts it. This prevents large corrections which could lead e.g. to inconsistent log files on servers. It remembers the clock drift and it will correct it autonomously after using it for a while, even if there is no reachable NTP server. Additionally, your machine is able to act as an NTP server for other computers.
To get ntpd install the ntp (main) package. Its configuration file is located at /etc/ntp.conf. See The NTP FAQ or NTP Support to find a configuration fitting your needs.
Have a look at http://www.pool.ntp.org. This is a great project which uses round-robin DNS to return a NTP server from a pool, spreading the load between several different servers. The main, global pool is reachable via pool.ntp.org. But you may want to choose a special pool for your region to get serves located near your location, e.g.:
north-america.pool.ntp.org – Servers located in North Americaeurope.pool.ntp.org – Servers located in Europede.pool.ntp.org – Servers located in GermanySee http://www.pool.ntp.org/zone/@ for details.
Unix-like systems are assuming your hardware clock is offering a UTC time by default. But MS Windows systems are assuming your hardware clock is offering a time fitting your local timezone. This may lead to problems regarding non-UTC timezones and Daylight saving time, resulting in an x hours drift on one of both systems. If you got such problems, you need to configure either Windows to assume an UTC hardware time or your Ubuntu to assume a localized hardware time:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation → RealTimeIsUniversal, dword:00000001 with administrator privilegessudo nano /etc/default/rcS, change UTC=yes to UTC=no. If you choose the “Dual Boot” installation option during your Ubuntu-Setup, Ubuntu sets UTC=no by default.4)ntpd – are started