automate dpkg-reconfigure tzdata

Solution 1:

You need to specify the frontend as `noninteractive' and it will save your current settings.

dpkg-reconfigure will take the current system settings as gospel, so simply change your timezone the way you would normally and run it with the non-interactive flag

e.g. for me to change to "Europe/Dublin" where I am:

# echo "Europe/Dublin" > /etc/timezone    
# dpkg-reconfigure -f noninteractive tzdata

Obviously this allows you to use puppet/cfengine as you like to distribute /etc/timezone also.

EDIT:

after @gertvdijk comment pointing to https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806 and @scruss answer you will probably have to do it like this in most modern distributions:

$ sudo ln -fs /usr/share/zoneinfo/Europe/Dublin /etc/localtime
$ sudo dpkg-reconfigure -f noninteractive tzdata

Solution 2:

Since the accepted answer no longer works (Debian Jessie, April 2017), an approach modified from @gertvdijk's comment link appears to do the job now:

sudo ln -fs /usr/share/zoneinfo/Europe/Dublin /etc/localtime
sudo dpkg-reconfigure -f noninteractive tzdata

Solution 3:

You should be able to use debconf-set-selections to preset the configuration. Install debconf-utils and run debconf-get-selections | grep tzdata on a properly configured system to figure out what to set it too.


Solution 4:

You can also use the recipe from the (now defunct) Puppet wiki (archive) which replaces /etc/localtime with the appropriate zoneinfo file from /usr/share/zoneinfo:-

class timezone {
    package { "tzdata":
        ensure => installed
    }
}

class timezone::central inherits timezone {
    file { "/etc/localtime":
        require => Package["tzdata"],
        source => "file:///usr/share/zoneinfo/US/Central",
    }
}

class timezone::eastern inherits timezone {
    file { "/etc/localtime":
        require => Package["tzdata"],
        source => "file:///usr/share/zoneinfo/US/Eastern"
    }
}

class timezone::pacific inherits timezone {
    file { "/etc/localtime":
        require => Package["tzdata"],
        source => "file:///usr/share/zoneinfo/US/Pacific"
    }
}

class timezone::mountain inherits timezone {
    file { "/etc/localtime":
        require => Package["tzdata"],
        source =>
             "file:///usr/share/zoneinfo/US/Mountain"
    }
}

I'm not sure if dpkg-reconfigure does anything extra, but I have used the above recipe and it works perfectly.