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

问题描述 投票:0回答:9

如何修正时区

上次,我弄清楚了如何在 vagrant 服务器中调整系统时钟。然而,当我停止 vagrant 并再次启动它时,系统时钟总是晚 9 小时。我可以手动使用ntp命令调整,但我想知道如何自动调整系统时钟。

我已经尝试了以下方法,但仍然不起作用。有什么建议吗?

如何在 VirtualBox 中同步主机唤醒时间?

server vagrant virtualbox centos6
9个回答
45
投票

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

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

您需要将“/Europe/Paris”替换为您要设置的时区


18
投票

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

安装一次:

vagrant plugin install vagrant-timezone



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

Vagrantfile

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

您可以将“UTC”替换为此处列出的任何 tz 值。 例如:“亚洲/加尔各答”。



或者您可以在您的

Vagrantfile
中使用主机的时区和此条目:

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

10
投票

接受的答案不够稳健,因为它没有考虑到在时区之间旅行的人,并且要求最终用户修改

Vagrantfile
而不是仅仅执行
vagrant up

在 Scott P. 的答案的基础上,这里有一个更好、更灵活的解决方案,可以自动将虚拟机时区与主机的 tz 相匹配。他的代码片段的 Etc/GMT 时区选择存在拼写错误/错误,根据 POSIX GMT+7 将时钟设置为落后 7 小时(请参阅Wiki 解释),因此我们需要交换偏移量:

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

6
投票

我得到了:

[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

因此删除了“\”转义字符。


4
投票

自动检测时区的稍微改进的版本:

自动检测部分来自这里

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
投票

我的 Vagrant 来宾操作系统时间已晚 7 天不同步。上述方法对我来说不起作用,因为我的访客计算机中没有安装访客添加和 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 响应标头字段中的时间更新系统时间。

因此,根据您的互联网连接速度和延迟,更新时间可能会偏差几毫秒到几秒(通常< 100ms). But it shouldn't matter for most cases.

以下是

curl
版本,如果您出于任何原因不想使用
wget

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

2
投票

@Benny K@Scott P. 的解决方案给了我时区的负值,就像 @rubo77 的情况一样。值得注意的是,我的主机操作系统是 Windows。如果您的客人(如 Debian 9+)上存在

timedatectl
,这就是我用来更改时区设置的方法:

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

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"

0
投票

方法是自动设置时区,与主机使用相同 vagrant-timezone 插件。

安装 vagrant-timezone 插件

vagrant plugin install vagrant-timezone

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

config.timezone.value = :host

请注意,此功能仅在 OS X 主机和 Linux 客户机上进行了测试。

© www.soinside.com 2019 - 2024. All rights reserved.