Fix: Ubuntu Server Time is Wrong Tips, Tricks and Tutorials 13 MAY 2014

One of our clients noted that audits he was doing via our Kinetica feedback system was reflecting timestamps that didn’t quite match up with the current date/time – and when it comes to clock accuracy he should know, he works at Manchester Airport!

So I quickly hopped on to the Ubuntu server install to run a date command, and as suggested, the server time was indeed off by a few minutes.

A quick Google search lead me here, and after following the suggested steps, the problem was resolved. So what exactly was the issue then? Well if you Google search terms like “ubuntu server time is wrong”, “ubuntu server time is off”, “ubuntu server time is out” you’ll see that there are more than just a few forum posts, queries and blogs dealing with the matter. The general consensus is it might be related to the fact that the hardware clock is only read at system boot, and an aging battery may mean the loss of of accuracy over time.

Nevertheless, by relying on the NTP protocol systems this is an issue easily solved.

ubuntu orange logo

NTP is a TCP/IP protocol for synchronising time over a network. Basically a client requests the current time from a server, and uses it to set its own clock.

The system itself is pretty complex, with multiple tiers of NTP servers, the primary servers which are hooked up to atomic clocks (often via GPS) and second and third tier servers which are then used to spreading the load of Internet requests handling. On the client side, the software has to factor out communication delays and adjust the time in a way that does not upset all the other processes that run on the server, though luckily for us, pretty much all of this happens behind the scenes.

Anyway, Ubuntu has two ways of automatically setting your time, namely ntpdate and nptd.

Ubuntu comes with ntpdate as standard, and will run it once at boot time to set up your time according to Ubuntu’s NTP server. However, a server’s clock is likely to drift considerably between reboots, so it makes sense to correct the time occasionally. The easiest way to do this is to get cron to run ntpdate every day, and to do this we’ll make use of Ubuntu Server’s daily cron folder mechanism to do this. First create a file in /etc/cron.daily/ and make this file executable:

sudo nano /etc/cron.daily/ntpdate
sudo chmod 755 /etc/cron.daily/ntpdate

Next, set the contents of the file to:

ntpdate ntp.ubuntu.com pool.ntp.org

Done. Now ntpdate is a bit of a blunt instrument – it can only adjust the time once a day, in one big correction. The ntp daemon ntpd is far more subtle. It calculates the drift of your system clock and continuously adjusts it, so there are no large corrections that could lead to inconsistent logs for instance. The cost is a little processing power and memory, but for a modern server this is fairly negligible.

Install the daemon as you would any other:

sudo apt-get install ntp

Note, you can make the NTP requests more resilient by adding more servers to increase accuracy and resilience, and you may want to use time servers that are geographically closer to you. To add servers to your ntpd setup, edit the /etc/ntp.conf config file to include the following server lines:

server ntp.ubuntu.com
server pool.ntp.org

Although you don’t have to have both setup on your server, having the two of them working in conjunction does give you a bit of a fallback, which is always a good thing.

Related Link: Time Synchronisation with NTP

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.