如何在 sudoers.d 文件中添加 DEBIAN_FRONTEND=noninteractive?

问题描述

我正在为我的项目目的之一自动安装 dpkg-sig。现在我想以非交互方式安装它。

我在 /etc/sudoers.d/ 中的文件添加了以下内容

Cmnd_Alias DPKGSIG_INSTALL = /usr/bin/apt install -y dpkg-sig,\
                            /bin/apt install -y dpkg-sig
abc ALL=(root) nopASSWD: DPKGSIG_INSTALL
Defaults:abc !requiretty

我正在尝试使用我的 golang 代码安装 dpkg-sig,如下所示:

installDpkgSig := "/usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install -o Dpkg::Options::=--force-confold -y dpkg-sig"
executor.cmd = *exec.Command("bash","-c",installDpkgSig)

无法安装。收到以下错误

sudo: sorry,you are not allowed to set the following environment variables: DEBIAN_FRONTEND

但是当我从安装命令中删除 DEBIAN_FRONTEND=noninteractive 部分时,它工作正常。 如何以非交互方式运行安装?

解决方法

无需涉及 Bash(它甚至可能会导致更多错误);只需为命令设置环境变量:

cmd := exec.Command("sudo","apt-get","install","-o","Dpkg::Options::=--force-confold","-y","dpkg-sig")
cmd.Env = append(os.Environ(),"DEBIAN_FRONTEND=noninteractive")
err := cmd.Run()

(另外,对于非交互式任务,使用 apt-get 而不是 apt。)

,

终于可以解决这个问题了。它不需要对 /etc/sudoers.d/ 文件进行任何更改。

将上面的代码修改如下,成功了。

installDpkgSig := "export DEBIAN_FRONTEND=noninteractive && /usr/bin/sudo apt install -o Dpkg::Options::=--force-confold -y dpkg-sig"
executor.cmd = *exec.Command("bash","-c",installDpkgSig)

当 bash 会话结束时,DEBIAN_FRONTEND 将被设置为默认值。