Configuring NTP

 

How to fix broken time in Linux

For purposes of this article, lets assume the date is April 7th, 2011. The issue we are seeing is the date on the server is wrong. As we can see there is def something funky going on here with the date/time between the OS and the server it is running on.

[root@nagios ~]# date
Sat Apr 16 01:28:07 PDT 2011
[root@nagios ~]# hwclock --show
Fri 15 Apr 2011 06:28:10 PM PDT  -0.734737 seconds

Traditionally you would reboot the server into the BIOS and update the time/date there. But in some cases this cannot be done. Enter NTP.

[root@nagios ~]# yum install ntp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.nwresd.org
 * updates: centos.mirror.facebook.net
 * addons: mirrors.kernel.org
 * extras: mirrors.kernel.org
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.2p1-9.el5.centos.2.1 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

========
 Package     Arch     Version     Repository     Size
========
Updating:
 ntp     x86_64     4.2.2p1-9.el5.centos.2.1     base     1.3 M

Transaction Summary
========
Install      0 Package(s)         
Update       1 Package(s)         
Remove       0 Package(s)         

Total download size: 1.3 M
Is this ok [y/N]: y
Downloading Packages:
ntp-4.2.2p1-9.el5.centos.2.1.x86_64.rpm                                                                                                                                                                          | 1.3 MB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating       : ntp                                               [1/2] 
  Cleanup        : ntp                                               [2/2] 

Updated: ntp.x86_64 0:4.2.2p1-9.el5.centos.2.1
Complete!

Now lets configure NTP. Before we enable the service, you need to contact the NTP server to update the time/date.

[root@nagios ~]# ntpdate pool.ntp.org
 7 Apr 11:12:36 ntpdate[5983]: step time server 149.20.68.17 offset -742667.876801 sec
[root@nagios ~]# date
Thu Apr  7 11:12:38 PDT 2011

Now that we know the Date/Time was off, you will most likely need to set the Timezone as well.

[root@nagios etc]# rm -rf localtime 
[root@nagios etc]# ln -sf /usr/share/zoneinfo/America/Denver /etc/localtime
[root@nagios etc]# ll localtime 
lrwxrwxrwx 1 root root 34 Apr  7 12:21 localtime -> /usr/share/zoneinfo/America/Denver

Now that the date is correct start the service and make sure it is enabled in the proper runlevels.

[root@nagios ~]# /etc/init.d/ntpd start
Starting ntpd:                                             [  OK  ]
[root@nagios ~]# date
Thu Apr  7 11:12:44 PDT 2011
[root@nagios ~]# chkconfig --level 345 ntpd on

 

Nothing makes a man more reverent than a library.
-- Winston Churchill