DEBIAN_FRONTEND 环境变量

Asa*_*uhi 38 apt environment-variables

我的潜在托管服务提供商建议在终端中运行命令,以使基于 KVM 的服务器的操作系统映像最小化。由于他们的 KVM 模板带有我不需要的包,我想我可能会使用相同的命令来删除不需要的包。

该命令以 开头DEBIAN_FRONTEND=noninteractive,然后apt-get remove按如下方式调用:

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 
Run Code Online (Sandbox Code Playgroud)

第一次遇到DEBIAN_FRONTEND环境变量,至今没找到有用的信息。所以我想知道将它设置为什么noninteractive,以及它是否可取,因为我认为值 ( noninteractive) 会持续存在。

ste*_*ver 46

在应用它的单个命令之后,简单地在apt命令前加上DEBIAN_FRONTEND=something并不会持续存在。

这些DEBIAN_FRONTEND选项记录在的第 7 节手册页中debconf(您可能需要安装该debconf-doc软件包才能在您的系统上使用它们)。来自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.
Run Code Online (Sandbox Code Playgroud)

它还指出:

   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.
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果将 `DEBIAN_FRONTEND` 与 `sudo` 一起使用,请在 `sudo` 命令中设置变量,而不是为它设置变量。即:`sudo DEBIAN_FRONTEND=noninteractive apt-get install slrn`。如果将 env 变量设置放在 `sudo` 之前,它将对 `sudo` 命令本身有效,并且 `sudo` 不会将其复制到正在运行的 `apt-get` 命令中。在 `sudo` 和实际命令之间使用它是 `sudo` 语法,用于为要运行的命令设置 env 变量(参见手册页)。这让我很难受,所以我想分享。 (12认同)
  • 我看到 Dockerfiles 中常用的`DEBIAN_FRONTEND=noninteractive`。这个答案让我想知道为什么我们不在 apt 命令中使用 `--yes` 标志,因为它会更明确,并且在大多数情况下它会满足我们的需求。 (7认同)
  • 如果要将前端默认行为更改为 `noninteractive`,可以使用以下命令:`dpkg-reconfigure debconf --frontend=noninteractive` (6认同)
  • @Dennis 两句话。据我了解,“apt”没有稳定的命令行界面,因此不推荐(对于 Dockerfile/Containerfile),而“apt-get”是。而且“DEBIAN_FRONTEND”变量还会影响一些在后台调用的命令,而不仅仅是“apt”/“apt-get”。如果您收到有关无法正常运行的终端的警告,_这_是您可以抑制它们的方法。另一方面,“--yes”对于这些情况不会改变任何事情。 (2认同)

小智 15

在编写无人值守脚本(包括 Dockerfile)时,如果使用apt install -y不使用 DEBIAN_FRONTEND=noninteractive,有时安装会卡在交互式提示符处。

在此输入图像描述

通过运行安装命令并DEBIAN_FRONTEND=noninteractive禁用这些交互式 xprompts。