如何自动纠正vagrant中的系统时钟

时间:2015-11-26 13:21:25

标签: server vagrant virtualbox centos6

How to Correct Timezone

上次,我想出了如何在vagrant服务器中调整系统时钟。但是,当我停止流浪汉再次启动时,系统时钟总是晚9个小时。我可以手动使用ntp命令进行调整,但我想知道如何自动调整系统时钟。

我已尝试过以下内容,但仍然无效。有什么建议吗?

How to sync time on host wake-up within VirtualBox?

8 个答案:

答案 0 :(得分:28)

我使用的方法不应该是特定于提供者的,而是在我的Vagrantfile中添加以下内容

  config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime", run: "always"

你需要更换' / Europe / Paris'使用您想要设置的时区

答案 1 :(得分:6)

接受的答案不够健全,因为它不考虑在时区之间旅行的人,并且要求最终用户修改Vagrantfile而不是仅仅vagrant up

建立在Scott P.的答案上,这是一个更灵活的解决方案,可以自动将VM时区与主机的tz相匹配。根据POSIX GMT + 7套时钟7小时落后(见Wiki explanation),他的片段的Etc / GMT时区选择中存在拼写错误/错误,因此我们需要交换偏移量:< / p>

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

答案 2 :(得分:3)

略微改进的版本,可自动检测时区:

自动检测部分来自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

答案 3 :(得分:3)

最简单的方法是自动设置时区,即使用vagrant-timezone插件。

通过以下方式安装vagrant-timezone插件

vagrant plugin install vagrant-timezone

然后,将以下内容添加到您的Vagrantfile

if Vagrant.has_plugin?("vagrant-timezone")
    config.timezone.value = "UTC"
end

您可以用here中列出的任何tz值替换“ UTC”。 例如:“亚洲/加尔各答”。

答案 4 :(得分:2)

我的流浪客户操作系统时间在7天内不同步。上面的方法对我来说不起作用,因为我的Guest机器中没有安装Guest添加和ntp。

我终于通过使用hack解决了这个问题 https://askubuntu.com/a/683136/119371

cfg.vm.provision "shell", inline: "date -s \"$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z\"", run: "always", privileged: true, upload_path: "/home/vagrant/tmp/vagrant-shell"

上述方法不会将来宾操作系统时间与您的主机或任何NTP服务器同步。它向google.com发送HTTP请求,并使用HTTP响应标头字段中的时间更新系统时间。

因此,根据您的互联网连接速度和延迟,更新的时间可能会减少几毫秒到几秒(通常<100毫秒)。但对大多数情况来说,这无关紧要。

以下是curl版本,如果您因任何原因不想使用wget

cfg.vm.provision "shell", inline: "date -s \"$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z\""

答案 5 :(得分:2)

我得到了:

[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.

这对我有用:

sudo date -s "$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z"
Sun Apr  1 16:36:59 CEST 2018

删除了“\”转义字符。

答案 6 :(得分:1)

根据@Benny K.的回答(https://stackoverflow.com/a/46778032/3194807),考虑夏令时:

require "time"
offset = ((Time.zone_offset(Time.now.zone) / 60) / 60) + (Time.now.dst? ? 1 : 0)
timezone_suffix = offset >= 0 ? "-#{offset.to_s}" : "+#{offset.to_s}"
timezone = 'Etc/GMT' + timezone_suffix

tzShellProvision = <<_SHELL_
    ln -fs /usr/share/zoneinfo/#{timezone} /etc/localtime
    dpkg-reconfigure -f noninteractive tzdata
_SHELL_

default.vm.provision :shell, inline: tzShellProvision, run: "always"

答案 7 :(得分:0)

@Benny K@Scott P.的解决方案为我提供了时区的负值,就像@rubo77的情况一样。值得注意的是,我的主机操作系统是Windows。如果您的来宾上有timedatectl(例如Debian 9+),这就是我用来更改时区设置的方法:

config.vm.provision "shell",
    inline: "timedatectl set-timezone Europe/Budapest",
    run: "always" 

这将返回预期的时区,而不是负值:

# before timedatectl
vagrant@master:~$ timedatectl
               Local time: Fri 2020-07-03 11:52:31 -02
           Universal time: Fri 2020-07-03 13:52:31 UTC
                 RTC time: Fri 2020-07-03 13:52:31
                Time zone: Etc/GMT+2 (-02, -0200)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

# after timedatectl
vagrant@master:~$ timedatectl
               Local time: Fri 2020-07-03 15:53:24 CEST
           Universal time: Fri 2020-07-03 13:53:24 UTC
                 RTC time: Fri 2020-07-03 13:53:24
                Time zone: Europe/Budapest (CEST, +0200)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no