Monday, December 31, 2012

Fixing clock time in Ubuntu

When you manage multiple systems across multiple time zones, it is inevitable the clocks will drift apart. And while I thought I was still loading NTP onto my work boxes (I've been loading ntpdate to use to force updates when needed), I was apparently mistaken. One other issue: BIOS hardware clocks; some may be set to UTC; some to a timezone other than where it was sent to. So I wrote a script to make sure the system clock was fixed correctly: this is best deployed as something like "" you can copy with scp + run as sudo....

apt-get -y install ntp
service ntp stop
dpkg-reconfigure tzdata
ntpd -g -q
service ntp start
hwclock -w

Explanation of what it does...

* Installs NTP client: default Ubuntu settings use the NTP pool.
* Stop NTP client service: attempting forced NTP updates fail otherwise (open socket error).
* Make sure the correct time zone is set (a menu will let you set this).
* Force synchronize via NTP: otherwise, a slew system forces it to update in increments over long periods of time. Fine for operation, but not for inital correction.
* Restart NTP service.
* Force hardware clock to use the system time pulled from NTP.


* NTPD on Wikipedia
* hwclock man page

No comments:

Post a Comment