Synchronize system time with a time server on the Internet

I usually run Ubuntu servers as development servers in VMware Player. And I frequently suspend/hibernate my host OS, my laptop. To my surprise Ubuntu as a guest OS is very tolerant for these interruptions. It hardly ever fails to work. Only downside is that the system time gets confused. This makes log entries less usable, because it is hard to guess the actual time. Therefore I install an ntp service, to syncronize the system time with some time servers on the Internet.
Special thanks to <a href=”; target=”_blank”>Anil Kumar</a>, find below his description how to make it work:

1. First, make sure NTP is intstalled.

sudo apt-get install ntpdate

2. Then, run ntpdate, specifying an NTP server:

sudo ntpdate

This should set the time correctly for that session. Note that superuser previleges are necessary to set the time, because setting time is really an “administrative task”.

3. Now you shall want this to run this regularly, right? Since it’s a server, I’d prefer to run it at startup. To do this, we shall add a little script to the startup.

First, open a terminal, and pass this command:

sudo nano /etc/init.d/ntpdate

In the blank file that comes up, copy and paste this:

#! /bin/sh
echo "Synchronizing system time with Ubuntu Servers..."
echo "Setting hardware clock to updated time..."
hwclock --systohc

Note the last line: It resets the hardware time (“BIOS time”) to the system time. Press Ctrl+O to write out this file, and Ctrl+X to quit nano. Now we shall make this little script executable. To do this,

sudo chmod 700 /etc/init.d/ntpdate

And finally, let’s inform the system about this new startup script we just added:

sudo update-rc.d ntpdate defaults 90

And you’re done.

To test this, you must restart your system, and keep a close watch on the messages being shown on the system. Alternatively, you could also read the system log files later (/var/log/syslog)