Vagrant Setting Timezone.

Maciej
2 min readFeb 22, 2022
Photo by Jon Tyson on Unsplash

Introduction

When we create virtual machines for local development, it sometimes happens that we need a different time zone than the one we currently have on the system. In this short post, I will show you how to easily do it in vagrant.

Vagrant Shell Provisioner

For the time being, you can set the time zone by adding one line inline and fixing it.

Vagrant.configure("2") do |config|
config.vm.box = "mmichal/centos7"
config.vm.box_version = "1.1.20210417"
config.vm.provision "shell", inline: "timedatectl set-timezone Europe/Warsaw"
end

It is necessary to have the Vagrantfile rewritten as it is.

From environment variables

In the shell environment, it is used as an environment variable for the time zone and this is TZ

[root@localhost vagrant]# date
Sun Feb 20 17:52:14 UTC 2022
[root@localhost vagrant]# TZ="America/New_York" date
Sun Feb 20 12:52:17 EST 2022
[root@localhost vagrant]#

Try use this value from Ruby, in the ternary operator, as if there were no TZ environment variables.

[root@localhost vagrant]# TZ="America/New_York" ruby -e 'p ENV["TZ"] ? ENV["TZ"] : "UTC"'
"America/New_York"
[root@localhost vagrant]# ruby -e 'p ENV["TZ"] ? ENV["TZ"] : "UTC"'
"UTC"
[root@localhost vagrant]#

Below the Vagrantfile created by combining these:

If you export the TZ environment variable permanently, it will be .bash_profile in the same time zone for the host OS and guest OS.

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.