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
2. Once updated, run the command below to install
the NTP package.
sudo apt install ntp -y
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.
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.
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.
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.
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 ufw
command 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
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.
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
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.
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.
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>
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
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):
Attribute | Details |
---|---|
remote | The IP address or hostname of your system’s NTP server or peer is synchronizing with. |
refid | The reference ID of the peer could be an IP address or a special identifier representing the source of time synchronization for that peer. |
st | The 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. |
t | Indicates 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. |
when | Indicates the time in milliseconds since the last response from the peer was received. |
poll | The poll interval represents how often your system sends packets to the peer to obtain time updates. |
reach | The reachability register indicates the recent success or failure of requests to the peer. |
delay | The estimated round-trip delay between your system and the peer (measured in milliseconds). |
offset | The time difference between your system’s and the peer’s clock (measured in milliseconds). |
jitter | The estimated jitter or variability in the time measurements between your system and the peer (measured in milliseconds). |
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?