How to Install and Configure NTP on Linux?
How to Install and Configure NTP on Linux?
Network Time Protocol (NTP) is a widely used protocol for synchronizing the time across computer systems using a reference clock on the network. NTP provides a Coordinated Universal Time to ensure consistent time synchronization.
NTP utilizes a set of algorithms and mechanisms to achieve accurate time synchronization. This algorithm is called clock discipline and has the following key processes,
- Measuring the offset between the local system clock and the reference time.
- Applying filtering algorithms to the measured clock offsets inaccurate readings caused by network delays or system load.
- Selecting the most accurate time sources based on their stability, reliability, and proximity.
- adjusts the system clock frequency to align it with the selected time sources for long-term accuracy.
The NTP has multiple servers that operate hierarchically. The hierarchy flows as,
- The primary time servers known as stratum 1 servers obtain accurate time from highly precise sources, such as atomic clocks or GPS receivers.
- The Stratum 2 servers synchronize their time with Stratum 1 servers, and the hierarchy continues down to lower stratum levels.
When a computer system wants to synchronize its time, the following process is done,
- The system sends a request to an NTP server.
- The server responds with the current time information, including the timestamp of when the server sent the response.
- The client adjusts its time based on the received information, considering network delays and clock inaccuracies.
In this article, we will guide you through the process of installing and configuring NTP on Linux, enabling you to keep your system's time accurate and in sync with other devices on the network.
Installing NTP on Linux
The steps to install NTP vary based on the Linux system you are using. Follow the respective steps for your Linux system,
For Ubuntu and Debian-based Linux distributions, you can use the apt package manager to install NTP. Open a terminal and follow these steps:
- Update the package list.
- Install the NTP package by executing the following command.
During the installation, you will be prompted to confirm. Press Y and hit Enter to proceed.
If you are using an older version of the apt package manager, replace apt with apt-get in the installation command.
For installing NTP on CentOS or Red Hat Enterprise Linux (RHEL) based systems, you can use the yum package manager. Open a terminal and follow these steps:
- Update the package list
- Install the NTP package by executing the following command.
Confirm the installation by typing y and hitting Enter if prompted.
For installing NTP on Fedora Linux, we can use the dnf package manager. Open a terminal and enter the following commands:
- Update the package list.
- Install NTP.
Confirm the installation if prompted for confirmation.
Configuring NTP on Linux
The configuration for NTP on Linux systems can be found in the /etc/ntp.conf file. Let us explore this file with the following sample file,
- The driftfile directive specifies the file where NTP stores the frequency offset or drift of the local clock over time.
- The server lines define the NTP servers that the local system will synchronize with. In the sample file, 0.pool.ntp.org, 1.pool.ntp.org, and 2.pool.ntp.org are used as the NTP server addresses. You can replace them with the NTP servers of your choice. NTP server addresses can be found in the npt pool website.
- The term iburst is an option used to enhance the initial synchronization process. When the iburst option is specified in the configuration, it instructs the NTP client to send a burst of eight packets to the server pool during the initial synchronization phase. This burst of packets helps in the synchronization process by obtaining time samples more quickly.
- We can also set the prefer option to prioritize a server in comparison with other servers for setting time.
- The restrict lines define access restrictions for NTP. The default restrict statement is used to help secure and control access to the NTP server by limiting various actions for the default restrictions. These various restrictions are,
- The kod directive is used to send a special response to clients that violate the NTP access restrictions. This is a mechanism to prevent abuse and protect NTP servers from unauthorized or excessive queries.
- The nomodify directive restricts clients from modifying the NTP configuration on the server. Clients with this restriction cannot make changes to the server's settings.
- The notrap directive prevents clients from remote configuration and control of the NTP server. With notrap set, clients cannot use this command to modify or interact with the server's operational variables.
- The nopeer directive prohibits clients from establishing a peer relationship with the NTP server. Peering is a mutual agreement between NTP servers to exchange time information and mutually synchronize their clocks.
- The noquery directive restricts clients from querying the NTP server for time information or status. With noquery set, clients cannot retrieve time data, system statistics, or other information from the server using NTP queries
In the sample file, it restricts modifications, traps, peering, and queries. The lines with restrict 127.0.0.1 and restrict -6 ::1 allow unrestricted access to NTP from the localhost (loopback) interface.
- The statistics lines enable NTP statistics logging. There are three types of logging,
- The loopstats file is used to log information about the local system's clock performance and behavior. It provides statistics on the frequency offset, jitter, and other parameters related to the system's clock stability.
- The peerstats file logs information about the NTP peers that the local system communicates with for time synchronization.
- The clockstats file is responsible for capturing information about the system's hardware clock or other reference clocks used for time synchronization.
- The subsequent filegen directives define the file paths and types for the generated statistics files.
- The logconfig line specifies the log configuration for NTP messages. The syncstatus, clockall, and peerall options determine the level of detail for the logged messages.
- The maxoffset directive sets the maximum time offset in seconds allowed before NTP adjusts the system clock. In the sample file, the maximum offset is set to 5 seconds.
- The tinker panic line sets the panic threshold to 0, which means NTP will not panic or stop adjusting the clock if the time offset exceeds the maximum offset defined.
To configure NTP on Linux, we have to make modifications to the /etc/ntp.conf file to specify NTP server addresses, define access restrictions, enable logging, and set other parameters. Follow these steps to configure NTP on Linux,
- Open the /etc/ntp.conf file using a text editor. For example, you can use the nano editor with the following command:
2. Locate the server directives in the file. These lines specify the NTP servers that your system will synchronize with. Remember that you have to select the server of your timezone and closer to your region. The list of server names for different regions can be found in the ntp pool website. Replace the default server addresses with the NTP servers copied from the NTP pool website. For Asia, the following values can be used:
- Set access restrictions by modifying the restrict directives. This helps control who can query or modify your NTP server. By default, the sample file includes restrictions to prevent modifications and peering. You can customize these directives based on your requirements.
- Enable NTP statistics logging by uncommenting the statistics and filegen directives. These lines enable the collection of various NTP statistics and specify the file paths for storing the statistics data.
- Save the changes and exit the text editor. In nano, press Ctrl+X, followed by Y, and Enter to save the changes to the file.
Using NTP in Linux
Once NTP is configured, you can start using it to synchronize the time on your Linux system. Here's how you can utilize NTP in Linux,
- Restart the NTP service to apply the changes made in the configuration file:
- Verify the status of the NTP service to ensure it is running correctly:
We can also use the script provided with the NTP installation to start the daemon of the NTP service. The following command is used to perform this,
There are also several other methods to check the status of NTP service on Linux systems. Some of the commands in these methods are available by default in Linux systems and some of them have to be installed. The methods are listed below.
The ntpstat command provides a simple way to check the synchronization status of the local NTP server. It displays a summary indicating whether the server is synchronized or unsynchronized. If synchronized, it also shows the time difference (offset) between the local server and the reference time source.
The ntpq command is a powerful utility for querying and monitoring NTP servers. It provides detailed information about the NTP server's peers, associations, offset, delay, jitter, and other statistics.
The chronyc command is another powerful utility that provides extensive control and monitoring capabilities for NTP servers. It allows you to query the NTP server's status, sources, reference IDs, offset, and more.
The timedatectl command provides information about the system's date, time, and time zone settings. It also indicates whether NTP synchronization is enabled or disabled.
If the NTP service is active and running without errors, the time synchronization will occur automatically in the background of your system. NTP will periodically adjust the system clock to align it with the NTP servers, compensating for clock drift and keeping your system's time accurate.
Learn More
- If you are getting started on Linux and want to improve your skills, you can check out this reading track regarding Linux.
- Expand your Linux system administration skills by understanding the authentication mechanism in Linux. Learn more about authorization in Linux.
- A good understanding of different editors and knowledge of how to use them can help in working with configuration files. You can understand different ways to modify a configuration file in this article.
- To learn deeper about the topic and explore advanced NTP configurations and options, you can also refer to the official NTP documentation. Before starting on exploring other services on Linux, refer to the Linux OS article for a complete overview of Linux systems.
Conclusion
- NTP (Network Time Protocol) is used to synchronize time across networked devices in Linux.
- Installing NTP involves using the package manager specific to your Linux distribution to install the NTP software.
- The /etc/ntp.conf file is the main configuration file for NTP on Linux. It contains directives for server addresses, access restrictions, logging, and more.
- Configuring NTP on Linux involves modifying the /etc/ntp.conf file to specify NTP server addresses, and access restrictions, and enabling logging.
- The kod, nomodify, notrap, nopeer, and noquery directives in the NTP configuration file control access and restrictions.
- Using NTP in Linux requires restarting the NTP service and verifying its status using commands like ntpstat, ntpq, chronyc, systemctl, and timedatectl.
- To check the NTP server status, use commands like ntpstat, ntpq -p or systemctl status ntp, and timedatectl status.
- The iburst option in server pools sends a burst of packets during initial synchronization to expedite the process. The preferred option is used to increase the priority of a server.