使apt-get(或aptitude)以-y运行,但不提示替换配置文件?

在Ubuntu 10.04上运行apt-get -y install <packages ...>时,我希望apt-get(或者如果更容易的话,是aptitude)在安装附加依赖时不提示我(根据我理解的-y的行为),而且询问我是否覆盖配置文件,而是始终保留现有的配置文件(通常是默认设置)。不幸的是,--trivial-only似乎是-y的相反,并且不影响显示的提示,根据man页面的说明。
特别是像sambanullmailerlocalepurgelighttpd这样的软件包迫使我与终端进行交互,尽管整个过程都是脚本化的,旨在非交互式地执行。
1个回答

你可以使用:

sudo apt-get update
sudo apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade

仅针对特定的软件包,例如mypackage1和mypackage2:

sudo apt-get update
sudo apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install mypackage1 mypackage2

来源:http://raphaelhertzog.com/2010/09/21/debian-conffile-configuration-file-managed-by-dpkg/
Avoiding the conffile prompt

Every time that dpkg must install a new conffile that you have modified
(and a removed file is only a particular case of a modified file in dpkg’s eyes),
it will stop the upgrade and wait your answer. This can be particularly annoying for
major upgrades. That’s why you can give predefined answers to dpkg with the help
of multiple --force-conf* options:

    --force-confold: do not modify the current configuration file, the new version
is installed with a .dpkg-dist suffix. With this option alone, even configuration
files that you have not modified are left untouched. You need to combine it with
--force-confdef to let dpkg overwrite configuration files that you have not modified.
    --force-confnew: always install the new version of the configuration file, the
current version is kept in a file with the .dpkg-old suffix.
    --force-confdef: ask dpkg to decide alone when it can and prompt otherwise. This
is the default behavior of dpkg and this option is mainly useful in combination with
--force-confold.
    --force-confmiss: ask dpkg to install the configuration file if it’s currently
missing (for example because you have removed the file by mistake).

If you use Apt, you can pass options to dpkg with a command-line like this:

$ apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade

You can also make those options permanent by creating /etc/apt/apt.conf.d/local:

Dpkg::Options {
   "--force-confdef";
   "--force-confold";
}

你可以在dpkg手册的http://manpages.ubuntu.com/manpages/xenial/en/man1/dpkg.1.html或者man dpkg中找到更多信息和更多选项,例如搜索"confdef"。

38我相信这是不言自明的。然后继续使用我从未见过任何人使用的apt-get选项。 - notbad.jpeg
2@notbad.jpeg:我认为这个评论是针对那些选项的命名。我觉得这些名字确实很容易理解。当然,知道如何使用它们可能不太容易 :-D - 0xC0000022L
我认为它们是互斥的(强制默认与强制旧版本),不明白为什么你要同时指定两者... - tink
@tink,没有--force-confold--force-confnew是互斥的。在某些情况下(我不太确定是什么情况),可能没有明确的默认操作可以执行,因此--force-confdef不知道该怎么做。在这种情况下,选择--force-confold。但是,默认情况下可以安装新的配置文件,我知道某个服务器曾经这样做过,因为一些选项是错误的(不安全),所以他们会覆盖您的文件,而不是让您使用错误的信息...(但是他们首先备份了文件)。 - Alexis Wilke
4关于 -y 呢? - JDS
4参见:https://linux.die.net/man/1/dpkg 在“--force”部分下,它描述了“confold”和“confdef”选项。还有一个有用的链接:https://askubuntu.com/questions/254129/how-to-display-all-apt-get-dpkgoptions-and-there-current-values,可以使用`apt-config dump`命令来显示所有apt-get和dpkg选项及其当前值。 - thom_nic
3“不言自明”……嗯,我发现这个描述非常令人困惑,特别是在是否要将它们结合使用的问题上。那个让我明白一切的是 dpkg(1)。谢谢 @thom_nic。 - Lloeki
回答根据评论进行了更新 :) - Savvas Radevic
似乎在http://raphaelhertzog.com/2010/09/21/debian-conffile-configuration-file-managed-by-dpkg/和http://manpages.ubuntu.com/manpages/xenial/en/man1/dpkg.1.html之间存在矛盾,关于同时使用`confdef`和`confold`的行为。至少可以说,这些文档很令人困惑,我认为最好有人通过编辑这个回答来澄清一下。 - Johnny Utahh
1现在,有人可以确认一下我对以下观点的解释是否正确吗?当同时使用confoldconfdef时,意思是:“覆盖未更改的配置文件(如果升级的软件包有新的配置文件),并保留已更改的配置文件。” - Johnny Utahh