Virtual PTP hardware clock on KVM guests

Written at evening time in English • Tags: , ,

Using a virtual PTP hardware clock with the ptp_kvm driver and chrony is an easy solution for keeping time on guest machines synchronized to their host. Ideally the host is already keeping accurate time by tracking a set of NTP servers, but it is still probably a good idea to still add some network NTP peers on guests as well.

Load the driver on the running system to check that it exists:

modprobe ptp_kvm

If there were no errors, configure the driver to be loaded on every boot:

echo ptp_kvm > /etc/modules-load.d/ptp_kvm.conf

Configure chrony to use the virtual clock by adding the following line to /etc/chrony/chrony.conf:

refclock PHC /dev/ptp0 poll 2 stratum 14

Setting the stratum of the clock to 14 makes it very unlikely that any network clients would select this system as their time source over the network. (By default chrony does not even listen to NTP requests.)

The configuration provided by the chrony package probably already has a pool statement in it, in which case you can leave it as is. Otherwise, the following pool statement will give you a random set of NTP servers geographically close to you. A single pool statement adds multiple servers from the NTP Pool Project. The “2” host is used to obtain both IPv4 and IPv6 addresses.

pool iburst

Finally, restart the chrony service:

service chrony restart

To see the time sources currently being considered:

chronyc sources -v