NTP is a networking protocol used to synchronize all system clocks in a network. This enables all the systems in a network to have the same time. NTP does so by contacting a number of other time servers on the internet. NTP uses UDP port 123 to communicate with clients and other NTP servers. In this post, we are going to show you how to configure the NTP server and client on the Debian system.
For demonstration, we have used two Debian machines with the following details:
NTP host – Hostname: ntp-host, Static IP address: 192.168.72.158
NTP Client – Hostname: client, IP address: 192.168.72.159
Note: The steps shown here have been tested on Debian 10 (Buster).
Install and Configure NTP Server on Debian Host Machine
For installing and configuring the NTP server on Debian 10, you will need to perform all the steps that are listed below:
Step 1: Run Apt Update
First, you will have to update the packages list in order to download the latest version of the NTP package. Open the Terminal and run the following command to do so:
$ sudo apt update
Step 2: Installing NTP on Host Server
To install the NTP server on the host machine, run this command:
$ sudo apt install ntp
After running the above command, you may be asked if you want to continue the installation by providing you with the y/n (yes/no) option. Enter y to continue the installation process.
To verify if the NTP is installed and to view the version information, run the following command:
$ sntp --version
Step 3: Configuring NTP Server
To configure the NTP server, you are required to have two elements on your host machine: one is /etc/ntp.conf file and the other is ntpd daemon. First, we will configure the ntp.conf file and then we will restart the ntpd daemon.
Edit the /etc/ntp.conf file using the following command:
$ sudo nano /etc/ntp.conf
When the NTP server is installed, it is by default configured to fetch the time from the following servers:
pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst pool 2.debian.pool.ntp.org iburst pool 3.debian.pool.ntp.org iburst
You can replace them with the time servers nearest to your location. Visit the ntp.org website to find the NTP pool servers nearest to your location.
To use any specific NTP pool server, you will need to add them to your ntp.conf file. We are going to add pools servers for the Asia region which is shown as follows:
In the ntp.conf file, replace the default pool list with the following lines:
server 0.asia.pool.ntp.org server 1.asia.pool.ntp.org server 2.asia.pool.ntp.org server 3.asia.pool.ntp.org
Then save and close the ntp.conf file.
Step 4: Restart NTP Daemon
Now you should restart the NTP daemon. Use the following command to do this:
$ sudo service ntp restart
Now, run the following command to check if the NTP server is running without any issues:
$ sudo service ntp status
Step 5: Allow NTP through Firewall
If you are using a firewall, then you will need to configure it to allow UDP connections to NTP port 123. Run the following command to allow clients access to NTP server:
$ sudo ufw allow 123/udp
Now run the following command to verify if the rule has been successfully added:
$ sudo ufw status
Install and Configure NTP Client on Debian Client Machine
To configure a machine as an NTP client, you will need to ensure that the offset (time difference between the local clock and NTP server) is not more than 1024 seconds. If the offset is greater than this value, the time source will be considered inaccurate. For this reason, we will first try to manually synchronize the NTP client with the NTP server using the ntpdate. If the offset value comes less than 1024 seconds, we will then configure ntpd.conf file for automatic synchronization.
Step 1: Sync NTP Client with NTP Server Manually Using ntpdate
Ntpdate is used to synchronize the time of the NTP client with the NTP server for once only. To install it on the client machine, use the following command:
$ sudo apt install ntpdate
After running the above command, you may be asked if you want to continue the installation by providing you with the y/n (yes/no) option. Enter y to continue the installation process.
Now use the following command to manually sync the NTP client with the NTP server:
$ sudo ntpdate <ntp-server-hostname/ip>
In our scenario, it would be:
$ sudo ntpdate 192.168.72.158
The output of the ntpdate command will show offset value (time difference between the local clock and NTP server). In our example, the offset value is 0.054776 sec which is very less. Therefore, we can now configure ntp.conf to automatically synchronize the time with our NTP server.
Step 2: Hosts File Configuration
Add the following hostname entry in the /etc/hosts file of the client machine. This step is required to resolve the NTP server through the hostname.
<ntp-server-hostname> <ntp-server-ip>
Make sure to replace ntp-server-hostname and ntp-server-ip with the hostname and IP address of your NTP respectively. In our scenario, it would be:
192.168.72.158 ntp-host
Step 3: Disable systemd timesyncd Daemon
As we are going to setup NTP, therefore disable the systemd timesyncd daemon. Use the following command to do so:
$ sudo timedatectl set-ntp off
Step 4: Sync NTP Client with NTP Server automatically Using NTP Configuration File
Now we will configure our client machine to sync with the NTP server automatically. For this reason, we will install NTP using the following command on the client system:
$ sudo apt install ntp
After running the above command, you may be asked if you want to continue the installation by providing you with the y/n (yes/no) option. Enter y to continue the installation process. Once the installation is finished, configure ntp.conf file.
Run the following command to edit the ntp.conf file:
$ sudo nano /etc/ntp.conf
Insert the following line in the file:
server <ntp-server-hostname/ip> prefer iburst
In our scenario, it would be:
server 192.168.72.158 prefer iburst
In the above line, prefer is used to mark the specified server as the preferred server for NTP synchronization. Where the iburst is used to send six packets for synchronization instead of the usual one (in case the NTP server is unreachable).
Now restart the NTP daemon to apply the configuration changes:
$ sudo service ntp restart
Check NTP Synchronization Status
Now to check if NTP is working correctly, run the following command:
$ ntpq -p
Here is the output of the above command where remote is the NTP server hostname and refid is the top-level server to which our NTP server has itself connected for synchronization.
That is all there is to it! In today’s post, we have shown you how to configure the NTP server on Debian 10. Then we have explained how to configure the NTP client on another Debian machine and synchronize it with the NTP server. By following all the simple steps explained above, you can easily synchronize all system clocks in a network.
You may like to visit our post on How to configure an NTP server and client on Ubuntu 20.04 LTS.