Homestead: Machine clock loses time synchronisation when computer goes to sleep

Created on 19 Jan 2018  路  26Comments  路  Source: laravel/homestead

Versions

  • Vagrant: Vagrant 2.0.1
  • Provider: Virtualbox
  • Homestead: 7.0.1

Host operating system

macOS 10.13.2

Homestead.yaml

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Code
      to: /home/vagrant/Code
      type: nfs

sites:
    - map: default
      to: /home/vagrant/Code/dovi-website/public
    - map: dovi-website.test
      to: /home/vagrant/Code/dovi-website/public

databases:
    - homestead
    - dovi

ports:
    - send: 8000
      to: 80

Vagrant destroy & up output

Can't really restore the machine now (sorry about that), but the bug has been happening for a couple of versions and my last provisions didn't throw anything weird. If it is really necessary I'll be able to provide it, but I don't think it is absolutely necessary in the case of this issue.

Expected behavior

Time should stay synced.

Actual behavior

Now what happens is when my mac goes to sleep, the virtual machine's clock gets unsynchronized. I get errors when trying to run queues, use pusher or upload to Amazon S3.

Steps to reproduce

  1. Run homestead up
  2. Put mac to sleep
  3. Wake mac and try to run queues or upload files to S3.

References

There are issues on Laracasts detailing the same issue. As per the answers, it seems to have to do with the timesync-set-threshold in the vagrant file.
https://laracasts.com/discuss/channels/general-discussion/homesteadvagrant-time-drift-help
https://laracasts.com/discuss/channels/general-discussion/homestead-wrong-time?page=1

Most helpful comment

At last this worked for me:

  vagrant ssh
  sudo apt install ntpdate
  sudo ntpdate ntp.ubuntu.com
  sudo timedatectl set-ntp on
  sudo service ntp stop
  sudo ntpd -gq
  sudo service ntp start

  cd /path/to/homestead
  vagrant plugin install vagrant-vbguest
  vagrant halt
  vagrant up

All 26 comments

I added a cronjob thats syncs the clock every 5 minutes...

added a cron.sh file in the Homestead folder...

echo "*/5 * * * * vagrant sudo service ntp stop;sudo ntpd -gq;sudo service ntp start >> /dev/null 2>&1" > "/etc/cron.d/timefix"

Glad to know I am not the only one who has this issue. Now, I am no expert in Vagrant, but can we fix this in vagrant file?

You can't fix this in any configuration option. What is happening is your computer is going to sleep and Virtualbox will pause the VMs (Stopping time for them), when you resume Virtualbox will un pause the machines (Starting time where they left off)

The only known fix I'm aware of is to vagrant halt all your VMs before letting your computer sleep.

Okay thanks for the answer. Just to make sure, using some of the code provided as answers in my reference links aren't available options? Something like:

vb.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000]

timesync-set-threshold AFAIK won't fix the issue. It would be dependent on how long your machine sleeps for.

Alright, thanks for your answers. I'll come back if I find a solution at some point.

I have tried

config.vm.provider :virtualbox do |v|
v.customize ['guestproperty', 'set', :id, '/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold', 10000] 

But did not work.

At last this worked for me:

  vagrant ssh
  sudo apt install ntpdate
  sudo ntpdate ntp.ubuntu.com
  sudo timedatectl set-ntp on
  sudo service ntp stop
  sudo ntpd -gq
  sudo service ntp start

  cd /path/to/homestead
  vagrant plugin install vagrant-vbguest
  vagrant halt
  vagrant up

@cosecantt getting error with newest homestead 5.1.0
```vagrant@homestead:~/app/project$ sudo apt install ntpdate
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
ntpdate
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 48.7 kB of archives.
After this operation, 173 kB of additional disk space will be used.
Err:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 ntpdate amd64 1:4.2.8p4+dfsg-3ubuntu5.7
404 Not Found [IP: 91.189.88.149 80]
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/n/ntp/ntpdate_4.2.8p4+dfsg-3ubuntu5.7_amd64.deb 404 Not Found [IP: 91.189.88.149 80]

E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?```

@envision - once you've ssh'd into homestead, try running sudo apt-get update as the error suggests. Then you should be able to run the sudo apt install ntpdate command and all the others just fine.

@cosecantt - huge thanks for posting this solution - this issue has been bugging me for months! Only implemented it yesterday but I don't seem to have hit the issue yet after leaving my mac on sleep last night. Cheers!

Just added ntpdate to settler. The next base box update will have it installed automatically.

https://github.com/laravel/settler/pull/148

Also had issues with this, noticed it when git commits were a few days older than they should be!

I just try to remember to run 'sudo service ntp restart' whenever use the VM.

I am just getting into this issue, but @svpernova09 I don't think your solution is the direction we want to go. from the documentation here https://help.ubuntu.com/lts/serverguide/NTP.html#timedate-info

ntpdate is considered deprecated in favour of timedatectl and thereby no more installed by default.

from what I'm reading, I think we'll actually want to revert that commit, and then also remove 'ntp' from our list of installed software.

Especially if our next base box is going to be Ubuntu 18.04 I don't think we want to use the deprecated stuff.

@browner12 Yeah, the fix I implemented was the quickest and wasn't intended to be a long term fix since 18.04 is on the horizon and as of yesterday is released. All Homestead efforts should now be focusing on upgrading to 18.04 for the base box.

sounds good. thanks

Neither ntpdate nor timedatectl works for me.
When I send my Mac to sleep on Friday and come back to work on Monday, the vagrant/homestead VM shows me date and time from Friday when I left my work.
Either ntpdate and timedatectl then cannot connect to ntp.ubuntu.com to set date and time correctly due to invalid client datetime:
ntpq: read: Connection refused
Which is rather stupid.
After restarting the VM however, time sync works again.

I'd prefer and recommend a solution through the VirtualBox provider which has access to the host time.

@raphaelbeckmann here's a custom function I use that's been working for me, I think someone in this thread came up with it.

function datesync() {
    sudo service ntp stop && sudo ntpd -gq && sudo service ntp start
}

it's probably just a bandaid until we can find a permanent solution.

This problem still frustrates me every single day, even though I've had my ~/.bashrc set up to run a datesync function similar to @browner12 's here (which automatically runs whenever I SSH into my Homestead box but is only a temporary fix). I don't want to need to SSH into Homestead every time there is time slippage (which is multiple times per day).

These 3 posts look promising, but I'm not experienced enough with Vagrant or VirtualBox to understand.

If any of you figure out how to keep Homestead in sync with its Windows host's clock, I'd love to hear how. That will be awesome. :-)

@browner12 This fixed it for me, but seems like a temporary solution

I just ran the following two commands that toggle the ntp status of timedatectl, and they seemed to work for me

> timedatectl set-ntp false
> timedatectl set-ntp true

I gave it about a second after running those two commands, and date was back in sync. Seems like it "kicked it" enough for me.

However you want to execute those (cron job, bash script, alias, etc.) should work.

I could not get this to work at all. My machine was showing it was 5 days out of sync.

However this did work first time: sudo ntpdate ie.pool.ntp.org

Obviously you can change the ntp server to whatever you need it to be. I've set mine up as an alias.

@jonnywilliamson sudo: ntpdate: command not found

@bigdawggi Did not change the date for me.

@browner12 Thanks, still the only functioning workaround for me.

I logged into my database via phpmyAdmin, and server time synced correctly. Try it

Was this page helpful?
0 / 5 - 0 ratings