59

My potential hosting provider suggests to run a command in the terminal in order to make KVM based server's OS image minimal. Since their KVM templates come with packages I will not need, I thought I might use that same command to remove unwanted packages.

That command starts with DEBIAN_FRONTEND=noninteractive, then invoke apt-get remove as follows:

DEBIAN_FRONTEND=noninteractive apt-get remove --purge -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" package-1 package-2 ... package-n; apt-get -y autoremove; apt-get clean all 

It is the first time I encounter the DEBIAN_FRONTEND environment variable, and I could not find useful information so far. So I wonder what setting it to noninteractive does, and if it is advisable, because I suppose that value (noninteractive) would persist.

Asarluhi
  • 1,607
  • 9
    Without knowing what it does, I can tell that when you set a variable that way as a part of a longer command string, it's only effective when running that command. It does not persist. – Gunnar Hjalmarsson Nov 03 '17 at 12:32
  • Related: https://serverfault.com/questions/227190/how-do-i-ask-apt-get-to-skip-any-interactive-post-install-configuration-steps – Jesse Nickles Feb 22 '23 at 11:13

2 Answers2

67

Simply prepending an apt command with DEBIAN_FRONTEND=something does not persist after the single command to which it is applied.

The DEBIAN_FRONTEND options are documented in the Section 7 manual pages of debconf (you may need to install the debconf-doc package in order to make these available on your system). From man 7 debconf:

Frontends
   One of debconf's unique features is that the interface it  presents  to
   you is only one of many, that can be swapped in at will. There are many
   debconf frontends available:

   dialog The default frontend, this uses  the  whiptail(1)  or  dialog(1)
          programs to display questions to you. It works in text mode.

   readline
          The  most  traditional frontend, this looks quite similar to how
          Debian configuration always has been:  a  series  of  questions,
          printed  out  at  the console using plain text, and prompts done
          using the readline library. It even supports tab completion. The
          libterm-readline-gnu-perl package is strongly recommended if you
          chose to use this frontend; the default readline module does not
          support  prompting  with default values.  At the minimum, you'll
          need the perl-modules package installed to use this frontend.

          This frontend has some special hotkeys. Pageup (or ctrl-u)  will
          go  back  to  the previous question (if that is supported by the
          package that is using debconf), and pagedown  (or  ctrl-v)  will
          skip forward to the next question.

          This is the best frontend for remote admin work over a slow con‐
          nection, or for those who are comfortable with unix.

   noninteractive
          This is the anti-frontend. It never interacts with you  at  all,
          and  makes  the  default  answers  be used for all questions. It
          might mail error messages to root, but that's it;  otherwise  it
          is  completely  silent  and  unobtrusive, a perfect frontend for
          automatic installs. If you are using this front-end, and require
          non-default  answers  to questions, you will need to preseed the
          debconf database; see the section below  on  Unattended  Package
          Installation for more details.

It also notes that:

   You can change the default frontend debconf uses by reconfiguring  deb‐
   conf.  On the other hand, if you just want to change the frontend for a
   minute, you can set the DEBIAN_FRONTEND  environment  variable  to  the
   name of the frontend to use. For example:

     DEBIAN_FRONTEND=readline apt-get install slrn

   The  dpkg-reconfigure(8) and dpkg-preconfigure(8) commands also let you
   pass --frontend= to them, followed by the frontend  you  want  them  to
   use.

   Note  that not all frontends will work in all circumstances. If a fron‐
   tend fails to start up for some reason, debconf will print out  a  mes‐
   sage explaining why, and fall back to the next-most similar frontend.
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • 15
    I see DEBIAN_FRONTEND=noninteractive commonly used in Dockerfiles. This answer makes me wonder why we simply don't use the --yes flag with apt commands as it'll be more explicit and in most cases it'll meet our needs. – Dennis Dec 04 '18 at 18:28
  • 1
    @Dennis I think it's because readline is not necessarily installed, and apt can output a warning as such. – hayd Jan 06 '19 at 01:32
  • 28
    Note: if using DEBIAN_FRONTEND with sudo, set the variable within the sudo command, and not for it. That is: sudo DEBIAN_FRONTEND=noninteractive apt-get install slrn. If you put the env variable setting before sudo, it will be valid for the sudo command itself, and sudo will not copy it to the apt-get command being run. Using it between sudo and the actual command is sudo syntax for setting up env variables for the command to be run (see man page). This gave me a hard time, so I thought like sharing. – caxcaxcoatl Jun 17 '19 at 03:28
  • 8
    If you want to change the frontend default behavior to noninteractive, you can do that with the following command: dpkg-reconfigure debconf --frontend=noninteractive – Carter Pape Nov 17 '19 at 00:40
  • 2
    @Dennis two remarks. As I understand it apt doesn't have a stable command line interface and therefore isn't recommended (for a Dockerfile/Containerfile), instead apt-get is. And the DEBIAN_FRONTEND variable also affects some commands called under the hood, not just apt/apt-get. If you ever get warnings about a non-functional terminal, this is how you can suppress them. On the other hand --yes won't change a thing for these cases. – 0xC0000022L Dec 30 '20 at 16:00
  • Python also asks for input without DEBIAN_FRONTEND=noninteractive

    See https://stackoverflow.com/questions/63737359/trying-to-create-a-custom-docker-image-but-getting-asked-for-question-during-a-r

    – Alex G Sep 14 '21 at 13:14
15

When writing unattended scripts (including Dockerfiles), when using apt install -y but without DEBIAN_FRONTEND=noninteractive, sometimes installation will get stuck at an interactive prompt.

enter image description here

By running the install command with DEBIAN_FRONTEND=noninteractive these interactive xprompts are disabled.

Thai
  • 251