You've already forked auto-update-ubuntu
96 lines
2.6 KiB
Bash
Executable File
96 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Author : Allan Christensen
|
|
# First Created : 23062022 (DD-MM-YYYY)
|
|
# Description : Configures unattended updates 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, if not, 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 equivalent to 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 reboot even if users are logged in
|
|
#
|
|
# sed -i '/Unattended-Upgrade::Automatic-Reboot-WithUsers/ s/^\/\/\s*//' "$config2"
|
|
|
|
#
|
|
# Create cron job to run every Sunday at 04:00
|
|
#
|
|
cronjob01='0 4 * * 0 root /usr/bin/apt update && /usr/bin/unattended-upgrade -v >/dev/null 2>&1'
|
|
echo "$cronjob01" > /etc/cron.d/updatesystem
|
|
echo "" >> /etc/cron.d/updatesystem
|
|
|
|
#
|
|
# Ensure correct permissions for /etc/cron.d entries
|
|
#
|
|
chmod 644 /etc/cron.d/updatesystem
|
|
chown root:root /etc/cron.d/updatesystem
|
|
|
|
#
|
|
# Restart unattended upgrades and cron.
|
|
#
|
|
systemctl restart unattended-upgrades.service ; systemctl restart cron.service
|
|
|
|
#
|
|
# All done
|
|
#
|
|
printf "\nAll Done...\n"
|
|
|
|
#
|
|
# End of script
|
|
#
|