57

I am running an Ubuntu 12.04-based box inside of Vagrant using VirtualBox. So far, everything is fine - except for one thing:

Let's assume that the VM is running. Then, the host goes to standby-mode. After waking it up again, the VM is still running, but its internal clock continues where it stopped when the host went down. So this basically means: Put the host to sleep for 15 minutes, wake it up again, then the VM's internal clock is 15 minutes late.

How can I fix this (setting the time manually is not an option for obvious reasons ;-))? Is there a way to run a script inside of a Vagrant VM whenever the host system changes its state?

I've read in the documentation that by default the VirtualBox Guest Additions sync the time with the host every 10 seconds. Apparently this is not happening, but I can not find any place where it is disabled. So any ideas?

PS: The Guest Additions are installed and match the version of VirtualBox being used.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • You may find the [answers here](https://superuser.com/questions/463106/virtualbox-how-to-sync-host-and-guest-time) useful. – Asclepius Feb 13 '15 at 17:36

6 Answers6

61

The documentation lacks some details here.

What VirtualBox does every 10 seconds is just slight adjustement (something like 0.005 seconds). Only when the time difference reaches a threshold (20 minutes by default) a "real" resync is done.

You can reduce the thresold (i.e. to 10 seconds) with the following command:

VBoxManage guestproperty set <vm-name> "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 10000
Emyl
  • 10,460
  • 2
  • 35
  • 34
  • 1
    Is it possible to specify this inside of the `Vagrantfile` somehow? – Golo Roden Oct 21 '13 at 11:08
  • 6
    Okay, I figured it out: `vb.customize [ "guestproperty", "set", :id, "--timesync-threshold", 10000 ]` – Golo Roden Oct 21 '13 at 11:45
  • 30
    @GoloRoden Thanks for the `vb.customize`, but the parameters are wrong. The correct command is `vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000 ]` – jbasko Dec 13 '13 at 18:00
  • 12
    Might be worth mentioning that vb.customize should be within a block such as: `config.vm.provider "virtualbox" do |vb|` – Slobodan Kovacevic Apr 25 '14 at 11:02
  • I have no idea why, but for me the time is not re-syncing. It is out by several hours, guest is MS-Windows7, host is debian. Virtualbox guest additions service is running in the guest. – ctrl-alt-delor Apr 30 '16 at 20:04
  • This fixed if for me (it turns the timesync on. It was, for some reason, off) `vboxmanage setextradata «machine-name» "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 0` – ctrl-alt-delor Apr 30 '16 at 20:22
  • Here's a cross reference to superuser question that lists additional properties with regard to timesync, https://superuser.com/questions/463106/virtualbox-how-to-sync-host-and-guest-time/765014#765014. – Martin Woolstenhulme Aug 24 '17 at 21:24
28

Summarizing answers of @zilupe and @Slobodan Kovacevic, solution is to add following to Vagrantfile:

config.vm.provider 'virtualbox' do |vb|
   vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 1000 ]
end

This will synchronize clocks each time when desync becomes > 1s (1000ms)

Daniel Garmoshka
  • 5,849
  • 39
  • 40
  • In the case of Linux, I also suggest disabling the `ntp service` to prevent constant need to sync. RHEL `sudo chkconfig ntpd off` and `sudo service ntpd stop` Ubuntu: `sudo update-rc.d -f ntp remove` and `sudo /etc/init.d/ntp stop` – Will B. Jan 08 '18 at 01:39
6

I give an other solution to sync time between guest & host without installing Virtualbox guest addition:

  1. install ntp on your guest, and de-comment these lines in /etc/ntp.conf:

    disable auth
    broadcastclient
    

Then, restart ntp with service ntp restart

  1. Active broadcast on your host:

    • For Linux users, edit your /etc/ntp.conf file and configure broadcast (you must adapt IP):

      broadcast 192.168.123.255

    • For Windows users, activate the "Windows Time" service. You can then read this page to configure it to broadcast time

    Then, restart time service on host.

kenorb
  • 155,785
  • 88
  • 678
  • 743
fred727
  • 2,644
  • 1
  • 20
  • 16
4

For me to get timesync working I had to do this:

vboxmanage setextradata «machine-name» "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 0

It turns the timesync on. It was, for some reason, off.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
  • To check this value before writing code to set it, you can use `vboxmanage getextradata "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled"` - VM names can be seen in VirtualBox GUI or with `vboxmanage list vms`. I found this value wasn't set by default with VirtualBox 5.1.14 and Vagrant 2.2.3 – RichVel Mar 03 '19 at 08:44
3

I found a solution:

  1. install ntpdate
  2. add "s" permission for ntpdate, this allows non-root users to run ntpdate as root: sudo chmod u+s /usr/sbin/ntpdate
  3. add one line in ~/.bashrc: ntpdate -u ntp.ubuntu.com

After that, each time you login to the linux system, the time will be sync once.

mengqi
  • 173
  • 2
  • 5
1

you can install the VirtualBox Guest Additions in the VM to sync the time automatically by VB.

che---
  • 59
  • 2