dpkg-reconfigure tzdata的非交互方法

时间:2011-12-29 17:46:36

标签: ubuntu automation timezone

首次设置Ubuntu服务器时,请确保aptitude install tzdata,然后dpkg-reconfigure tzdata,以便我正确设置时区。

我正在尝试使用脚本自动化我的服务器设置,并注意到这一部分会引发自动化,因为它需要一个用户干预的交互式会话。

有没有办法在没有交互的情况下使用dpkg-reconfigure?

7 个答案:

答案 0 :(得分:83)

更新:此问题现在已成为StackOverflow的主题 请参阅the correct answer on ServerFault

答案 1 :(得分:55)

sw水的答案并不是如何正确完成的。如果您想要无人参与/脚本化的dpkg配置包,那么您需要使用debconf预置机制。

在您的情况下,这意味着您必须执行以下操作:

  • 设置以下环境变量以避免debconf尝试向用户询问任何问题:

    export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
    
  • 然后使用以下preseed.txt文件(或您想要的任何其他设置)预置debconf:

    tzdata tzdata/Areas select Europe
    tzdata tzdata/Zones/Europe select Berlin
    
  • 通过运行以下方式设置上述预置文件:

    debconf-set-selections /your/preseed.txt
    
  • 您现在可以通过apt安装tzdata(如果尚未安装)或运行dpkg-reconfigure。最后,将根据您在debconf预置文件中指定的内容设置tzdata。

请记住,使用debconf preseeding可以自动执行更多操作。例如,在我的预习中,我总是设置:

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

您始终可以通过运行debconf-get-selections来检查当前系统的debconf设置。输出应该让您了解使用debconf preseeding可以自动执行多少系统配置。

答案 2 :(得分:24)

16.04中存在一个错误(https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806,在编写此答案时未修复),导致/etc/timezone的内容在运行dpkg-reconfigure -f noninteractive tzdata时被旧值覆盖。修复如下(来自上面的错误报告):

$ 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

无需手动更改/etc/timezone的内容。这在Ubuntu 16.04.2 LTS上对我有用。

答案 3 :(得分:17)

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

在我的实验中,我决定删除/etc必需的文件。

答案 4 :(得分:9)

推进josch的回答;在运行/etc/{localtime,timezone}之前设置debconf db值并删除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.

已知此方法适用于: -

  • Ubunty Trusty(14.04.5 LTS)

答案 5 :(得分:5)

这是我的Dockerfile,用于最新的Ubuntu 18.04 LTS发行版,改编自@NilsBallmann的回答。我还删除了临时文件的创建,并将软件包安装压缩到一个单独的层:

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

答案 6 :(得分:0)

在具有systemd的Ubuntu 18.04中,我正在使用:

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