How to correctly sync time in linux running as virtual guest after host resumes from hibernation?

24,786

Solution 1

In brief: Install (the ntp package and) the VirtualBox Guest Additions :)

  • Host: Windows 6.1 SP1, x64, 3GB
  • VirtualBox: v4.3.6
  • Guest: Debian 7.3, 32-bits, 768 KB

From VirtualBox, attach the file Oracle\VirtualBox\VBoxGuestAdditions.iso to the guest system (Settings/Storage) and as root

cd /media/cdrom0
sh ./VBoxLinuxAdditions.run

The VirtualBox Help at "9.4. Advanced configuration for Linux and Solaris guests" describes the steps but I had to install the package linux-headers-3.2.0-4-486, first.
Then, as root,

/usr/lib/VBoxGuestAdditions/vboxadd setup
/usr/lib/VBoxGuestAdditions/vboxadd-service setup
/usr/lib/VBoxGuestAdditions/vboxadd setup

Afterwards, reboot the guest.

I've checked by quitting the guest system (Alt+F4, then order VirtualBox to save the state of the guest), waiting 5 minutes, then restarting the guest system
At first, the clock was restored from its previous value, but after a few seconds all went fine (clock synced)

Solution 2

NTP server was not designed to run inside of a virtual machine. It requires a high resolution system clock, with response times to clock interrupts that are serviced with a high level of accuracy. NTP client is ok to run in some virtualization solutions. Run NTP on the base OS of the machine, and then have your various guest OSes take advantage of the good clock that is created on the system. Even that may not be enough, as there may be additional tools or kernel options that you need to enable so that virtual machine clients can adequately synchronize their virtual clocks to the physical system clock.

Source: Support NTP Known Issues

From that statement, and reading that your important part is git commit times, then it would be trivial to write a pre-commit hook to gather the time from your host machine, and you can get that simply by asking your host machine:

ssh hostmachine "date +%Y%m%d%H%M%s"

Share:
24,786

Related videos on Youtube

SiliconMind
Author by

SiliconMind

Updated on September 18, 2022

Comments

  • SiliconMind
    SiliconMind almost 2 years

    I use multiple virtual boxes for development and correct time and date is important for me when I push git commits. I do not shut down my host - it runs for months without restarting. I only hibernate it for nights and weekends. This introduces weird issue to my guest operating systems (all debians from turnkeylinux.org) - after some time, the date of guest operating systems is off by few days.

    Although all guests use NTP it does not work if it detects that date is off by some large values (few hours seems to be enough). Is there any way to force NTP sync even for large time differences? Or any other solution that would restore correct date and time on guest boxes after host resumes from hibernation?

    • Pierz
      Pierz almost 8 years
      See this answer for details - basically the guest additions provide for direct clock sync without the need for NTP: superuser.com/questions/463106/…
  • SiliconMind
    SiliconMind over 10 years
    Thanks but that's not quite an answer :) I'm using VirtualBox and there are no options for time sync. So if not NTP then what?
  • vgoff
    vgoff over 10 years
    Sorry, I took the leap... but now with an edit clarified my thought.
  • SiliconMind
    SiliconMind over 10 years
    As a matter of fact, that's what I ended up doing :) Just didn't had time to post the answer, so the credit goes to you.
  • Pierz
    Pierz almost 8 years
    NTP is not required for guest additions clock sync - it is performed by the guest additions daemon (VBoxService).
  • RDP
    RDP over 7 years
    Why do you suggest to repeat the first command twice? Is that really necessary?
  • Pierre
    Pierre over 7 years
    @RDP <quote>Is it really necessary to repeat the first command twice?</quote> I don't remember, and documentation has changed see [virtualbox.org/manual/ch09.html#idm7495]
  • Harry Tsai
    Harry Tsai over 7 years
    You have to install gcc and make, too. Not having them manifests as an error starting the vboxadd service. But you don't get the helpful suggestion to install those packages unless you run vboxadd explicitly -- VBoxLinuxAdditions.run doesn't expose it.