26
October
2023
Time Synchronization in Linux: Using the SNTP Service
15:45

Time Synchronization in Linux: Using the SNTP Service

26 October 2023 15:45

There are situations when the capabilities of a regular NTP client service are not enough: rebooting a PC without a CMOS battery is an example of this. Poor Internet quality - infrequent time synchronization does not allow the NTP service to function fully. To solve this problem, the systemd-timesyncd service was invented, which works using the simplified SNTP protocol - Simple NTP.

Setting the time zone

(Optional) Time zones are discussed in the article "Linux Time".

sudo timedatectl set-timezone Europe/Moscow

Time synchronization methods

1) Using the client NTP what is discussed in article- full NTP client.

2) Using the systemd-timesyncd service - a simple client SNTP with synchronization using systemd.
3) other methods (I do not consider).

Installing the systemd-timesyncd service

sudo apt install systemd-timesyncd

Starting the systemd-timesyncd service

sudo systemctl enable systemd-timesyncd.service
sudo systemctl start systemd-timesyncd.service

Enabling time synchronization through the systemd-timesyncd service

Allows time synchronization over the network:

sudo timedatectl set-ntp true

Setting up automatic time synchronization

In order for time correction to occur automatically at specified intervals, you need to correct the configuration file /etc/systemd/timesyncd.conf:

sudo nano /etc/systemd/timesyncd.conf

[Time]
NTP=2.ru.pool.ntp.org
FallbackNTP=3.ru.pool.ntp.org
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

In this example, the NTP server polling interval ranges from 32 seconds to 34 minutes.

Restarting the systemd-timesyncd service:

sudo systemctl restart systemd-timesyncd.service

Checking the time synchronization status

timedatectl status

If all is well, the output will be:

System clock synchronized: yes
NTP service: active

Note

Both methods are mutually exclusive:
*NTP:* installing the ntp/sntp packages will automatically remove the systemd-timesyncd package.
SNTP:** the ntp package is not required for systemd-timesyncd to work.

It seems that the second method, SNTP, is more suitable for field environments and embedded systems. Since it maintains the time between reboots in a special file and with the help of amendments, clock drift is taken into account.

MAN - user manual for the SYSTEMD-TIMESYNCD.SERVICE service

SYSTEMD-TIMESYNCD.SERVICE(8) systemd-timesyncd.service SYSTEMD-TIMESYNCD.SERVICE(8)

NAME
systemd-timesyncd.service, systemd-timesyncd — синхронизация времени по сети

SYNOPSIS
systemd-timesyncd.service
/lib/systemd/systemd-timesyncd

DESCRIPTION
systemd-timesyncd is a system service that can be used to synchronize the local system clock with a remote Network Time Protocol (NTP) server.

It also saves local time to disk every time the clock is synchronized, and uses this to possibly advance the system real-time clock on subsequent reboots to ensure that it advances (roughly) monotonically, even if the system lacks a battery-buffered RTC chip.

The systemd-timesyncd service only implements SNTP. This minimalist service will increment the system clock for large offsets or slowly adjust it for smaller offsets. Complex use cases that require full NTP support (and where SNTP is not sufficient) are not covered by systemd-timesyncd.

Access to NTP servers is determined based on global settings in timesyncd.conf(5), static per-channel settings in .network files, and dynamic per-channel settings obtained via DHCP found in Additional Information - see systemd.network(5).

Team timedatectl set-ntp true can be used to enable and start or disable and stop this service.

Teams timedatectl timesync-status and timedatectl show-timesync can be used to display the current status of that service.

Systemd-timesyncd initialization delays the launch of modules that are ordered after time-set.target (see systemd.special(7) for details) until the local time is updated from /var/lib/systemd/timesync/lock (see below). ) to make it approximately monotonous. It does not delay other devices until synchronization with accurate time reference sources is achieved. To do this, use systemd-time-wait-sync.service(8), which will delay the launch of modules ordered after time-sync.target until synchronization with an accurate reference clock is achieved.

FILES
/var/lib/systemd/timesync/clock
The modification time ("mtime") of this file indicates the timestamp of the last successful synchronization (or at least the systemd build date if synchronization is not possible). It is used to ensure that the system clock remains approximately monotonic across reboots in the absence of a local real-time clock.

/run/systemd/timesync/syncronized
A file that is touched on every successful synchronization to help systemd-time-wait-sync and other applications detect synchronization with an accurate reference clock.

How can I find out the date and time of the last successful time synchronization?

ls -l /var/lib/systemd/timesync/clock

Starting time synchronization manually

There is no need to manually synchronize time if configured automatic time synchronization via the Internet.

Command for manual time synchronization:

sudo systemctl restart systemd-timesyncd

 

For the convenience of manual time synchronization, you can create a shell script named "timesync.sh":

#!/bin/sh
sudo systemctl restart systemd-timesyncd.service
find /var/lib/systemd/timesync/clock -printf "File: %f: Date: %TY-%Tm-%Td - Time: %TT\n" -exec tail -n5 {} \;
timedatectl status | grep -e "synchronized" -e "NTP"

Here is the file in a ZIP archive:
timesync.sh.zip

Save it in the current directory and make it executable

sudo chmod +x timesync.sh

Manually starting time synchronization:

./timesync.sh

(To avoid asking for a password every time:usermod -aG sudo ваше_имя).

N.B.: If the PC clock is synchronized with high accuracy, the service will write: "System clock synchronized: yes" regardless of whether there was a connection to the network at the time of the request to the NTP server.

What is the time error (deviation) when using SNTP?

The deviation of the system clock relative to the exact time is 5-6 milliseconds:

sudo apt-get update
sudo apt install iputils-clockdiff
sudo clockdiff 3.ru.pool.ntp.org

Technical details

At the time of time synchronization, the UDP protocol, remote port 123, NTP Version 4 protocol is used.
The payload is 48 bytes.

Frame xxxx: 90 bytes on wire (720 bits), 90 bytes captured (720 bits) on interface enp1s10, id 0
Ethernet II, Src: PERIPHER_0c(), Dst: Router()
Internet Protocol Version 4, Src: xx.xx.xx.xx, Dst: 91.209.94.10
User Datagram Protocol, Src Port: xxxxx, Dst Port: 123
Source Port: xxxxx
Destination Port: 123
Length: 56
Checksum: 0x7b13 [unverified]
[Checksum Status: Unverified]
[Stream index: 1059]
[Timestamps]
UDP payload (48 bytes)
Network Time Protocol (NTP Version 4, client)
Flags: 0x23, Leap Indicator: no warning, Version number: NTP Version 4, Mode: client
Peer Clock Stratum: unspecified or invalid (0)
Peer Polling Interval: invalid (0)
Peer Clock Precision: 1,000000 seconds
Root Delay: 0,000000 seconds
Root Dispersion: 0,000000 seconds
Reference ID: NULL
Reference Timestamp: (0)Jan 1, 1970 00:00:00.000000000 UTC
Origin Timestamp: (0)Jan 1, 1970 00:00:00.000000000 UTC
Receive Timestamp: (0)Jan 1, 1970 00:00:00.000000000 UTC
Transmit Timestamp: Oct 26, 2023 13:10:05.053581346 UTC

Experiment

What will the clock deviation be if you leave your PC without Internet overnight?

If you leave the PC turned off overnight and then boot it without a network, the time error was plus 114-168 milliseconds.

After turning on the Internet, the system time, thanks to the SNTP protocol and the SystemD service, independently returned to the accurate one - the error of the computer’s system clock was minus 14 milliseconds.

Useful link:
*Configure Time Synchronization on Linux using Systemd Timesyncd