21 Jan

Sync linux server time with network time protocol(NTP) servers

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.

1
apt-get install ntp 

Sync linux server time with network time protocol(NTP) servers

To perform the installation in a Redhat or in a CentOS server, please execute the below command,

1
 yum install ntp

Once you are done with the NTP client installation, we need to edit the NTP configuration file /etc/ntp.conf

1
 vi /etc/ntp.conf

By default, you may find a list of NTP servers listed in the configuration file.

Sync linux server time with network time protocol(NTP) servers

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.

Sync linux server time with network time protocol(NTP) servers

For India location, you need to visit the link http://www.pool.ntp.org/zone/in

Sync linux server time with network time protocol(NTP) servers

Once the configuration changes are made, we need to restart the NTP service

1
 /etc/init.d/ntp restart

Sync linux server time with network time protocol(NTP) servers

To run the time synchronisation with  the NTP servers, please execute the below command, after stopping the NTP service.

1
 /usr/sbin/ntpdate pool.ntp.org

If the ntpdate is not installed in your server, then you can run the below command to install it.

1
apt-get install ntpdate

Sync linux server time with network time protocol(NTP) serversIf 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.

Sync linux server time with network time protocol(NTP) serversSo stop the service and execute the command again.

Sync linux server time with network time protocol(NTP) servers

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

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

1
 ntpq -pn
1
2
 -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.

Sync linux server time with network time protocol(NTP) serversTo know more about the utility, please refer the man page

1
man ntpq

NTPSTAT

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.

1
apt-get install ntpstat

Execute the below command to get the status of your NTP daemon,

1
ntpstat

The return value of ntpstat will tell you the status. Please execute the below command to get the return value,

1
echo $? 

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

1
man ntpstat

timedatectl

On a systemd based system, you can use the command timedatectl. The status can be checked by executing the below command,

1
 timedatectl status

If NTP enabled is set to No, then you can edit the systemd-timesyncd configuration file “/etc/systemd/timesyncd.conf” to change it.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

two + 20 =