// Time synchronisation with NTP on Ubuntu (and Debian)

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

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:

  1. Open a terminal
  2. Create a /etc/cron.daily/ntpdate file with root privileges and your favorite editor, e.g.
    • nano: sudo nano /etc/cron.daily/ntpdate
    • gedit: gksudo gedit /etc/cron.daily/ntpdate
  3. Store the following command:
    ntpdate -u ntp.ubuntu.com pool.ntp.org

    I added pool.ntp.org to increase accuracy and resilience.

  4. Make the created file executable:
    sudo chmod 755 /etc/cron.daily/ntpdate

If you got a really bad hardware clock, you may want to sync every hour:

  1. Open a terminal
  2. Create a /etc/cron.hourly/ntpdate file with root privileges and your favorite editor, e.g.
    • nano: sudo nano /etc/cron.hourly/ntpdate
    • gedit: gksudo gedit /etc/cron.hourly/ntpdate
  3. Store the following command:
    ntpdate -u ntp.ubuntu.com pool.ntp.org

    I added pool.ntp.org to increase accuracy and resilience.

  4. Make the created file executable:
    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.

ntpd

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.

Good NTP servers

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 America
  • europe.pool.ntp.org – Servers located in Europe
  • de.pool.ntp.org – Servers located in Germany

See http://www.pool.ntp.org/zone/@ for details.

Dual boot environments: my Windows/Ubuntu clock goes x hours wrong!

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:

  • Windows→UTC:
    Create the registry value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformationRealTimeIsUniversal, dword:00000001 with administrator privileges
  • Debian/Ubuntu→Local time
    sudo 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)
1)
The whole system and the client software is a lot more complex than you might think. Look at the Weblinks and/or search the www if you are interested.
2)
we are talking about ~200 milliseconds
3)
if not used only when booting the machine to get a correct time before all important services – including an eventually existing ntpd – are started
4)
AFAIK since 9.04 Jaunty

Leave a comment…




M I P​ Q᠎ Y
  • E-Mail address will not be published.
  • Formatting:
    //italic//  __underlined__
    **bold**  ''preformatted''
  • Links:
    [[http://example.com]]
    [[http://example.com|Link Text]]
  • Quotation:
    > This is a quote. Don't forget the space in front of the text: "> "
  • Code:
    <code>This is unspecific source code</code>
    <code [lang]>This is specifc [lang] code</code>
    <code php><?php echo 'example'; ?></code>
    Available: html, css, javascript, bash, cpp, …
  • Lists:
    Indent your text by two spaces and use a * for
    each unordered list item or a - for ordered ones.
I'm no native speaker (English)
Please let me know if you find any errors (I want to improve my English skills). Thank you!
QR Code: URL of current page
QR Code: URL of current page 2011:06:12:ntp-time-synchronisation-ubuntu-debian (generated for current page)