2

I am writing a script that synchronizes the system clock with the hardware clock (aka RTC, or CMOS clock) assuming that no Network Time Protocol (NTP) daemon is running.

I am running in a virtual machine and the supervisor doesn't let the hardware clock to be changed by the guest.

So in linux, I can use the hwclock --hctosys to do this, in Debian for example:

$ date
Thu May 18 00:48:57 UTC 2017
$ date 0401121621
Thu Apr  1 12:16:00 UTC 2021
$ hwclock --hctosys # this set my system clock to the hardware clock back again
$ date
Thu May 18 00:49:28 UTC 2017

But how can I do the same thing on FreeBSD as it doesn't provide the hwclock utility? I tried using adjkerntz -a but it didn't work. I am running FreeBSD version 11.0-RELEASE-p9

Thanks for your help

1

Check sysctl machdep.disable_rtc_set. It should be 0, and hardware clock is set automatically every time settimeofday() is called. Then, if the ntpd is running, you don't have to set hardware clock with a separate command as ntpd already invokes this system call.

  • Thanks for your reply but I want to set the clock the other way around, I don't want to change the hardware clock, I want to change the system clock – Lilás May 18 '17 at 12:59
  • Why? Your hardware clock is more reliable than NTP? Is your FreeBSD running on an atomic clock, keeping the whole Internet on correct time? :) – Esa Jokinen May 18 '17 at 13:04
  • 1
    yes, I am running on top of an hypervisor, and I can't grant that the clients will run ntp, but the clock in the hypervisor (which I control) is reliable – Lilás May 18 '17 at 18:30
1

See answer 1 (below?) for general advice.

In order to make sure that ntpd will set the clock it should be run with the -g switch. Or if you do it by hand "ntpd -g -n". The last switch is to make sure that it does not fork. The first sets the time regardless. Watch the messages and exit using ^C. If it does not work, try using it with two -g switches.

Turn on ntp by enabling it in /etc/rc.conf There should be something like the following if ntpd is enabled. ntpd_enable="YES" ntpd_flags="-p /var/run/ntpd.pid -f /var/db/ntpd.drift -g -g"

Start the service with the command service ntpd start

openntpd sets the time when it starts so it seems to not need the -g option.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.