How to correct system clock in vagrant automatically
Solution 1
The method I use and it should not be provider specific is to add the following in my Vagrantfile
config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime", run: "always"
you would need to replace '/Europe/Paris' with the timezone you want to set
Solution 2
The simplest way is to set the timezone automatically is to use the vagrant-timezone plugin.
Install it once with:
vagrant plugin install vagrant-timezone
After that, add the below to your Vagrantfile
:
if Vagrant.has_plugin?("vagrant-timezone")
config.timezone.value = "UTC"
end
You may replace "UTC" with any of the tz values listed here. For example: "Asia/Kolkata".
Or you can use your host's timezone with this entry in your Vagrantfile
:
if Vagrant.has_plugin?("vagrant-timezone")
config.timezone.value = :host
end
Solution 3
Accepted answer is not robust enough, as it does not account for people who travel between timezones, and requires end users to modify Vagrantfile
instead of just doing vagrant up
.
Building up on Scott P.'s answer, here's a better more flexible solution that matches VM timezone to host's tz automatically. There's a typo/mistake in his snippet's Etc/GMT time zone selection, as per POSIX GMT+7 sets clock 7 hours behind (see Wiki explanation), hence we need to swap offsets:
Vagrant.configure("2") do |config|
require 'time'
offset = ((Time.zone_offset(Time.now.zone) / 60) / 60)
timezone_suffix = offset >= 0 ? "-#{offset.to_s}" : "+#{offset.to_s}"
timezone = 'Etc/GMT' + timezone_suffix
config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/" + timezone + " /etc/localtime", run: "always"
end
Solution 4
A slightly improved version that auto-detects timezone:
The auto-detect portion came from here.
Vagrant.configure("2") do |config|
require 'time'
offset = ((Time.zone_offset(Time.now.zone) / 60) / 60)
timezone_suffix = offset >= 0 ? "+#{offset.to_s}" : "#{offset.to_s}"
timezone = 'Etc/GMT' + timezone_suffix
config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/" + timezone + " /etc/localtime", run: "always"
end
Solution 5
I got:
[vagrant@ansiblecontrol ~]$ date -s \"$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z\"
date: extra operand ‘2018’
Try 'date --help' for more information.
This works for me:
sudo date -s "$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z"
Sun Apr 1 16:36:59 CEST 2018
So removed the "\" escape character.
Related videos on Youtube
Sho
Updated on April 03, 2022Comments
-
Sho about 2 years
Last time, I figured out how to adjust a system clock in vagrant server. However, when I halt the vagrant and start it again, the system clock is always 9 hours late. I can adjust by using ntp command manually, but I'd like to know how to adjust the system clock automatically.
I have tried the below, but it still doesn't work. Are there any suggestions?
-
Sho over 8 yearsAfter matching the version of guest addtion, it perfectly works. (I can not see clearly if the version mismatch really matters to this problem ) I think I should learn ruby if I will keep using vagrant. Thanks soooo much!
-
Admin over 6 years@Sho: you can also use vagrant with shell provisioning scripts or writing them with puppet or chef. you are not forced to use ruby. i am quite happy with shell provisioning.
-
rubo77 almost 6 yearsThis did change the timezone, but in the wrong direction. (I live in Europe/Berlin Time)
-
polynomial_donut over 5 yearsApart from this, times were never properly synced in my Ubuntu based guest until I installed the
ntp
package (it was already installed on my host) to get NTP up and running - 'Duh' you might say, but I was assuming the guest additions were supposed to take care of this entirely. Of course, one should make sure guest additions are installed. -
cleary almost 4 yearsThanks for posting! This seems far and away the most vagrant-like method to do this, I'm surprised that I'm the first upvoter though - this is not a new plugin, going by the github commits, it first began development in 2016 (github.com/tmatilai/vagrant-timezone/commits/master)
-
cleary almost 4 yearsThere is a vagrant-timezone plugin, please see this post: stackoverflow.com/a/62085199/3164018
-
Frederic Henri almost 4 years@cleary - correct but it did not exist at the time of the question and it depends if you want to have a large set of plugins or in control of what's happening ... for a 1 line in Vagrantfile I don't prefer to install a plugin
-
samir mankar almost 4 years"vagrant plugin install vagrant-timezone" this solved my issue. Thanks.