Keeping accurate time is critical for many computer systems. Accurate timekeeping is particularly important for servers, which often need to perform tasks at specific times or coordinate with other systems. The Network Time Protocol (NTP) is a protocol for synchronizing computer clocks over a network. In this article, we’ll cover how to configure NTP on Linux systems.
Installing NTP
Before configuring NTP on our Linux system, firstly we need to install the ntp
package. For example, on Ubuntu or Debian, we can install the ntp
package with the following command. It will automatically synchronize your clock with the NTP servers.
sudo apt-get install ntp
Configuring NTP
The NTP configuration file is usually located at /etc/ntp.conf
. We can edit this file using a text editor such as nano
or vi
. Here’s an example configuration file:
# /etc/ntp.conf
# Use servers from the NTP Pool Project.
pool 0.pool.ntp.org iburst
pool 1.pool.ntp.org iburst
pool 2.pool.ntp.org iburst
pool 3.pool.ntp.org iburst
# By default, exchange time with everybody, but don't allow configuration.
restrict default nomodify notrap nopeer noquery
# Allow localhost to use NTP service
restrict 127.0.0.1
# Allow specific networks to use NTP service
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap
# Use the driftfile to record the frequency offset of the local clock oscillator.
driftfile /var/lib/ntp/ntp.drift
In this example, we’re using the NTP Pool Project servers to synchronize our clock. The pool
directive specifies a list of NTP servers to use. The restrict
directives specify which hosts or networks are allowed to access the NTP service. Also, the restrict default
directive restricts all access to the NTP service, except for localhost. The driftfile
directive specifies the path to the file where the local clock frequency offset is stored.
After editing the ntp.conf
file, we need to restart the NTP service using the following command:
sudo systemctl restart ntp
Verifying NTP Configuration
Once we’ve configured NTP, we need to verify that it’s working properly. We can do this by checking the status of the NTP service:
ntpq -p
And the output:
remote refid st t when poll reach delay offset jitter
==============================================================================
+0.pool.ntp.org 193.79.237.14 2 u 39 64 7 77.076 0.667 1.582
*1.pool.ntp.org 195.141.242.34 2 u 49 64 7 77.614 -0.220 1.699
+2.pool.ntp.org 5.196.80.216 2 u 47 64 7 79.625 0.646 1.249
+3.pool.ntp.org 194.190.168.1 2 u 46 64 7 71.129 -0.151 1.819
We can see the NTP servers that we have specified in the ntp.conf
file.
We can also use the date
command to check the system time:
date
This command will show the current date and time. If NTP is working properly, the system time should be accurate.
Your article helped me a lot, is there any more related content? Thanks!