Install and Configure NTP Client on Ubuntu/Debian systems

0
611

Follow through this tutorial to quickly learn how to install and configure NTP Client on Ubuntu/Debian systems.

Install and Configure NTP Client on Ubuntu/Debian systems

We will discuss three ways in which you can install and configure NTP Client on Ubuntu/Debian systems.

  1. Synchronize time with Systemd-timesyncd Service
  2. Synchronize time using ntpdate command
  3. Synchronize time using NTP service

The guide is based on the NTP servers we configured as per our previous guides on how to configure NTP Server.

Synchronizing using systemd timesyncd service

In an Ubuntu/Debian systems, an NTP Client, systemd-timesyncd.service, is running by default.

Edit the file /etc/systemd/timesyncd.conf and add the address for your NTP server by adding such an entry at the end of the file:

vim /etc/systemd/timesyncd.conf
NTP=192.168.59.38

Where 192.168.56.103 is the IP address of configured NTP server.

You can also set FallBack by uncommenting the line below;

FallbackNTP=ntp.ubuntu.com

Restart systemd-timesyncd NTP client service:

sudo systemctl restart systemd-timesyncd

Confirm the status of status that it is now synchronized with the configured NTP server.

 systemctl status systemd-timesyncd

Output:

● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-05-14 12:10:14 UTC; 2s ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 1166 (systemd-timesyn)
     Status: "Initial synchronization to time server 192.168.59.38:123 (192.168.59.38)."
      Tasks: 2 (limit: 2241)
     Memory: 1.2M
        CPU: 73ms
     CGroup: /system.slice/systemd-timesyncd.service
             └─1166 /lib/systemd/systemd-timesyncd

May 14 12:10:14 jellyfish systemd[1]: Starting Network Time Synchronization...
May 14 12:10:14 jellyfish systemd[1]: Started Network Time Synchronization.
May 14 12:10:14 jellyfish systemd-timesyncd[1166]: Initial synchronization to time server 192.168.59.38:123 (192.168.59.38).
timedatectl status
               Local time: Sat 2022-05-14 12:10:53 UTC
           Universal time: Sat 2022-05-14 12:10:53 UTC
                 RTC time: Sat 2022-05-14 12:10:53
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Using ntpdate to Synchronize Client Systems’ Time

Optionally the ntpdate command can be used to manually synchronize client system time with NTP server. This guide uses Ubuntu 18.04 as the client.

Install ntpdate package, if not already installed.

sudo apt install ntpdate -y

NOTE: Ensure that Client and NTP Server can communicate. You can use nc command to verify NTP server port connection.

For example;

nc -uvz 192.168.59.38 123

Sample output;

Connection to 192.168.59.38 123 port [udp/ntp] succeeded!

Next, use ntpdate Command to Query Time Service. The ntpdate command can be uses to query time service from an NTP server by running the command:

sudo ntpdate 192.168.59.38

The output shows the time offset between the two systems.

14 May 12:17:28 ntpdate[1516]: adjust time server 192.168.59.38 offset -0.019108 sec

Synchronize time Automatically Using NTP

NTP client can automatically be configured to query NTP server by using the NTPd daemon.

Install NTP on Ubuntu/Debian

sudo apt install ntp -y

The installation of NTP will mask the systemd-timesyncd.service.

The NTP service is set to run by default after installation. First check if the client is synchronized with NTP:

timedatectl

The output will show if the system clock is synchronized or not. The output below clearly indicates time is not synced to any server.

               Local time: Sat 2022-05-14 12:20:04 UTC
           Universal time: Sat 2022-05-14 12:20:04 UTC
                 RTC time: Sat 2022-05-14 12:20:04
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
              NTP service: n/a
          RTC in local TZ: no

To configure the NTP client to synchronize time from your NTP server, edit the ntp configuration file:

 sudo vim /etc/ntp.conf 

Replace public NTP pool servers with your server.

#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst

pool 192.168.59.38 iburst

Ideally the server can be added without commenting out the default NTP servers by making it the preferred reference clock using the prefer option:

pool 192.168.59.38 prefer iburst

Save the configuration file and restart ntp.

 sudo systemctl restart ntp

The client is now successfully configured to sychronize system time with NTP server. This can be verified by running the command:

ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.59.38   .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
*192.168.59.38   91.189.89.198    3 u    2   64    1    1.732  +16.240   0.089
 chilipepper.can 17.253.34.251    2 u    8   64    1  255.060  -41.243   0.000
 pugot.canonical 17.253.108.125   2 u    5   64    1  171.300   -0.021   0.000
 golem.canonical 134.71.66.21     2 u    4   64    1  263.212  -45.093   0.000
 alphyn.canonica 194.58.200.20    2 u    5   64    1  251.289   +2.991   0.000

From the output we can see NTP server (192.168.56.103) as the time synchronization host/source in the queue.

Confirm NTP service is set to start at boot time:

systemctl is-enabled ntp

To enable NTP service to start at boot time, just in case is not enabled, then you would run the command:

systemctl enable ntp

Great, your NTP Clients should now be able to query the time services from your NTP Server.

That brings us to the end of the guide on how install and setup NTP Client on Ubuntu/Debian.

LEAVE A REPLY

Please enter your comment!
Please enter your name here