Is there any way to set DEBIAN_FRONTEND=noninteractive
when executing a script remotely via SSH?
Doing so I get this error:
sudo: sorry, you are not allowed to set the following environment variables: DEBIAN_FRONTEND
The reason I want to set DEBIAN_FRONTEND=noninteractive
at exactly this place is because sometimes apt-get dist-upgrade
shows a mask for some user interaction (whiptail). As want to run this script remotely via crontab -e
there is no human to klick or press anything. Hence, I need to turn that off as the script would otherwise remain stuck.
The script I am trying to execute is located on server_one
in /usr/local/bin/perform-update
:
#!/bin/bash
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
sudo apt-get -y autoremove
sudo snap refresh
I try to call it from server_two
using this command:
SERVER_COMMAND="perform-update"
ssh -i $HOME/.ssh/id_rsa backupuser@server_one $SERVER_COMMAND
I have also set the appropriate rights for backupuser
on server_one
in sudo visudo
:
backupuser ALL=(ALL) NOPASSWD: /usr/local/bin/perform-update
backupuser ALL=(ALL) NOPASSWD: /usr/bin/apt-get
backupuser ALL=(ALL) NOPASSWD: /usr/bin/snap
You could allow the user to set the
DEBIAN_FRONTEND
variable when usingsudo
, but ... you have granted permission for the user to execute the script usingsudo
, so there's no reason to then usesudo
inside the script. Just run the script itself withsudo
.Change your script to:
And then run it using:
That said, you should be using
unattended-upgrades
instead of re-inventing the wheel.For the particular case of allowing a user to set an environment variable for a specific command, add the
SETENV
tag in addition to theNOPASSWD
tag. Fromman sudoers
:So the rule should look like:
For all practical intents and purposes please see @muru's answer. If in a hurry, only the
sudo
change on thessh
line on server_two is necessary, and the/usr/local/bin/perform-update
script on server_one does not need to be edited. But for security reasons, it should be followed in its entirety, plus my edit at the end of my answer.What if we consider things the other way? What if we couldn't change the command on server_two and could only change the command on server_one? I was looking for an APT option to just set an environment variable, but I found more than I expected. Here is an outrageous out-of-the-box answer just to demonstrate that your
/etc/sudoers
policy needs to be secured.I do not think you intended to give
backupuser
full root access. To secure your system, you need to the removeapt-get
andsnap
lines invisudo
, then follow @muru's answer.EDIT: Except you need to remove
apt-get
line instead invisudo
. Those 3 lines should be replaced with just the first line: