Vagrant Setting Timezone.


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| = "mmichal/centos7"
config.vm.box_version = "1.1.20210417"
config.vm.provision "shell", inline: "timedatectl set-timezone Europe/Warsaw"

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"'
[root@localhost vagrant]# ruby -e 'p ENV["TZ"] ? ENV["TZ"] : "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.

 by the author.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


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