Configure accurate time for Compute Engine VMs

Linux

Many software systems that depend on careful sequencing of events rely on astable, consistent system clock, using system logs with timestamps to ensuretime synchronization and debug issues as they occur. To help keep system clocksin sync, Compute Engine virtual machine (VM) instances are preconfigured tousenetwork time protocol (NTP), a bundled solution of time synchronizationhardware and software. If ensuring accurate time synchronization and monitoringthe accuracy of your time synchronization are important for your goals, you canconfigureaccurate time instead of NTP, to sync your VM's clock with the hostclock by usingchrony andptp_kvm. This configuration is designed to achieveaccuracy within 1 ms for supported setups.

Supported machine types

The following machine types support accurate time:

Supported operating systems

The following operating systems (OSes) support accurate time:

OSVersionsImages
CentOS Stream9centos-stream-9
Container-Optimized OSCOS 105 LTS,COS 109 LTS,COS 113 LTS,COS 117 LTScos-105-lts, cos-109-lts,cos-113-lts, cos-117-lts
Debian11 (Bullseye),12 (Bookworm)debian-11, debian-12
Fedora Cloud39fedora-cloud-39
RHEL8, 9rhel-8-4-sap-ha,rhel-8-6-sap-ha,rhel-8-8-sap-ha,rhel-8-10-sap-ha,rhel-9, rhel-9-0-sap-ha,rhel-9-2-sap-ha,rhel-9-4-sap-ha
Rocky Linux8, 9rocky-linux-8,rocky-linux-8-optimized-gcp,rocky-linux-9-optimized-gcp,rocky-linux-9-optimized-gcp
SLES15sles-15, sles-15-sp2-byos,sles-15-sp2-sap,sles-15-sp3-byos,sles-15-sp3-sap,sles-15-sp4-byos,sles-15-sp4-sap,sles-15-sp5-byos,sles-15-sp5-sap
Ubuntu22.04 LTS(JammyJellyfish),24.04 LTS(Noble Numbat)ubuntu-2204-lts,ubuntu-2404-lts-amd64
Ubuntu Pro2004ubuntu-pro-2004-lts,ubuntu-pro-2004-lts-amd64

Supported zones

The following zones support accurate time:

ZoneLocation
europe-west1-bSt. Ghislain, Belgium, Europe
europe-west1-cSt. Ghislain, Belgium, Europe
europe-west2-bLondon, England, Europe
europe-west3-aFrankfurt, Germany, Europe
us-central1-aCouncil Bluffs, Iowa, North America
us-central1-bCouncil Bluffs, Iowa, North America
us-central1-cCouncil Bluffs, Iowa, North America
us-central1-fCouncil Bluffs, Iowa, North America
us-east1-bMoncks Corner, South Carolina, NorthAmerica
us-east1-cMoncks Corner, South Carolina, NorthAmerica
us-east4-cAshburn, Virginia, North America
us-east5-aColumbus, Ohio, North America
us-south1-aDallas, Texas, North America
us-west1-bThe Dalles, Oregon, North America
us-west2-aLos Angeles, California, NorthAmerica
us-west3-aSalt Lake City, Utah, North America

Configure accurate time synchronization

To configure accurate time synchronization for your project's VMs, complete thefollowing tasks for each VM:

  • Configurechrony to useptp-kvm as its time source.
  • Configure Google Cloud Ops Agent for data collection and analysis.

After you've completed both tasks, accurate time synchronization is set up for theVMs in your project.

For a sample script that creates a VM and completes both tasks to configureaccurate time synchronization, see theVM creation scriptin GitHub.

Configurechrony to useptp-kvm

To configurechrony to useptp-kvm as its time source, run the followingscript inside each of your Google Cloud project's VMs:

#!/bin/bash# Install chrony as neededif!command-vchronyc&>/dev/null;then# Detect the package manager and install chronyifcommand-vapt&>/dev/null;then# Debian, Ubuntu, and derivativesecho"Detected apt. Installing chrony..."apt-getupdateapt-getinstall-ychronyelifcommand-vdnf&>/dev/null;then# Fedora, RHEL 8+, CentOS 8+echo"Detected dnf. Installing chrony..."dnfinstall-ychronyelifcommand-vyum&>/dev/null;then# RHEL 7, CentOS 7echo"Detected yum. Installing chrony..."yuminstall-ychronyelifcommand-vzypper&>/dev/null;then# openSUSE, SLESecho"Detected zypper. Installing chrony..."zypperinstall-ychronyelseecho"Please install chrony manually."exit1fifi# Different distros place chrony config in# different locations, detect this.if[-f"/etc/chrony/chrony.conf"];thenCHRONY_CONF="/etc/chrony/chrony.conf"elseCHRONY_CONF="/etc/chrony.conf"fi# Load PTP-KVM clock for high-accuracy clock synchronization# PTP-KVM allows the VM to read a cross time-stamp of a platform# provided clock and the VM CPU Clock (CycleCounter), providing# resiliency from network and virtualization variability when# synchronizing the realtime/wall clock/sbin/modprobeptp_kvmecho"ptp_kvm">/etc/modules-load.d/ptp_kvm.conf# NTP servers might indicate the wrong time due to chrony# greatly reducing the polling frequency, resulting in# overall negative impact to the clock synchronization# achieved, especially after live migration events.## We disable NTP servers to prevent these issues.## Customers interested in monitoring the clock accuracy compared to NTP sources# should run a second instance of chrony in a no-change mode to do so.# Disable NTP servers:sed-i'/^server /d'$CHRONY_CONFsed-i'/^pool /d'$CHRONY_CONFsed-i'/^include /d'$CHRONY_CONF#Disable DHCP based NTP config:sed-i's/^NETCONFIG_NTP_POLICY="auto"/NETCONFIG_NTP_POLICY=""/'/etc/sysconfig/network/configtruncate-s0/var/run/netconfig/chrony.serversechoPEERNTP=no|sudotee-a/etc/sysconfig/networkserviceNetworkManagerrestartsystemctldisablesystemd-timesyncd.servicetimedatectlset-ntpfalse# Configure PTP-KVM based HW refclock, with leap second smearing# Google's clocks are doing leap second smearing, and therefor chrony shouldn't attempt to adjust# the time received from PTP-KVM to adjust for leap seconds.sed"s/^leapsectz/#leapsectz/"-i$CHRONY_CONFecho"refclock PHC /dev/ptp_kvm poll -1">>$CHRONY_CONF# For extra debugging logging, uncomment the following line#echo "log measurements statistics tracking" >> $CHRONY_CONF# Enable chrony's clock accuracy tracking log,# for monitoring and auditing.echo"log tracking">>$CHRONY_CONF# Restart chrony (Ubuntu named it differently)systemctlrestartchronydsystemctlrestartchrony

Configure Google Cloud Ops Agent on your VM

To configure Google Cloud Ops Agent for data collection and analysis, runthe following script inside each of your Google Cloud project's VMs:

#!/bin/bash# From https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/installation#install-latest-versioncurl-sSOhttps://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.shbashadd-google-cloud-ops-agent-repo.sh--also-installOPS_AGENT_CONF="logging:    receivers:        chrony_tracking_receiver:            type: files            include_paths:                - /var/log/chrony/tracking.log    processors:        chrony_tracking_processor:            type: parse_regex            regex: \"^.*PHC0.*  (?<max_error>[-\d\.eE]+)$\"    service:        pipelines:            chrony_tracking_pipeline:                receivers: [chrony_tracking_receiver]                processors: [chrony_tracking_processor]"OPS_AGENT_CONF_PATH="/etc/google-cloud-ops-agent/config.yaml"echo"$OPS_AGENT_CONF">"$OPS_AGENT_CONF_PATH"systemctlrestartgoogle-cloud-ops-agent

Configure time synchronization monitoring

To configure time synchronization monitoring for your Google Cloudproject's VMs, run the logging and dashboard setup script for yourGoogle Cloud project. This script helps you to complete the followingtasks for your Google Cloud project:

  • It sets appropriate permissions on the service account associated with yourVM's Google Cloud project.
  • It creates a log-based metric thatchrony uses to ensure accuracy betweenthe clocks on the VM and its host server.
  • It creates a dashboard measuring the VM clock's traceability to UTC bycombining the following metrics:

To accomplish the preceding tasks, run the following script:

#!/bin/bashif[-z"$1"];thenecho"Usage: time-sync-logging-dashboard.sh <project_id>">&2exit1fiPROJECT_ID="$1"PROJECT_NUMBER=$(gcloudprojectsdescribe"$PROJECT_ID"--format="value(projectNumber)")SERVICE_ACCOUNT_EMAIL=${PROJECT_NUMBER}-compute@developer.gserviceaccount.comgcloudprojectsadd-iam-policy-binding"${PROJECT_ID}"\--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}"\--role="roles/compute.instanceAdmin"gcloudprojectsadd-iam-policy-binding"${PROJECT_ID}"\--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}"\--role="roles/monitoring.metricWriter"gcloudprojectsadd-iam-policy-binding"${PROJECT_ID}"\--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}"\--role="roles/logging.logWriter"cpclock-error-metric.json/tmp/clock-error-metric.jsonsed-i"s/PROJECT_ID/${PROJECT_ID}/"/tmp/clock-error-metric.jsongcloudloggingmetricscreate--project"${PROJECT_ID}"phc-clock-max-error-gce--config-from-file=/tmp/clock-error-metric.jsongcloudmonitoringdashboardscreate--project"${PROJECT_ID}"--config-from-file=metric-dashboard.json

After the script completes running, use the dashboard it created to view clockaccuracy data for your project's VMs.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.