Ubuntu – changing timezone with dpkg-reconfigure tzdata and debconf-set-selections

debiandpkgtime zoneUbuntu

I want to set up a script that automatically changes the timezone on a machine (running ubuntu 11.10) and also sets the right values to the debconf database. I've tried the following, but it does not work (at the end, the current timezone remains unchanged, and if I run manually the dpkg-reconfigure tzdata command, the selected values are indeed the old ones):

#!/bin/sh -e
echo "tzdata    tzdata/Areas    select  Europe" | debconf-set-selections
echo "tzdata    tzdata/Zones/Europe select  Madrid" | debconf-set-selections
echo "tzdata    tzdata/Zones/America    select  " | debconf-set-selections
dpkg-reconfigure -f noninteractive tzdata

So, by now, I'm doing it messing with the files /etc/localtime and /etc/timezone directly, but I'd rather prefer the dpkg-reconfigure and debconf way.

Best Answer

Simplest way that I know of is:

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