19

I have a shell script to automatically configure new Ubuntu virtual machines for my purposes. I would like this script to install and enable unattended-upgrades, but I cannot figure out how to do so without user interaction.

The usual way to enable upgrades is dpkg-reconfigure unattended-upgrades, but of course that is interactive. The noninteractive front end avoids asking any questions at all, and the text front end seems bound and determined to do its I/O with the tty and not with stdin/stdout.

1
  • 1
    I've not used this tool before, but it looks like enabling it writes a 1 to the two lines in the file /etc/apt/apt.conf.d/20auto-upgrades. If you manually write that file will it work?
    – mfisch
    Nov 17, 2012 at 3:43

6 Answers 6

22
+50

Just make a copy of /etc/apt/apt.conf.d/20auto-upgrades after configuring it the way you like, and drop that into place on your target machine. You could embed it in your script, or you could rsync or wget it in from a server, or whatever.

So basically your script might do something like this:

apt-get install unattended-upgrades
wget -O /etc/apt/apt.conf.d/20auto-upgrades http://myserver.mytld/confs/20auto-upgrades
/etc/init.d/unattended-upgrades restart

There's really no reason to monkey with the dpkg-reconfigure script at all.

If you don't want to fetch the conf file from a remote server, it's VERY very short and simple - the default version, which fetches and installs security updates only, looks like this:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

So you can just echo those lines into the config file directly with the following:

echo -e "APT::Periodic::Update-Package-Lists \"1\";\nAPT::Periodic::Unattended-Upgrade \"1\";\n" > /etc/apt/apt.conf.d/20auto-upgrades
4

If you want to use dpkg-reconfigure, you can set the value with "debconf-set-selections", and then reconfigure it in a noninteractive way.

echo "unattended-upgrades       unattended-upgrades/enable_auto_updates boolean true" | debconf-set-selections; dpkg-reconfigure -f noninteractive unattended-upgrades
0
0

You have to use sudo to echo into /etc/apt/apt.conf.d/20auto-upgrades

if [[ ! -f /etc/apt/apt.conf.d/20auto-upgrades.bak ]]; then
    sudo cp /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/20auto-upgrades.bak
    sudo rm /etc/apt/apt.conf.d/20auto-upgrades
    echo "APT::Periodic::Update-Package-Lists \"1\";
    APT::Periodic::Download-Upgradeable-Packages \"1\";
    APT::Periodic::AutocleanInterval \"30\";
    APT::Periodic::Unattended-Upgrade \"1\";" | sudo tee --append /etc/apt/apt.conf.d/20auto-upgrades
fi
0
dpkg-reconfigure -f noninteractive unattended-upgrades
0

I would suggest to insert configuration parameters to configure unattended-upgrades.

sudo touch /etc/apt/apt.conf.d/20auto-upgrades

echo "APT::Periodic::Update-Package-Lists "1"" >> /etc/apt/apt.conf.d/20auto-upgrades

echo "APT::Periodic::Unattended-Upgrade "1" " >> /etc/apt/apt.conf.d/20auto-upgrades

Same way you can configure/add parameters according to your requirement in conf file /etc/apt/apt.conf.d/50unattended-upgrades.

Don't forget to restart service to apply changes.

/etc/init.d/unattended-upgrades restart
0
0

Ubuntu 22.04

TL;DR

sudo sed -i.bak '/^APT::Periodic::Update-Package-Lists/ s/"0"/"1"/' /etc/apt/apt.conf.d/20auto-upgrades
sudo sed -i.bak '/^APT::Periodic::Unattended-Upgrade/ s/"0"/"1"/' /etc/apt/apt.conf.d/20auto-upgrades
sudo dpkg-reconfigure -f noninteractive unattended-upgrades

Why

debconf-set-selections does not work in this case.

During dpkg-reconfigure, the script /var/lib/dpkg/info/unattended-upgrades.config rewrites it back to the values in the config:

#!/bin/sh

set -e

AUTO_UPGRADE="/etc/apt/apt.conf.d/20auto-upgrades"

# Source debconf library.
. /usr/share/debconf/confmodule

# Load configuration from disk, if it exists.
if [ -e "$AUTO_UPGRADE" ]; then
    if grep -q 'APT::Periodic::Unattended-Upgrade "1";' $AUTO_UPGRADE ; then
        db_set unattended-upgrades/enable_auto_updates true
    elif grep -q 'APT::Periodic::Unattended-Upgrade "0";' $AUTO_UPGRADE ; then
        db_set unattended-upgrades/enable_auto_updates false
    fi
fi

db_input low unattended-upgrades/enable_auto_updates || true
db_go

Bonus

From a shell script, you can access debconf DB with /usr/share/debconf/confmodule.

#!/bin/sh
. /usr/share/debconf/confmodule
db_get unattended-upgrades/enable_auto_updates
echo Value before: ${RET}
db_set unattended-upgrades/enable_auto_updates $1

There are debconf libs for Python and Perl. An example of the latter is /usr/sbin/dpkg-reconfigure.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .