Troubleshooting NTP Sync Issues on Ubuntu 20.04 KVM Host: Fixing System Clock and Timesyncd Conflicts


2 views

When working with KVM virtualization on Ubuntu 20.04, accurate time synchronization is crucial - especially when dealing with PCI passthrough configurations. The standard Ubuntu stack uses systemd-timesyncd by default, but many sysadmins prefer the full NTP implementation for better precision.

sudo timedatectl status
               Local time: Fri 2020-07-10 09:14:14 EDT  
           Universal time: Fri 2020-07-10 13:14:14 UTC  
System clock synchronized: no                           
              NTP service: n/a

This output indicates the system isn't syncing with any time servers. The first thing to check is whether we have conflicting time services:

sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service
     Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)
     Active: inactive (dead)

Ubuntu 20.04 can use either systemd-timesyncd or the full ntpd service, but they shouldn't run simultaneously. Here's how to properly configure each:

Option 1: Using systemd-timesyncd

sudo apt purge ntp
sudo systemctl unmask systemd-timesyncd
sudo systemctl enable --now systemd-timesyncd

Then edit the configuration:

sudo nano /etc/systemd/timesyncd.conf

Uncomment and modify these lines:

[Time]
NTP=0.us.pool.ntp.org 1.us.pool.ntp.org
FallbackNTP=ntp.ubuntu.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

Option 2: Using Full NTP Implementation

sudo apt install ntp
sudo systemctl stop systemd-timesyncd
sudo systemctl mask systemd-timesyncd
sudo systemctl enable --now ntp

Configure your NTP servers in:

sudo nano /etc/ntp.conf

Add server entries like:

server 0.us.pool.ntp.org iburst
server 1.us.pool.ntp.org iburst
server 2.us.pool.ntp.org iburst
server 3.us.pool.ntp.org iburst

After making changes, verify with:

sudo timedatectl
               Local time: Fri 2020-07-10 09:34:52 EDT  
           Universal time: Fri 2020-07-10 13:34:52 UTC  
System clock synchronized: yes                          
              NTP service: active

For NTP implementation specifically:

ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+45.79.1.160     142.66.101.13    2 u   64  256  377   36.988   -0.263   0.372
+50.116.42.201   17.253.34.125    2 u  130  256  377   20.329   -0.377   0.349
*216.229.0.179   199.102.46.73    2 u   33  256  377   15.858   -0.079   0.180

For virtual machines, you might want to add these kernel parameters to your host:

sudo nano /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="... clocksource=tsc tsc=reliable"

Then update grub:

sudo update-grub

If you're still having issues, force an immediate sync with:

sudo timedatectl set-ntp false
sudo timedatectl set-ntp true
sudo systemctl restart systemd-timesyncd

Or for NTP:

sudo systemctl stop ntp
sudo ntpd -gq
sudo systemctl start ntp

When dealing with time synchronization on Ubuntu 20.04, we're typically working with two competing services:

systemd-timesyncd (the default lightweight service)
ntp/ntpd (the traditional network time protocol daemon)

The fundamental issue arises when both services attempt to control time synchronization simultaneously. Here's how to check their status:

$ sudo systemctl status systemd-timesyncd
$ sudo systemctl status ntp

When seeing conflicting reports from timedatectl:

$ timedatectl
# Note these key indicators:
System clock synchronized: no
NTP service: n/a

Here's the complete solution workflow I've verified on multiple Ubuntu 20.04 KVM hosts:

1. Clean Up Existing Configurations

$ sudo apt purge ntp
$ sudo systemctl unmask systemd-timesyncd
$ sudo systemctl enable --now systemd-timesyncd

2. Configure timesyncd Properly

Edit /etc/systemd/timesyncd.conf:

[Time]
NTP=0.us.pool.ntp.org 1.us.pool.ntp.org
FallbackNTP=ntp.ubuntu.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

3. Verify Operation

$ sudo systemctl restart systemd-timesyncd
$ timedatectl show-timesync --all
$ journalctl -u systemd-timesyncd -f

When virtualization demands higher precision, install and configure chrony:

$ sudo apt install chrony
$ sudo systemctl disable systemd-timesyncd
$ sudo nano /etc/chrony/chrony.conf

Sample chrony.conf additions for KVM:

server 0.us.pool.ntp.org iburst
server 1.us.pool.ntp.org iburst
server 2.us.pool.ntp.org iburst
server 3.us.pool.ntp.org iburst
makestep 1.0 3
rtcsync

Essential diagnostic toolkit:

$ chronyc tracking
$ chronyc sources -v
$ chronyc sourcestats
$ timedatectl timesync-status
$ dmesg | grep -i time

Confirm proper synchronization with:

$ timedatectl
# Should show:
System clock synchronized: yes
NTP service: active