Vagrant Tips: How to automatically adjust the Time Zone of your Vagrant Box

working copies

Intro

I was playing with my Linux vagrant boxes lately and I realized I was always spinning third-party boxes that had a totally different time zone (Europe/UK mostly). Up until now I never bothered as I have used a bunch of those from vagrant Cloud for years. I sometimes notice, and think of using a shell command to fix it during the bootstrap, but I haven’t had the time. However, I was heavily debugging some logs this week and the wrong Time Zone started to get annoying for me so I decided to look for a permanent fix. 

 

I. Vagrant Time zone plugin to the rescue 

Solution

There is no need to add a random shell script in the shell provisioning area of your Vagrantfile.Teemu Matilainen has you covered with his sweet ruby plugin called  vagrant-timezone that does just that.

How does it work

Install the plugin:

C:\Users\brokedba> vagrant plugin install vagrant-timezone
Installing the 'vagrant-timezone' plugin. This can take a few minutes...
Fetching vagrant-timezone-1.3.0.gem
Installed the plugin 'vagrant-timezone (1.3.0)'!

  Configuration

To Configure time zone for all Vagrant VMs, add the following to $HOME/.vagrant.d/Vagrantfile or to a project specific Vagrantfile (see below example in windows)

C:\Users\brokedba\.vagrant.d>vagrant init
--- Add the IF block below in the master or each build’s Vagrantfile
Vagrant.configure("2") do |config| 
   if Vagrant.has_plugin?("vagrant-timezone") 
      config.timezone.value = "America/Toronto" 
   end 
   # ... other stuff 
end

You can of course choose your own Time Zone from this TZ database list

 

II. Usage and Prerequisites

– The configuration is done on vagrant up and vagrant reload actions.
Note that no services are restarted automatically so they may keep using the old-time zone information.

– This plugin requires Vagrant 1.2 or newer.

 

III. Compatibility

  • Linux guests.
    • Arch
    • CoreOS
    • Debian (and derivatives)
    • Gentoo
    • RedHat (and derivatives)
  • BSD guests:

    • FreeBSD
    • NetBSD
    • OpenBSD
    • OS X
  • Windows

 

Conclusion:

This will hopefully encourage you to more often use and enjoy vagrant boxes without being stuck with the original Time Zone!

Share on:
Come work with the Best