Ubuntu inside virtual machine: ntpd or ntpdate or ??? to avoid clock drift
Problem: ntpd
, when installed on a virtual machine running Ubuntu (I've had this on hosts with a variety of hypervisors such as KVM and VMware ESX) seem to be prone to severe clock drift. I haven't found any good method and not everyone seems to experience this.
Question: How can I prevent this? Are there any settings in /etc/ntp.conf
to force ntpd
to be more aggressive in synchronizing the time inside the VM or should I use ntpdate -b timeserver
instead inside a cron
job running every so often?
Which method is recommended to avoid leaps when using ntpdate -b
or clock drift in general inside a Ubuntu virtual machine?
Side-note: I've seen similar effects on vios (IBM hypervisor) slaves running AIX as well as on Solaris. So the problem is very real and appears to be related to hypervisors in general.
Solution 1:
I observed similar symptoms on a Linux machine running on VMware ESX. It seems that the drift (several minutes per day) was caused by having time synchronization both provided by NTP inside the VM and VMware tools. We solved our problem by disabling time synchronization in the VMware tools properties (in the VM properties in ESX). After that NTP was able to keep the clock in synch with a remote time server. If you prefer to edit the .vmx
file manually, set tools.syncTime = "0"
.
Stopping NTP inside the VM and letting the tools synch the time would probably work as well, at least to correct the server drift. ESX itself can run an NTP daemon. VMware's timekeeping best practices recommend using NTP instead of VMWare Tools for time synchronization.
VMware publishes a lengthy paper on timekeeping in VMware virtual machines. The most relevant passage is the subsection “Clocksource Kernels”. The VMware KB also has troubleshooting tips for timekeeping in Linux guests.
Solution 2:
In VirtualBox at least, you can... I had time sync problems before, but tuning the Guest Additions time synchronization parameters seemed to fix it.
Here is relevant text from the manual:
Tuning the Guest Additions time synchronization parameters
The VirtualBox Guest Additions ensure that the guest's system time is synchronized with the host time. There are several parameters which can be tuned. The parameters can be set for a specific VM using the following command:
VBoxManage guestproperty set "VM name" "/VirtualBox/GuestAdd/VBoxService/PARAMETER" VALUE
where PARAMETER is one of the following:
--timesync-interval
Specifies the interval at which to synchronize the time with the host. The default is 10000 ms (10 seconds).
--timesync-min-adjust
The minimum absolute drift value measured in milliseconds to make adjustments for. The default is 1000 ms on OS/2 and 100 ms elsewhere.
--timesync-latency-factor
The factor to multiply the time query latency with to calculate the dynamic minimum adjust time. The default is 8 times, that means in detail: Measure the time it takes to determine the host time (the guest has to contact the VM host service which may take some time), multiply this value by 8 and do an adjustment only if the time difference between host and guest is bigger than this value. Don't do any time adjustment otherwise.
--timesync-max-latency
The max host timer query latency to accept. The default is 250 ms.
--timesync-set-threshold
The absolute drift threshold, given as milliseconds where to start setting the time instead of trying to smoothly adjust it. The default is 20 minutes.
--timesync-set-start
Set the time when starting the time sync service.
--timesync-set-on-restore 0|1
Set the time after the VM was restored from a saved state when passing 1 as parameter (default). Disable by passing 0. In the latter case, the time will be adjusted smoothly which can take a long time.All these parameters can be specified as command line parameters to
VBoxService
as well.Disabling the Guest Additions time synchronization
Once installed and started, the VirtualBox Guest Additions will try to synchronize the guest time with the host time. This can be prevented by forbidding the guest service from reading the host clock:
VBoxManage setextradata "VM name" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled"