Debian Jessie Systemctl TimeDateCtl replacement for NTP network time source synchronisation

Still With the latest Debian Jessie image where I have my Asterisk running on the ARM device Rasperry Pi, I find that the clock source synchronisation is still being accomplished with the NTP daemon rather than with the “timedated” daemon.

Below describes the steps to remove the setup of legacy NTP configuration, and setup the new light weight “systemd-timedated” configuration to synchronise your system’s clock from the Internet.

First shutdown the NTP daemon process, disable the auto-start setup and deinstall the packages with the following commands on the shell prompt:

sudo systemctl stop ntp
sudo systemctl disable ntp
sudo apt-get -y remove ntp
sudo rm -f /etc/init.d/ntp
sudo rm -f /etc/default/ntp
sudo rm -f /etc/rc?.d/{S,K}??ntp
sudo rm -f /etc/ntp.conf
sudo rm -f /etc/cron.daily/ntp
sudo rm -f /var/log/ntpstats

You may do the same steps to de-activate the “fake-hwclock” deamon that takes care of the timekeeping if your device does not have a RTC source built-in. Issue the following commands on the shell prompt:

sudo systemctl stop fake-hwclock
sudo systemctl disable fake-hwclock
sudo apt-get -y remove fake-hwclock
sudo rm -f /etc/init.d/fake-hwclock
sudo rm -f /etc/rc?.d/{S,K}??fake-hwclock
sudo rm -f /etc/default/fake-hwclock
sudo rm -f /etc/cron.hourly/fake-hwclock

With your favourite text editor, modify the file “/etc/systemd/timesyncd.conf” to have the following contents:

[Time]
NTP=0.europe.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org

You may choose the NTP sources according to your region as described with the “NTP Pool Time Servers” list or simply consult the “How do I use pool.ntp.org” instructions.

With your favourite text editor, modify the file “/etc/rc.local” to have the following contents:

[ ! -e /var/lib/systemd/clock -a "`systemctl is-active systemd-timesyncd | grep -i active`" ] && timedatectl set-ntp 1 > /dev/null 2>&1
sleep 2

Note: The back-quote in the “systemctl” command and the “sleep” statement are very important to keep!

To finally enable the automatic start-up of the system’s clock synchronisation during boot-up using the “systemd-timesyncd” service, execute the following commands on the shell prompt:

sudo systemctl enable systemd-timesyncd
sudo systemctl start systemd-timesyncd
sudo timedatectl set-ntp 1

With this approach, we leave it up to “systemd” to start up the relevant services for date and time synchronisation from the RTC chip during boot up, and if the computer is also connected to the Internet, to synchronise the OS and RTC with the corresponding source of network time.

To verify its proper operation, after a little while, simply execute the following command on the shell prompt:

timedatectl

It shall provide an output similar to the following:

  Local time: Wed 2017-03-18 09:44:46 CET
  Universal time: Wed 2017-03-18 08:44:46 UTC
    RTC time: Wed 2017-03-18 08:44:46
   Time zone: … (CET, +0100)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

Similar to the legacy “fake-hwclock” mechanism, a file “/var/lib/systemd/clock” will be created and regularly updated with the time of last clock synchronisation with the network time.

Hope you find this little tutorial useful :wink: