FreeBSD: how to syncronize the system clock with the hardware clock in bash?

5,154

Solution 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.

Solution 2

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.

Share:
5,154

Related videos on Youtube

Lilás
Author by

Lilás

Updated on September 18, 2022

Comments

  • Lilás
    Lilás almost 2 years

    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

  • Lilás
    Lilás about 7 years
    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
  • Esa Jokinen
    Esa Jokinen about 7 years
    Why? Your hardware clock is more reliable than NTP? Is your FreeBSD running on an atomic clock, keeping the whole Internet on correct time? :)