69

I'm using puppet to admin a cluster of debian servers. I need to change the timezone of each machine on the cluster. The proper debian way to do this is to use dpkg-reconfigure tzdata. But I can only seem to change it if I use the dialog. Is there some way to automate this from the shell so I can just write an Exec to make this easy?

If not, I think the next best way would probably be to have puppet distribute /etc/timezone and /etc/localtime with the correct data across the cluster.

Any input appreciated!

1

7 Answers 7

99

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
3
  • 5
    as an alternative to changing permissions, you could do something like this. echo 'Europe/Dublin' | sudo tee /etc/timezone > /dev/null
    – NDBoost
    Jan 19, 2015 at 14:04
  • 3
    For completeness, another way to echo via sudo is sudo bash -c 'echo "Europe/Dublin" > /etc/timezone' Nov 9, 2015 at 22:26
  • 8
    Heads up! Newer Ubuntu/Debian versions don't work this way anymore. Behaviour changed. bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806
    – gertvdijk
    Sep 12, 2016 at 21:38
38

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
0
4

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.

1
  • 4
    For things that are debconf based, this should work. But tzdata prefers the config from /etc/timezone. So this does not work on an already installed (wheezy) system.
    – Elrond
    Jul 18, 2014 at 15:01
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.

0
sudo EDITOR='sed -Ei "
    s|tzdata/Areas=.+|tzdata/Areas=\"Asia\"|;
    s|tzdata/Zones/Asia=.+|tzdata/Zones/Asia=\"Shanghai\"|
    "' dpkg-reconfigure -f editor tzdata

Related answer: How do you set a locale non-interactively on Debian/Ubuntu?

0

dpkg-reconfigure -f noninteractive not very intuitive.

I want to point out another way.

A. debconf frontend: teletype

It's a hidden option (seems debconf builtin frontend).

echo -e 'Asia\nTokyo' | sudo dpkg-reconfigure tzdata -f teletype

It's easy to read and write.

🔵 debconf frontends

see all frontends (but no teletype listed)

dpkg-reconfigure debconf
or
dpkg-reconfigure debconf -f teletype

  1. Dialog
  2. Readline
  3. Gnome
  4. Kde
  5. Editor
  6. Noninteractive

🔵 check timezone

$ ll /etc/localtime; tail -v /etc/timezone
lrwxrwxrwx 1 root root 30 Apr 30 02:59 /etc/localtime -> /usr/share/zoneinfo/Asia/Tokyo
==> /etc/timezone <==
Asia/Tokyo

NOTE: sometimes maybe you need rm /etc/localtime first (if it is broken link).

B. manually edit file

maybe off-topic

ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
echo 'Asia/Tokyo' > /etc/timezone

OR

ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
apt install tzdata        # it will use the linked timezone

OR

ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
dpkg-reconfigure tzdata   # it will use the linked timezone



tested on ubuntu18 & 20

-5

Its very simple and only need to type a command and answer the simple questions.
then run:

/usr/bin/tzselect
1
  • 2
    man tzselect: "Note that tzselect will not actually change the timezone for you. Use 'dpkg-reconfigure tzdata' to achieve this."
    – spinkus
    Dec 21, 2014 at 9:12

You must log in to answer this question.