How to use Network Time Protocol on Linux

Linux System Time Synchronization with Network Time Protocol (NTP)

This post shows the synchronization of the system time with the atomic time of the NTP server in the Linux command line. Proper system time is essential for computer systems and creates the prerequisite for a smooth boost to the interacting services. The following command-line tools are used to check and adjust system time.

To querying the current system time with date and timedatectl:

$ date
Fri Feb 19 10:15:46 CET 2021
$ timedatectl
               Local time: Fri 2021-02-19 10:15:46 GMT
           Universal time: Fri 2021-02-19 09:15:46 UTC
                 RTC time: Fri 2021-02-19 09:15:44
                Time zone: Europe/Zurich (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: n

The time synchronization on a server is carried out by an NTP daemon to synchronize the system time and ensure that the systems function properly. Atomic time can be synchronized by NTP servers on the Internet, such as Google Public Network Time Protocol (NTP) time.google.com

NTP-Daemon deploying

Installing NTP daemon on Debian Linux-based systems:

$ sudo apt install ntp ntpdata -y

Install NTP daemon on RHEL/CentOS/Fedora systems:

$ sudo dnf install ntp ntpdate -y

NTP Pool Server pool.ntp.org tries to find the closest available server. The NTP pool project is a dynamic pool of time servers. The NTP pool servers can be edited in /etc/ntp.conf.

pool time.google.com
pool europe.pool.ntp.org

Before the ntpd service can be started, the system time must be set manually (in the range of a few minutes). This can be done with hit date or here run timedatectl set-time or via the NTP pool with ntpdate pool.ntp.org

$ sudo ntpdate pool.ntp.org
$ sudo ntpq -pn
$ sudo ntpq -c rv

ntpdate shows the current system time be synchronized with an NTP server. The deviations of the current system time from the times of the servers in the NTP pool can be queried with ntpq:

The deviations of the current system time from the times of the servers in the NTP pool can be queried with ntpq

Now run the NTP daemon and link it in systemd for automatic start:

$ sudo systemctl start ntp.service
$ sudo systemctl enable ntp.service

systemd-timesyncd configuring

The systemd-timesyncd service is already installed on many distributions. The configuration files control the time synchronization of the NTP network.

The network time synchronization for the systemd-timesyncd service includes the file /etc/systemd/timesyncd.conf

The timesyncd.conf configuration file can be as follows.

# See timesyncd.conf(5) for details.
 
[Time]
NTP=ch.pool.ntp.org
FallbackNTP=0.ch.pool.ntp.org 1.ch.pool.ntp.org 2.ch.pool.ntp.org 3.ch.pool.ntp.org 4.ch.pool.ntp.org
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

  use NTP servers that are in your zone for local time synchronization, see pool.ntp.org

So that systemd-timesyncd is started automatically, the command enable the service accordingly:

$ sudo systemctl enable systemd-timesyncd
$ sudo systemctl start systemd-timesyncd

With a systemd-based system, run the following command to check the service status:

$ timedatectl status
               Local time: Fr 2021-02-19 10:15:46 CET
           Universal time: Fr 2021-02-19 09:15:46 UTC
                 RTC time: Fr 2021-02-19 09:15:44
                Time zone: Europe/Zurich (CET, +0100)
System clock synchronized: no
              NTP service: n/a
          RTC in local TZ: no

The output on lines 7 and 8 means that the time synchronization is not carried out. To enable synchronization with the following command:

$ sudo timedatectl set-ntp true

Query the NTP service status again, after a few seconds the output shows System clock synchronized: yes and NTP service: active. The NTP service status should show something like this:

$ timedatectl status
               Local time: Fr 2021-02-19 10:15:46 CET
           Universal time: Fr 2021-02-19 09:15:46 UTC
                 RTC time: Fr 2021-02-19 09:15:44
                Time zone: Europe/Zurich (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Kerberos authentication

Kerberos authentication is currently the standard authentication technology under Windows; Apple macOS, FreeBSD and Linux systems also use Kerberos implementations. When authenticating in an environment using Kerberos, a synchronous system time is of fundamental importance for interacting systems.

Since Kerberos requires three entities for authentication, it works as a ticket authorization by a third party, if the system time exceed a certain drift limit, a token created by Kerberos expires after a certain time and the ticket becomes invalid.

Kerberos has also become the standard for websites and SSO implementations across platforms. In Active Directory Domain Services (AD DS), when integrating NAS and other devices for access to network resources, it is necessary to ensure a consistent system time. For logging like syslog, the precise timestamp for the evaluation of the events and for the error analysis are important.

apt install ntp ntpdate -y
Linux Mint 20 Cinnamon: apt install ntp ntpdate

How to Set Time and Date on Linux by timedatectl

Control the system time and date using the timedatectl command in the Linux Shell.

This post shows how to set the time and date in the Linux command line using timedatectl. It is always a good practice to maintain the correct system time. It’s essential and the prerequisite able to allow proper functioning of any service. The command line tool timedatectl use to check and modify time setting.

Run the query usingtimedatectlwithout additional arguments:

$ timedatectl
               Local time: Wed 2021-02-17 17:04:29 GMT
           Universal time: Wed 2021-02-17 17:04:29 UTC
                 RTC time: Wed 2021-02-17 16:57:58
                Time zone: Europe/London (GMT, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: n

Set time and date by timedatectl

To set-time switch along with the format of time in HH:MM:SS (Hour, Minute, and Seconds):

$ timedatectl set-time 14:09:0

set-time switch along with the format of date in YY:MM:DD (Year, Month, Day):

$ timedatectl set-time 2021-02-1

To set both date and time:

$ timedatectl "2021-02-18 14:09:05"

Check hardware clock

Find out if your hardware clock is set to local timezone:

$ timedatectl | grep local

Set hardware clock

Set your hardware clock to local timezone:

$ timedatectl set-local-rtc

Network time synchronization

Enable network time synchronization.

$ timedatectl set-ntp true
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ===
Authentication is required to control whether network time synchronization shall be enabled.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===

Ceck status of network time synchronization service.

$ systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-07-11 18:16:59 CEST; 1 months 2 days ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 650 (systemd-timesyn)
     Status: "Initial synchronization to time server 84.16.73.33:123 (ch.pool.ntp.org)."
      Tasks: 2 (limit: 4660)
     Memory: 1.3M
        CPU: 11.296s
     CGroup: /system.slice/systemd-timesyncd.service
             └─650 /lib/systemd/systemd-timesyncd

Show current status of systemd-timesyncd.service.

$ timedatectl timesync-status
       Server: 84.16.73.33 (ch.pool.ntp.org)
Poll interval: 34min 8s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 1
    Reference: GPS
    Precision: 1us (-25)
Root distance: 52us (max: 5s)
       Offset: -159us
        Delay: 6.944ms
       Jitter: 531us
 Packet count: 1392
    Frequency: +16.521ppm

Maintain the correct time by timedatectl

The timedatectl command allows you to query and change the configuration of the system clock and its settings. You can use this command to set or change the current date, time, and timezone. Or enable automatic system clock synchronization with a remote NTP server.

Here you walk through the ways you can manage time on your Linux system by setting the date. The time, timezone, and synchronize time with NTP from the terminal using the new timedatectl command.

It is always a good practice to maintain the correct time on your Linux server or system and it can have the following advantages:

  • maintain a timely operation of system tasks since most tasks in Linux are controlled by time.
  • the correct time for logging events and other information on the system and many more.

time and date command

The time extension adds two functions named gettimeofday() and sleep(), as follows. time – time functions for gawk.

$ time

real    0m0.000s
user    0m0.000s
sys     0m0.000s

Display the current time in the given FORMAT, or set the system date.

$ date
Sun 13 Aug 2023 04:44:24 PM CEST

You might also be interested in this related post.