0

I am creating automated tests for an rsync-like utility and I need to be able to set the clock back 24 hours for one of the tests. However, when I use timedatectl to set the time and date they revert back to the current time after 15 seconds.

Checking timedatectl status I see that it is not running NTP:

# timedatectl status
      Local time: Wed 2019-05-15 23:24:17 EDT
  Universal time: Thu 2019-05-16 03:24:17 UTC
        RTC time: Wed 2019-01-16 02:13:40
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2019-03-10 01:59:59 EST
                  Sun 2019-03-10 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2019-11-03 01:59:59 EDT
                  Sun 2019-11-03 01:00:00 EST

I also checked and NTPD is not running and nothing is bound to port 123.

Does timedatectl have another method of syncing time? The CentOS 7 test machine is running in a Docker container under a VirtualBox image so Internet connectivity is somewhat limited. I am also testing on CentOS directly on VirtualBox so I will need to solve for the non-Docker case as well.

  • 1
    You can't set the time separately in a Docker container. It is always the time of the host. Use a fake time library for your application tests. – Michael Hampton May 16 '19 at 4:41
  • Thanks Michael. Oddly enough, I am seeing the same behavior with CentOS 7 running directly in VirtualBox and not in Docker so there must be something more to this. – Not a machine May 16 '19 at 13:11
  • @Notamachine did you install the VBox Guest Additions. This also includes a time sync with the host. virtualbox.org/manual/ch09.html#fine-tune-timers – eKKiM May 16 '19 at 14:48
  • @eKKiM Yes I did! I looked for 30 minutes in VBox proper but not in the Guest Additions. I will check that now. – Not a machine May 16 '19 at 16:53
  • @eKKiM That solved the problem. I disabled time sync in Guest Additions and that solved the problem both for the client and for the Docker image. If you want to submit that as an answer I will mark it as correct. – Not a machine May 16 '19 at 17:06
0

VBox Guest Additions includes and enables time syncronisation with the host by default.

You can disable this with the following command:

vboxmanage setextradata <vmname> "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "1"

Or add the following line to your yourVM.vbox file

<ExtraDataItem name="VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" value="1"/>
0

timedatectl is called by systemd after specific frequency it checks /etc/adjtime, /usr/share/zoneinfo/, /etc/localtime and many other files to check if any info is available, it having it's own logic to adjust the clock without using ntp.

static int context_write_data_timezone(Context *c) {
    _cleanup_free_ char *p = NULL;
    int r = 0;

    assert(c);

    if (isempty(c->zone)) {
            if (unlink("/etc/localtime") < 0 && errno != ENOENT)
                    r = -errno;

            return r;
    }

    p = strappend("../usr/share/zoneinfo/", c->zone);
    if (!p)
            return log_oom();

    r = symlink_atomic(p, "/etc/localtime");
    if (r < 0)
            return r;

    return 0;

}

  • Thanks! When I run "systemctl list-units --all | grep time" I do not see timedatectl. Is it embedded in another service or called by another name? – Not a machine May 16 '19 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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