How to sync system time with NTP server?

This is small howto on how to setup ntp sync on a Linux box. Before going to do this we should know why we require to sync with NTP server. NTP is a network time protocol server used to sync time across the machines. When we setup time locally using date command there are chances that the time is out of sync in your network. In order to make the time consistent we have to sync to a centralized time server which will take care of updating our machine time when we try to sync. 

Step1: Install ntp on Linux(Redhat flavors/Debian flavors)

Redhat based machines(Centos/Redhat/Fedora):

	yum install ntp

Output:

	[root@node1 ssl]# yum install ntp -y
Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=i386 error was
14: problem making ssl connection
 * base: centos.mirror.uber.com.au
 * epel: mirror.as24220.net
 * extras: centos.mirror.serversaustralia.com.au
 * updates: centos.mirror.uber.com.au
base                                                                                                                                                                                            | 3.7 kB     00:00     
extras                                                                                                                                                                                          | 3.3 kB     00:00     
updates                                                                                                                                                                                         | 3.4 kB     00:00     
updates/primary_db                                                                                                                                                                              | 812 kB     00:00     
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ntp.i686 0:4.2.6p5-1.el6.centos set to be updated
--> Processing Dependency: ntpdate = 4.2.6p5-1.el6.centos for package: ntp-4.2.6p5-1.el6.centos.i686
--> Running transaction check
---> Package ntpdate.i686 0:4.2.6p5-1.el6.centos set to be updated
--> Finished Dependency Resolution
Dependencies Resolved =======================================================================================================================================================================================================================
 Package                                           Arch                                           Version                                                         Repository                                      Size
=======================================================================================================================================================================================================================
Updating:
 ntp                                               i686                                           4.2.6p5-1.el6.centos                                            base                                           586 k
Updating for dependencies:
 ntpdate                                           i686                                           4.2.6p5-1.el6.centos                                            base                                            74 k
Transaction Summary
=======================================================================================================================================================================================================================
Install       0 Package(s)
Upgrade       2 Package(s)
Total download size: 660 k
Downloading Packages:
(1/2): ntp-4.2.6p5-1.el6.centos.i686.rpm                                                                                                                                                        | 586 kB     00:00     
(2/2): ntpdate-4.2.6p5-1.el6.centos.i686.rpm                                                                                                                                                    |  74 kB     00:00     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                  675 kB/s | 660 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating       : ntpdate-4.2.6p5-1.el6.centos.i686                                                                                                                                                               1/4 
  Updating       : ntp-4.2.6p5-1.el6.centos.i686                                                                                                                                                                   2/4 
  Cleanup        : ntp-4.2.4p8-2.el6.i686                                                                                                                                                                          3/4 
  Cleanup        : ntpdate-4.2.4p8-2.el6.i686                                                                                                                                                                      4/4 
Updated:
  ntp.i686 0:4.2.6p5-1.el6.centos                                                                                                                                                                                      
Dependency Updated:
  ntpdate.i686 0:4.2.6p5-1.el6.centos                                                                                                                                                                                  
Complete!


Debian based machiens(Ubuntu):

	apt-get install ntp

Output:

	root@linuxnix:/home/surendra# apt-get install ntp
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libopts25
Suggested packages:
  ntp-doc
The following NEW packages will be installed:
  libopts25 ntp
0 upgraded, 2 newly installed, 0 to remove and 23 not upgraded.
Need to get 666 kB of archives.
After this operation, 1,668 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu/ trusty/main libopts25 amd64 1:5.18-2ubuntu2 [55.3 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu/ trusty/main ntp amd64 1:4.2.6.p5+dfsg-3ubuntu2 [611 kB]
Fetched 666 kB in 3s (199 kB/s)
Selecting the previously unselected package libopts25:amd64.
(Reading database ... 446676 files and directories currently installed.)
Preparing to unpack .../libopts25_1%3a5.18-2ubuntu2_amd64.deb ...
Unpacking libopts25:amd64 (1:5.18-2ubuntu2) ...
Selecting the previously unselected package ntp.
Preparing to unpack .../ntp_1%3a4.2.6.p5+dfsg-3ubuntu2_amd64.deb ...
Unpacking ntp (1:4.2.6.p5+dfsg-3ubuntu2) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Setting up libopts25:amd64 (1:5.18-2ubuntu2) ...
Setting up ntp (1:4.2.6.p5+dfsg-3ubuntu2) ...
 * Starting NTP server ntpd                                                                                                                                                                                     [ OK ] 
Processing triggers for libc-bin (2.19-0ubuntu6.3) ...
Processing triggers for ureadahead (0.100.0-16) ...

Step2: Sync local system time with NTP server. This can be done in two ways.

1)Permanent way where we have to edit /etc/ntp.conf file and give server entry as pool.ntp.org as shown below.

	server pool.ntp.org

Save the file and restart ntp service

	service ntpd restart
	chkconfig ntpd on

Output:

	[root@node1 ssl]# service ntpd restart
Shutting down ntpd:                                        [  OK  ]
Starting ntpd:                                             [  OK  ]


2) Instentanious syncing and a temporary way to sync to an NTP server

	/usr/sbin/ntpdate pool.ntp.org

Output:

	21 Nov 03:18:44 ntpdate[20473]: step time server 129.250.35.250 offset 4286060.163705 sec

In our next post we will see on how to set timezone in Linux.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.