从Shell脚本启用无人值守升级


17

我有一个shell脚本可以自动配置新的Ubuntu虚拟机。我希望安装并启用此脚本unattended-upgrades,但是如果没有用户交互,我将无法解决该问题。

启用升级的通常方法是dpkg-reconfigure unattended-upgrades,但当然是交互式的。非交互式前端完全避免提出任何问题,并且文本前端似乎已绑定并确定使用tty而不是stdin / stdout进行其I / O。


1
我以前没有使用过此工具,但看起来好像启用它会在文件/etc/apt/apt.conf.d/20auto-upgrades中的两行中写入1。如果您手动写入该文件,它将起作用吗?
mfisch

Answers:


20

在按照自己的方式配置后,只需复制/etc/apt/apt.conf.d/20auto-upgrades副本,然后将其放到目标计算机上即可。您可以将其嵌入脚本中,也可以rsync或从服务器中获取它,或进行其他操作。

因此,基本上,您的脚本可能会执行以下操作:

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

根本没有任何理由使用dpkg-reconfigure脚本。

如果您不想从远程服务器获取conf文件,则它非常简短,非常简单-默认版本仅获取和安装安全更新,如下所示:

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

因此,您可以使用以下命令直接将这些行回显到配置文件中:

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

1

如果要使用dpkg-reconfigure,则可以使用“ debconf-set-selections”设置该值,然后以非交互方式重新配置它。

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

0

您必须使用sudo回显 /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

我建议插入配置参数以配置无人值守的升级。

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

您可以根据需要在conf文件中配置/添加参数的方法相同 /etc/apt/apt.conf.d/50unattended-upgrades

不要忘记重启服务以应用更改。

/etc/init.d/unattended-upgrades restart
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.