When I first setup an Ubuntu server, I make sure I aptitude install tzdata
, then dpkg-reconfigure tzdata
so that I set my timezone properly.
I am trying to automate my server setup with a script, and noticed this piece sort of throws a wrench into it being automatic, as it requires an interactive session with user intervention.
Is there a way to use dpkg-reconfigure without it being interactive?
The answer by swill is not how it is done properly. If you want unattended/scripted dpkg configuration of packages, then you want to use the debconf preseeding mechanism.
In your case this means that you have to do the following:
set the following environment variables to avoid that debconf tries to ask the user any questions:
then preseed debconf with the following preseed.txt file (or whatever other settings you desire):
you set the above preseed file by running:
you can now either install tzdata (if it is not installed yet) via
apt
or rundpkg-reconfigure
. In the end, tzdata will be set up according to what you specified in your debconf preseed file.Remember that you can automate lots more using debconf preseeding. For example in my preseeds I always set:
You can always inspect the debconf settings of your current system by running
debconf-get-selections
. The output should give you some idea of how much of the system configuration you are able to automate using debconf preseeding.There is a bug (https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806, not fixed at the time of writing this answer) in 16.04 which causes the contents of
/etc/timezone
to be overwritten with the old value when runningdpkg-reconfigure -f noninteractive tzdata
. The fix is as follows (from the above bug report):No need to manually change the contents of
/etc/timezone
. This worked for me on Ubuntu 16.04.2 LTS.Advancing josch's answer; set the debconf db values and remove
/etc/{localtime,timezone}
before runningdpkg-reconfigure
:-This method is known to work on:-
Doing this in a
Dockerfile
:In my experiments I determined the removal of the files in
/etc
necesssary.Here's my
Dockerfile
for the latest Ubuntu 18.04 LTS distro, adapted from the answer by @NilsBallmann. I also removed temp file creation and compacted the package installation into a single layer:I found the following details for you.
https://serverfault.com/a/84528
Edit: (copy and paste from the above link...)
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:
Obviously this allows you to use puppet/cfengine as you like to distribute /etc/timezone also.