LoginSignup
5

More than 5 years have passed since last update.

apt-get installでDEBIAN_FRONTEND=noninteractiveが効かない

Last updated at Posted at 2016-07-16

問題

たとえば以下のようなスクリプトがあったとします。

export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get install -y -qq keyboard-configuration

TravisやDockerなど画面のないPCではapt-getを使ったアプリのインストール中に起こるインタラクティブな操作をしたくないので、DEBIAN_FRONTEND=noninteractiveという環境変数を入れることで無効にしていました。

ところが、このコードはubuntuのTrusty (14.04)までは正常に動きますが、ふとXenial (16.04)で試したところ、途中でインタラクティブに操作を求められ、Travisなどキー入力不可の環境で先に進まなくなってしまいます。

原因

sudoでroot権限へ昇格するときに環境変数が引き継がれていないのが原因。

Xenialでman sudoをすると、

-E The -E (preserve environment) option will override the env_reset option in sudoers(5)). It is only available when either the matching command has the SETENV tag or the setenv option is set in sudoers(5).

とあります。
このオプションはTrusty以前からもあり、これを指定せずともなんとなく環境変数は引き継がれていた(PATHとかは従来でも引き継がれない)はずですが、どうやらXenialからはsudo -Eを明示的に指定しないと環境変数が引き継がれなくなったようです。

解決

export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get update
sudo -E apt-get install -y -qq keyboard-configuration

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5