Sync linux server time with network time protocol(NTP) servers
To sync linux server time with network time protocol(NTP) servers, you need to have the NTP client installed in your machine. To perform the installation in an Ubuntu server, please execute the below command.
apt-get install ntp
To perform the installation in a Redhat or in a CentOS server, please execute the below command,
yum install ntp
Once you are done with the NTP client installation, we need to edit the NTP configuration file /etc/ntp.conf
By default, you may find a list of NTP servers listed in the configuration file.
If you wish to change the default values to the ones closer to your location, please visit the NTP site http://www.pool.ntp.org/. You will see the list of NTP servers for each time zone listed at the NTP portal.
For India location, you need to visit the link http://www.pool.ntp.org/zone/in
Once the configuration changes are made, we need to restart the NTP service
To run the time synchronisation with the NTP servers, please execute the below command, after stopping the NTP service.
If the ntpdate is not installed in your server, then you can run the below command to install it.
apt-get install ntpdate
If you are facing the error “the NTP socket is in use, exiting“, upon executing the above command, which points to the issue that the NTP service is still running.
So stop the service and execute the command again.
If you are encountering the error “no server suitable for synchronization found“, please check your firewall settings. Please ensure that, the UDP port 123 is enabled.
Verify the NTP client status
To verify the NTP client status, three major utilities can be used,
NTPQ is a standard NTP Query program, which is used to monitor NTP daemon ntpd operations and analyse its performance.
Run the below command to obtain the current status of ntp
-n : Output all host addresses in dotted-quad numeric format rather than converting to the canonical host names.
-p : Print a list of the peers known to the server as well as a summary of their state. This is equivalent to the peers interactive command.
To know more about the utility, please refer the man page
The ntpstat utility will display the network time synchronisation status. If your server is synchronised to reference NTP value, then the ntpstat command will return the approximate time accuracy.
If the ntpstat is not installed in your machine, please execute the below command to install it.
apt-get install ntpstat
Execute the below command to get the status of your NTP daemon,
The return value of ntpstat will tell you the status. Please execute the below command to get the return value,
The clock is synchronised, if the return value is “0”. If the ntpstat return value is “1”, then the clock is not synchronised. If the return value is “2”, then the clock is indeterminant, Eg : If ntp is not reachable.
To know more about the ntpstat usage, please refer its man page
On a systemd based system, you can use the command timedatectl. The status can be checked by executing the below command,
If NTP enabled is set to No, then you can edit the systemd-timesyncd configuration file “/etc/systemd/timesyncd.conf” to change it.