Files
auto-update-ubuntu/auto-update-ubuntu
2025-10-23 15:39:14 +02:00

89 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Author : Allan Christensen
# First Created : 23062022 (DD-MM-YYYY)
# Description : Configures unattended updates on on Ubuntu 24.04
# License : MIT License (see LICENSE file for details)
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then printf "\nMust be root or use sudo!\n\n"; exit; fi
#
# Define variables and functions
#
config1="/etc/apt/apt.conf.d/20auto-upgrades"
config1alt="/usr/share/unattended-upgrades/20auto-upgrades"
config2="/etc/apt/apt.conf.d/50unattended-upgrades"
config2alt="/usr/share/unattended-upgrades/50unattended-upgrades"
#
# Function to check if config exists, else copy from alternative location
#
chkcfg () { if [ ! -f "$1" ]; then echo "Config missing: $1 → copying from $2" ; cp -Rp "$2" "$1" ; fi; }
#
# Install required packages
#
apt install -y unattended-upgrades update-notifier-common
#
# Ensure configuration files exist
#
chkcfg "$config1" "$config1alt"
chkcfg "$config2" "$config2alt"
#
# Back up configuration files
#
cp -Rp "$config1" /root/20auto-upgrades.orig
cp -Rp "$config2" /root/50unattended-upgrades.orig
#
# Disable automatic updates (20auto-upgrades)
#
sed -i 's/^\(APT::Periodic::Unattended-Upgrade\s*"\)1"/\10"/' "$config1"
#
# Enable package updates (50unattended-upgrades)
#
sed -i '/Unattended-Upgrade::Allowed-Origins/ s|//\s*"\${distro_id}:\${distro_codename}-updates"|"\${distro_id}:\${distro_codename}-updates"|' "$config2"
#
# Uncomment the next 2 lines to enable automatic removal of unused packages this equals apt autoremove
#
# sed -i '/Unattended-Upgrade::Remove-Unused-Dependencies/ s/^\/\/\s*//' "$config2"
# sed -i 's/Remove-Unused-Dependencies "false"/Remove-Unused-Dependencies "true"/' "$config2"
#
# Uncomment the next 2 lines to enable automatic reboot
#
# sed -i '/Unattended-Upgrade::Automatic-Reboot/ s/^\/\/\s*//' "$config2"
# sed -i 's/Automatic-Reboot "false"/Automatic-Reboot "true"/' "$config2"
#
# Uncomment the line below to enable automatic rebbot even if users are logged in
#
# sed -i '/Unattended-Upgrade::Automatic-Reboot-WithUsers/ s/^\/\/\s*//' "$config2"
#
# Create cronjob running every Sunday at 04:00
#
cronfile="/etc/cron.d/updatesystem"
cronjob='0 4 * * 0 root /usr/bin/apt update && /usr/bin/unattended-upgrade -v >/dev/null 2>&1'
#
# Restart unatended upgrades and cron.
#
systemctl restart unattended-upgrades.service ; systemctl restart cron.service
#
# All done
#
printf "\nAll Done...\n"
#
# End of script
#