Non-interactive method for dpkg-reconfigure tzdata

When I set up the Ubuntu server first, I am sure that I have aptitude install tzdata , then dpkg-reconfigure tzdata to properly configure my timezone.

I am trying to automate the server configuration using a script, and noticed that this piece sorts the key in automatic mode, as this requires an interactive session with user intervention.

Is there a way to use dpkg-reconfigure without being interactive?

+66
timezone ubuntu automation
Dec 29 '11 at 17:46
source share
6 answers

UPDATE : This question is now off topic for StackOverflow.
Please see the correct answer on ServerFault .

+83
Dec 29 '11 at 18:02
source share

The answer from swill is not how it is done correctly. If you want to have a package configuration without using the / dpkg script, then you want to use the debconf wait mechanism.

In your case, this means that you must do the following:

  • set the following environment variables to avoid debconf trying to ask the user any questions:

     export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true 
  • then imagine debconf with the following preseed.txt file (or any other settings you want):

     tzdata tzdata/Areas select Europe tzdata tzdata/Zones/Europe select Berlin 
  • you installed the previous preseed file by doing:

     debconf-set-selections /your/preseed.txt 
  • Now you can either install tzdata (if it is not already installed) via apt , or run dpkg-reconfigure . In the end, tzdata will be configured according to what you specified in your debconf preseed file.

Remember that you can automate a lot more using debconf prefetching. For example, in my queries I always set:

 locales locales/locales_to_be_generated multiselect en_US.UTF-8 UTF-8 locales locales/default_environment_locale select en_US.UTF-8 

You can always check the debconf settings of your current system by running debconf-get-selections . The result should give you some idea of ​​what part of the system configuration you can automate with debconf preseeding.

+55
Dec 19 '13 at 23:24
source share

There is an error ( https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806 , not fixed at the time of writing this answer) in 04.16, which causes the contents of /etc/timezone to overwrite the old value when running dpkg-reconfigure -f noninteractive tzdata . The fix is ​​as follows (from the error report above):

 $ sudo ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime $ sudo dpkg-reconfigure --frontend noninteractive tzdata Current default time zone: 'America/New_York' Local time is now: Mon Feb 20 07:30:33 EST 2017. Universal Time is now: Mon Feb 20 12:30:33 UTC 2017. $ cat /etc/timezone America/New_York 

No need to manually modify the contents of /etc/timezone . This worked for me on Ubuntu 16.04.2 LTS.

+24
Feb 20 '17 at 12:37
source share

Doing this in the Dockerfile :

 FROM ubuntu:xenial ## for apt to be noninteractive ENV DEBIAN_FRONTEND noninteractive ENV DEBCONF_NONINTERACTIVE_SEEN true ## preesed tzdata, update package index, upgrade packages and install needed software RUN echo "tzdata tzdata/Areas select Europe" > /tmp/preseed.txt; \ echo "tzdata tzdata/Zones/Europe select Berlin" >> /tmp/preseed.txt; \ debconf-set-selections /tmp/preseed.txt && \ rm /etc/timezone && \ rm /etc/localtime && \ apt-get update && \ apt-get install -y tzdata ## cleanup of files from setup RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 

In my experiments, I decided to delete the files in /etc needed.

+17
Dec 20 '17 at 15:08
source share

Josch transition; set the debconf db values ​​and remove /etc/{localtime,timezone} before running dpkg-reconfigure : -

 $ echo "tzdata tzdata/Areas select Europe" > some/file.txt $ echo "tzdata tzdata/Zones/Europe select Berlin" >> some/file.txt $ sudo debconf-set-selections some/file.txt $ sudo rm /etc/timezone $ sudo rm /etc/localtime $ sudo dpkg-reconfigure -f noninteractive tzdata Current default time zone: 'Europe/Berlin' Local time is now: Thu Sep 1 17:13:16 CEST 2016. Universal Time is now: Thu Sep 1 15:13:16 UTC 2016. 

This method is known to work on: -

  • Ubunty Trusty (04.14.5 LTS)
+8
Sep 01 '16 at 15:17
source share

Here is my Dockerfile for the latest Ubuntu 18.04 LTS distribution adapted from @NilsBallmann's answer. I also removed the creation of a temporary file and compressed the installation of the package into one layer:

 FROM ubuntu:bionic RUN export DEBIAN_FRONTEND=noninteractive; \ export DEBCONF_NONINTERACTIVE_SEEN=true; \ echo 'tzdata tzdata/Areas select Etc' | debconf-set-selections; \ echo 'tzdata tzdata/Zones/Etc select UTC' | debconf-set-selections; \ apt-get update -qqy \ && apt-get install -qqy --no-install-recommends \ tzdata \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* 
+5
Aug 16 '18 at 16:37
source share



All Articles