NTP logo

Raspberry Pi and NTP

What is NTP?
NTP or Network Time Protocol is, as the name implies, a protocol that is used to synchronize time over a network. All system clocks in a network use the same time reference. When using the term NTP, we are referring to both the protocol and the programs running on the computer(s)/server(s).

In this tutorial we’ll setup a NTP server and configure a Client to use this server.
The method described here can be used on any Debian system.

NTP Server

If you already have a NTP server available you can skip these steps. Otherwise you’ll need 2 hosts, in this case we’ll be using 2 Raspberry Pi’s but as said before any Debian machine will do.

Step 1: Update your Pi

Before we continue we’ll update the pi with the latest available software, unless you have a specific reason why not to do this. This is good practise and will make sure you always have the latest bugfixes.

sudo apt update
sudo apt upgrade -y
APT update & Upgrade

Step 2: Installing NTP Server

To install the NTP server we use the APT package manager from Debian:

sudo apt install ntp ntpstat -y

An APT command has to be run as administrator, either use sudo or login as root.

NTP install

Step 3: Verify installation (optional)

You can verify your NTP installation and also check the version number by running the following command in your Terminal:

sntp --version
ntp version

Step 4: Change the NTP Pool servers

The NTP server is mostly configured to fet the proper time after initial installation.
However, you can switch the server pool to the ones closest to your location. This includes making some changes in the /etc/ntp.conf file.

Open the file in the nano editor as sudo by running this following command:

sudo nano /etc/ntp.conf
ntp server config

In this file, you will be able to see a pool list.
We have highlighted this list in the above image.
You can replace these with the pool’s you like.

Exit the file by hitting Ctrl+X and then press y for saving changes.
After making an adjustment or change to any config file it’s recommended to reboot the service or program
So lets do that.

sudo service ntp restart

Step 5: Verify

Lets check if NTP is up and running, we use the same command as above with a small adjustment.

sudo service ntp status
ntp status

The Active status verifies that your NTP server is up and running.

Step 6: Configure Firewall so that client(s) can access NTP server

Finally, it is time to configure your system’s UFW firewall that incoming connections can access the NTP server at UDP Port number 123.

Run the following command as sudo to open port 123 for incoming traffic:

sudo ufw allow from any to any port 123 proto udp

Note that standard the Raspberry Pi OS does not come with a Firewall!

NTP Client

Let us now configure our Debian like client machine to be time synchronized with the NTP server.

Step 1: Install ntpdate

The ntpdate command will let you manually check your connection configuration with the NTP-server. Use following command to install it on your system:

sudo apt install ntpdate -y
ntpdate install

Step 2: Changing configs (OPTIONAL)

For your NTP server to be resolved by a hostname in your client machine, you need to configure your /etc/hosts file.

Open the hosts file as sudo in the nano editor by entering the following command:

$ sudo nano /etc/hosts

Now add your NTP server’s IP and specify a hostname as follows in this file:

ntpdate hostfile change

Quit the file by hitting Ctrl+X and then save it by entering y.

Step 3: Check synch

Now that we have it all installed, lets check if we are in synch with our time-server.

sudo ntpdate -u NTP-server-host

Now in an ideal world these should be the same.
But since this Pi is behind a proxy server and doesn’t have access to the internet it never synced correctly as can be seen by the offset.

NTPdate Offset

Step 4: Disable timesyncd service

Because we want our client to sync time with the NTP server, let us disable the timesyncd service on the client machine.

Enter the following command to do so:

sudo timedatectl set-ntp off

Step 5: Install & configure NTP

Run the following command as sudo in order to install NTP on your client machine:

sudo apt install ntp -y

Now we want our client machine to use our own NTP host server to be used as the default time server. For this, we need to edit the /etc/ntp.conf file on the client machine.

Run the following command as sudo in order to open the file in the Nano editor:

sudo nano /etc/ntp.conf

Then, add the following line in the file, where NTP-server-host is the hostname you specified for your NTP server:

server NTP-server-host prefer iburst

Hit Ctrl+x in order to quit the file and then enter y to save the changes.

Step 6: Restart the NTP server

In order for the above changes to take effect, you need to restart the NTP service. Run the following command as sudo in order to do so:

sudo service ntp restart

Now your client and server machines are configured to be time-synced. You can view the time synchronization queue by running the following command:

$ ntpq -pn

You should be able to see NTP-server-host as the time synchronization host/source in the queue.


So this is al there is to know about a basic install and configuration for NTP synchronization to a local NTP server.
It might not have been looking straight forward but once you get the hang of Linux you’ll see the logic in it.

Have fun with you NTP install!

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.