How to set up time synchronization on Ubuntu 16.04

Introduction

Accurate timing has become an essential component of modern software deployment. Whether it’s making sure the logs are written in the correct update order or the databases are being applied correctly, out of sync time can lead to errors, data corruption, and other difficult problems to debug.

Ubuntu 16.04 has time sync built and activated by default with the timesyncd service in Systemd. In this article, we’ll go over some basic timing related commands, make sure timesyncd is active, and know how to install an alternative network time service.

Prerequisites

Before starting this tutorial, you will need an Ubuntu 16.04 server with non-root, sudo user support, as described in this Ubuntu 16.04 server initial setup tutorial.

The most basic command for finding out the time on your server date… Anyone can enter this command to print out the date and time:

date

Output

Mo May 15 20:33:50 UTC 2017

More often than not, your server will default to UTC timezone as highlighted in the output above. UTC is coordinated time, time at zero degrees longitude. Consistently using Universal Time reduces confusion when your infrastructure spans multiple time zones.

If you have different requirements and need to change the time zone, you can use the command timedatectl, to do this.

First, a list of available time zones:

timedatectl list-timezones

A list of time zones will be printed on the screen. You can click SPACE one page down, and page up. Once you find the correct timezone, enter to exit the list.

Now set your timezone from timedatectl set-timezonemaking sure to replace the highlighted portion below with the timezone you found in the list. You will need to use sudo from timedatectl to make these changes:

sudo timedatectl set-timezone America/New_York

You can check the changes by doing date again:

date

Output

Mo May 15 20:33:50 EDT 2017

The time zone should reflect the newly selected value.

Now that we know how to check the clock and set the time zones, let’s make sure our times are synchronized correctly.

Controlling timesyncd with timedatectl

Until recently, most network time synchronization was not handled by the Network Time Protocol daemon or ntpd… This server connects to a pool of other NTP servers that provide constant and accurate time updates.

Ubuntu now uses timesyncd by default instead of ntpd… timesyncd connects to the same time servers and works in much the same way, but lighter and more integrated with Systemd and Ubuntu low-level commands.

We can query the timesyncd status by running timedatectl no arguments. You don’t need to use sudo in this case:

timedatectl

Output

Local time: Mo 2017-05-15 20:33:50 EDT
  Universal time: Mo 2017-05-15 20:33:50 EDT
        RTC time: Mo 2017-05-15 20:33:50
       Time zone: Etc/UTC (UTC, +0000)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

This will print out local time, universal time (which can be the same as local time if you don’t switch from UTC), and some network time information. Network time on: yes means timesyncd is on, and NTP synchronized: yes indicates the time was successfully synchronized.

If timesyncd is not enabled, enable it with timedatectl:

sudo timedatectl set-ntp on

Execute timedatectl again to confirm the network time status. It may take a minute for the actual sync, but in the end how Network time on: and NTP synchronized:should read yes

Switching to NTPD

While timesyncd is fine for most purposes, some applications that are very sensitive to even the slightest disturbance in time may be better served by ntpd, as it uses more sophisticated techniques to keep the system time consistently and gradually.

Before installing ntpd, we must turn off timesyncd:

sudo timedatectl set-ntp no

Make sure timesyncd is off:

timedatectl

Take a look Network time on: no at the exit. This remedy timesyncd was stopped. Now we can install the package ntp from apt-get:

sudo apt-get install ntp

Ntpd will start automatically after installation. You can query ntpd for status information to make sure everything is working:

sudo ntpq -p

Output

remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000
-makaki.miuku.ne 210.23.25.77     2 u   45   64    3  248.007   -0.489   1.137
-69.10.161.7     144.111.222.81   3 u   43   64    3   90.551    4.316   0.550
+static-ip-85-25 130.149.17.21    2 u   42   64    3   80.044   -2.829   0.900
+zepto.mcl.gg    192.53.103.108   2 u   40   64    3   83.331   -0.385   0.391

ntpq is a tool for querying ntpd. The -p flag requests information about NTP – connected servers ntpd… Your output will be slightly different, but the standard Ubuntu pool servers plus a few others should be listed. Note that it may take several minutes for ntpd to establish a connection.

Output

In this tutorial, we showed you how to view the system time, change time zones, work with Ubuntu by default with timesyncd, and install ntpd. If you have more complex timing needs than what we have covered here, you can refer in the official NTP documentationand also take a look at pool of the NTP project, a global group of volunteers providing much of the world’s NTP infrastructure.

Sidebar