Non-interactive method for dpkg-reconfigure tzdata

UbuntuAutomationTimezone

Ubuntu Problem Overview


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?

Ubuntu Solutions


Solution 1 - Ubuntu

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:

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

     tzdata tzdata/Areas select Europe
     tzdata tzdata/Zones/Europe select Berlin
    
  • you set the above preseed file by running:

     debconf-set-selections /your/preseed.txt
    
  • you can now either install tzdata (if it is not installed yet) via apt or run dpkg-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:

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 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.

Solution 2 - Ubuntu

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 running dpkg-reconfigure -f noninteractive tzdata. The fix is as follows (from the above bug report):

$ 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 change the contents of /etc/timezone. This worked for me on Ubuntu 16.04.2 LTS.

Solution 3 - Ubuntu

Doing this in a 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 truncate -s0 /tmp/preseed.cfg; \
    echo "tzdata tzdata/Areas select Europe" >> /tmp/preseed.cfg; \
    echo "tzdata tzdata/Zones/Europe select Berlin" >> /tmp/preseed.cfg; \
    debconf-set-selections /tmp/preseed.cfg && \
    rm -f /etc/timezone /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 determined the removal of the files in /etc necesssary.

Solution 4 - Ubuntu

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:

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/*

Solution 5 - Ubuntu

Advancing josch's answer; 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 (14.04.5 LTS)

Solution 6 - Ubuntu

In Ubuntu 18.04 with systemd, I'm using:

  $ sudo timedatectl set-timezone 'Europe/Madrid'
  $ sudo dpkg-reconfigure --frontend noninteractive tzdata

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkennyView Question on Stackoverflow
Solution 1 - UbuntujoschView Answer on Stackoverflow
Solution 2 - UbuntutdennistonView Answer on Stackoverflow
Solution 3 - UbuntuNils BallmannView Answer on Stackoverflow
Solution 4 - Ubuntusergei_ivanovView Answer on Stackoverflow
Solution 5 - UbuntujahView Answer on Stackoverflow
Solution 6 - UbuntuggrandesView Answer on Stackoverflow