How to Setup Ubuntu NTP Server

Published:18 March 2024 - 6 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

In the digital world, getting timing right is key to keeping smooth event loggings or handling database transactions. Messing up the timing invites chaos and errors, risking your systems and data integrity. But have you heard of a Network Time Protocol (NTP) server? With an Ubuntu NTP setup, you have the perfect tool to keep your system ticking along without a hitch!

This tutorial will walk you through setting up an NTP server as a reliable way to ensure consistent timing across your network.

Turn your system into a beacon of accurate timekeeping for your network!

Prerequisites

Below are preparatory steps crucial to ensure a smooth and successful installation process for your NTP server:

  • Two servers running Ubuntu 18.04 or higher: One configured as the NTP (Network Time Protocol) server, and the other as a client – This tutorial uses Ubuntu 20.04 servers. Still, the instructions are compatible with any server edition of Ubuntu 18.04 or newer.
  • A user with sudo privileges on both servers.

Installing and Configuring the Ubuntu NTP Server

Accurate time synchronization is vital, from maintaining consistent logs to ensuring smooth coordination between distributed systems. By correctly setting up your Ubuntu NTP server, you lay a solid foundation for efficient time management within your network infrastructure.

To install and configure your Ubuntu NTP server, carry out the following:

1. Open a terminal and execute the following apt update command to update your server’s package index.

sudo apt update -y
Updating the server's package index
Updating the server’s package index

2. Once updated, run the command below to install the NTP package.

sudo apt install ntp -y
Installing the NTP package
Installing the NTP package

3. Next, open NTP server’s configuration file (/etc/ntp.conf) in your preferred editor (i.e., nano or vim), and comment out the default servers.

As shown below, the default configuration uses various NTP pool servers, generally providing accurate time. But, you will achieve better synchronization by using time sources closer to your geographic location.

Commenting out the default servers
Commenting out the default servers

4. Now, open your favorite web browser, visit the NTP Pool Servers page, and pick servers from the list closest to your region for better performance.

Picking servers to add to the pool of servers
Picking servers to add to the pool of servers

5. Return to the NTP server’s configuration file, add your chosen servers underneath the commented default servers, save the changes, and close the editor.

Adding servers from the NTP Pool Project to your NTP configuration is not just about sprucing up your settings but casting a wider net for time accuracy.

Imagine your local machine as a cozy timekeeper in its own right. But by roping in these servers from all corners of the globe, managed by folks, you give your network’s time sync a serious upgrade.

Adding servers to the NTP server configuration file
Adding servers to the NTP server configuration file

6. With added servers, run the below systemctl command to restart the NTP service and apply your configuration changes.

This command has no output, but you will confirm the result later in the following step.

sudo systemctl restart ntp

7. Lastly, execute the following command to check the status of the NTP service.

sudo systemctl status ntp

If everything goes well, you will see an active (running) message in the output, which confirms your server is now acting as a reliable time source for your network.

Checking the status of the NTP service
Checking the status of the NTP service

Enabling and Securing the NTP Service Connection

With a fully configured NTP service, you must ensure a secure connection to the service by managing your firewall. By default, UFW on Ubuntu is configured to allow all outbound connections and deny all incoming connections, including incoming traffic, to the NTP service.

To enable NTP clients to synchronize time with your server, you must allow incoming traffic on the port used by NTP as follows:

Execute the following ufwcommand to create a UFW rule to allow traffic on UDP port 123, which is the port NTP uses for its communication.

This configuration ensures your server is protected by UFW while still allowing NTP clients to communicate with your NTP server for time synchronization.

sudo ufw allow 123/udp
Allowing incoming traffic to the NTP service
Allowing incoming traffic to the NTP service

Now, run the command below to check the detailed (verbose) status of the UFW firewall.

sudo ufw status verbose

The output indicates incoming connections on port 123 for UDP traffic are allowed, as shown below.

Checking the UFW firewall status
Checking the UFW firewall status

Installing and Configuring the Ubuntu NTP Client

Coming off the heels of securing your NTP service connection, you need a diligent timekeeper, syncing its clock with external NTP servers. You will set up an Ubuntu NTP client to ensure precise timekeeping.

To install and configure the Ubuntu NTP client, proceed with these steps:

1. On your client system, execute the following command to update the package lists and install the NTP client software.

sudo apt update -y && sudo apt install ntpdate -y && sudo apt install ntp -y
Update the package lists and install the NTP client software
Update the package lists and install the NTP client software

2. Next, switch to your NTP server and run the command below to display information about the NTP server’s network configuration and hostname.

hostname -I && hostname

Note the NTP server’s local IPv4 IP address (i.e., 192.168.1.3) and hostname (i.e., ntpserver), as you will need them to configure your NTP client.

Retrieving the NTP server’s local IP and the hostname
Retrieving the NTP server’s local IP and the hostname

3. Back on your client, open the hosts file (/etc/hosts) in your editor, add the following line to the end of the file, save the changes, and close the editor.

<Server_IP_Address> <Server_Hostname>

This hosts file update resolves the NTP server’s hostname to its IP address. Thus, ensure you replace <Server_IP_Address> and <Server_Hostname> with your NTP server’s IP address and hostname you noted in step two.

Ensuring hostname resolution
Ensuring hostname resolution

4. Now, open the NTP configuration file (/etc/ntp.conf) on your client machine, comment out the default servers, then add your own NTP server line as follows.

Substitute <Server_Hostname> with the actual hostname of your NTP server, save the changes, and exit the editor. This configuration allows your NTP client to resolve the hostname to the corresponding IP address when attempting to synchronize its time.

server <Server_Hostname>
Editing the NTP configuration file on the client machine
Editing the NTP configuration file on the client machine

5. Subsequently, run the command below to enable the NTP service on the client. Doing so ensures your NTP client synchronizes its time with the configured NTP server upon restart.

sudo systemctl enable ntp
Enabling the NTP service on the client
Enabling the NTP service on the client

6. Lastly, execute the following systemctl command to restart the NTP service on the client machine to apply the changes.

This command produces no output to the terminal but you will later verify the synchronization works.

sudo systemctl restart ntp

Verifying the NTP Synchronization Works

With all configurations done, you must ensure everything is ticking just right by diving into verifying the NTP synchronization works. You will verify your client machine is accurately adjusted according to the server.

To verify the NTP synchronization works, proceed as follows:

On your client machine, execute the ntpq command to query the NTP server status.

ntpq -p

The output shows a list of servers (NTP peers) your client communicates with, including the Ubuntu NTP server you set up earlier (ntpserver):

AttributeDetails
remoteThe IP address or hostname of your system’s NTP server or peer is synchronizing with.
refidThe reference ID of the peer could be an IP address or a special identifier representing the source of time synchronization for that peer.
stThe stratum level of the NTP server. Stratum 1 servers are directly connected to an authoritative time source (i.e., atomic clock), while Stratum 2 servers synchronize with Stratum 1 servers, and so on.
tIndicates the type of peer. For example: ‘l’ is the local reference clock, ‘u’ is the unspecified source, ‘s’ is the symmetric active mode peer, and ‘x’ is the discarded or unreachable peer.
whenIndicates the time in milliseconds since the last response from the peer was received.
pollThe poll interval represents how often your system sends packets to the peer to obtain time updates.
reachThe reachability register indicates the recent success or failure of requests to the peer.
delayThe estimated round-trip delay between your system and the peer (measured in milliseconds).
offsetThe time difference between your system’s and the peer’s clock (measured in milliseconds).
jitterThe estimated jitter or variability in the time measurements between your system and the peer (measured in milliseconds).
Verifying the NTP Synchronization
Verifying the NTP Synchronization

Conclusion

Successfully configuring an Ubuntu NTP server is not merely a technical necessity but an essential pillar for any robust network infrastructure. By harnessing the power of NTP, you are now equipped with the ability to orchestrate precise and unified timekeeping across all nodes within your digital realm.

But why stop here? Keep exploring different advanced configurations and features available through NTP to enhance timekeeping in your network. Why not dive deeper into the ntp.conf file and explore configuration directives at your disposal?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!