如何设置流浪环境的日期和时间?

发布于 2025-02-12 08:58:56 字数 1123 浏览 0 评论 0原文

我试图在流浪环境(例如6月29日)设置日期和时间。

我使用以下vagrantfile来实现这一目标。

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"  

  config.vm.provider "virtualbox" do |vb|
  end

  config.vm.provision :shell, :inline => "sudo date -s '2022-06-29 12:34:56'", run: "always"

end

当我运行Vagrant Up时,我会看到

[...] 默认值:运行:内联脚本
默认值:6月29日星期三12:34:56 UTC 2022

但是当我与SSH连接并执行日期时,当前日期已打印。

当我尝试通过sudo日期手动更改时间时-s'2022-06-29 12:34:56'并执行我看到了更改的日期。
但是,如果我退出并重新进入,我再次看到当前日期和时间。

我如何在环境上永久更改日期和时间,或者至少在环境启动时将其设置?

注意:这个答案描述了如何在VirtualBox-Level上执行此操作:

VBoxManage setextradata "The VM name" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1

# Milliseconds to shift to in the past
VBoxManage modifyvm "The VM name" --biossystemtimeoffset -50000000

我测试了上述操作,显然可以计算出知道当前日期和日期的毫秒我们想去。但是同样,以上不是Vagrant的自动解决方案。

I am trying to set the date and time on a vagrant environment (e.g. to 29th of June).

I used the following Vagrantfile to achieve this.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"  

  config.vm.provider "virtualbox" do |vb|
  end

  config.vm.provision :shell, :inline => "sudo date -s '2022-06-29 12:34:56'", run: "always"

end

When I run vagrant up I see

[...]
default: Running: inline script
default: Wed Jun 29 12:34:56 UTC 2022

but then when I connect with ssh and execute date the current date is printed.

When I try to change the time manually via ssh with sudo date -s '2022-06-29 12:34:56' and execute date I see the changed date.
But If I exit and get back in, I again see the current date and time.

How can I permanently change the date and time on an environment, or at least set it when the environment starts?

NOTE: This answer describes how to do this on virtualbox-level:

VBoxManage setextradata "The VM name" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1

# Milliseconds to shift to in the past
VBoxManage modifyvm "The VM name" --biossystemtimeoffset -50000000

I tested the above and it works and obviously the milliseconds can be calculated knowing the current date and the date to which we want to go to. But again the above is not an automatic solution via Vagrant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不会写诗 2025-02-19 08:58:56

我设法使用触发器找到了部分虚拟盒特定的解决方案:

  config.vm.define "mymachine" do |mymachine|
    config.vm.box = "ubuntu/focal64"  
 
    mymachine.trigger.before :up do |trigger|
      trigger.info = "changing time"
      trigger.ruby do |env,machine|
        puts `VBoxManage setextradata #{machine.id} "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1`
        puts `VBoxManage modifyvm #{machine.id} --biossystemtimeoffset -150000000`
      end
    end
  end

我想剩下的就是根据当前VM的日期计算毫秒以转移到所需日期的毫秒。

I managed to find a partial virtualbox-specific solution using triggers:

  config.vm.define "mymachine" do |mymachine|
    config.vm.box = "ubuntu/focal64"  
 
    mymachine.trigger.before :up do |trigger|
      trigger.info = "changing time"
      trigger.ruby do |env,machine|
        puts `VBoxManage setextradata #{machine.id} "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1`
        puts `VBoxManage modifyvm #{machine.id} --biossystemtimeoffset -150000000`
      end
    end
  end

I guess what is left is to calculate the milliseconds for shifting to the desired date based on current vm's date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文