Search code examples
bashubuntugodebiansudoers

How to add DEBIAN_FRONTEND=noninteractive in sudoers.d file?


I am automating dpkg-sig installation for one of my project purpose. Now I want to install it noninteractive way.

I have added the following in a file inside /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

I am trying to install dpkg-sig using my golang code like this:

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

It is not able to install it. Getting the following error:

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

But when I remove DEBIAN_FRONTEND=noninteractive part from the installation command, it is working fine. How can run the installation noninteractive way?


Solution

  • No need to involve Bash (it might even open up for more bugs); just set the environment variable for the command:

    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()
    

    (Also, use apt-get, not apt, for non-interactive tasks.)