Knowledgebase
Knowledgebase
Ubuntu Time Drift
Posted by Ilya Vasilenko on 10 January 2023 12:30 PM

Tested config for Ubuntu 12/14/16 running under Hyper-V with no time drift

 

1. Kernel boot options

Ensure that cmdline has no other time/clock related parameters, but only "clock=pit".

To check/modify:

- open the file /etc/default/grub

- find the line with text GRUB_CMDLINE_LINUX_DEFAULT

- ensure all is correct there, clocksource=pit set and NO other timer/clock related parameters existing

- save file if changed

- run "update-grub" to re-generate grub.cfg

 

2. Install NTPd service

- run "apt-get install ntp"

- stop the service by command "service ntp stop"

- backup the file /etc/ntp.conf and replace the content as shown below:

tinker panic 0
tos maxdist 16
driftfile /var/lib/ntp/ntp.drift
server 172.30.66.8 iburst prefer
server 0.au.pool.ntp.org iburst
server 1.au.pool.ntp.org iburst
server 0.oceania.pool.ntp.org iburst
restrict -4 default kod notrap nomodify
restrict -6 default kod notrap nomodify
restrict 127.0.0.1
restrict ::1

- start the service "service ntp start"

Set ntp to start automatically after boot

update-rc.d ntp enable

If it is the fresh Ubuntu install, jump to step 6.

If your Ubuntu setup has the time drift fix applied earlier from our different article, do additional steps as below.

3. Remove clock adjusting script

- open the file /etc/rc.local

- remove the string sh /etc/clocksync/clocksync.sh &

-  save the file

4. Remove other adjustments

- uninstall adjtimex by "apt-get purge adjtimex"

- if the file /etc/adjtime exists, edit it, remove all lines with digits and replace word UTC by LOCAL

- save the file

5. Remove old NTP drift file

- if existing, remove /var/lib/ntp/ntp.drift

6. Restart the VM

7. If still not syncing correctly

Set the ntp.conf file

# nano /etc/ntp.conf

It should read like this (if it does not please change it to the below accordingly):

tinker panic 0
tos maxdist 16
driftfile /var/lib/ntp/ntp.drift
server 172.30.66.8 iburst prefer minpoll 3
server 0.au.pool.ntp.org iburst
server 1.au.pool.ntp.org iburst
server 0.oceania.pool.ntp.org iburst
restrict -4 default kod notrap nomodify
restrict -6 default kod notrap nomodify
restrict 127.0.0.1
restrict ::1

CTRL-O to save

Sync the time

# ntpd -gq

Reinstall adjtimex

# apt-get install adjtimex

This stops NTP, forces it to sync the clock (to “prime the pump”), sleeps for 100 seconds, forces a second clock sync, and restarts NTP. It produces output like this:

# /etc/init.d/ntp stop ; ntpd -q ; sleep 100s ; ntpd -q; /etc/init.d/ntp start

Stopping NTP server: ntpd.
ntpd: time set +12.262938s
ntpd: time set +2.603381s <— drift per 100s
Starting NTP server: ntpd.

If the second time set is +2.60 then run this command (these don't have to be exact):

# adjtimex -t 10260

If the second time set is -2.60 then run this command (these don't have to be exact):

# adjtimex -t 9740

# /etc/init.d/ntp stop ; ntpd -q ; sleep 100s ; ntpd -q; /etc/init.d/ntp start

Stopping NTP server: ntpd.
ntpd: time set +3.044932s
ntpd: time set -2.659021s
Starting NTP server: ntpd

Most likely this will now report for example 2.6 in the other direction. This is normal. Now return the adjtimex to the default value:

# adjtimex -t 10000

Then reprime again:

# /etc/init.d/ntp stop ; ntpd -q ; sleep 100s ; ntpd -q; /etc/init.d/ntp start

You should now see an output like this:

* Stopping NTP server ntpd [ OK ]
ntpd: time set -2.653074s
ntpd: time slew -0.006406s
* Starting NTP server ntpd [ OK ]

Note that it now reports time slew (as it's only a very minor amount out). This is great.

You can now type:

# watch ntpq -p

This should report the current status of the NTP servers and you should see the jitter falling down to below 3 on the 172.31.66.5 server. Your time is now sync'd correctly.

 

(0 vote(s))
Helpful
Not helpful