Linux automatic updates

In this post I would like to show how to set up automatic updates on a linux system with a simple bash script:

  1. Open a nano editor to create the auto-update-setup.sh script:
nano auto-update-setup.sh
  1. Paste the following code into the nano editor:
#!/bin/bash

# Install unattended-upgrades package
sudo apt update
sudo apt install -y unattended-upgrades

# Enable automatic upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

# Create or modify the configuration for unattended-upgrades
cat <<EOL | sudo tee /etc/apt/apt.conf.d/50unattended-upgrades
// Automatically upgrade packages from these origins
Unattended-Upgrade::Allowed-Origins {
    "\${distro_id}:\${distro_codename}-security";
    "\${distro_id}:\${distro_codename}-updates";
};

// Remove unused dependencies after upgrades
Unattended-Upgrade::Remove-Unused-Dependencies "true";

// Automatically reboot if required after upgrades
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
EOL

# Enable periodic updates in APT configuration
echo "Enabling periodic updates in APT..."
cat <<EOL | sudo tee /etc/apt/apt.conf.d/10periodic
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
EOL

# Restarting unattended-upgrades service to apply the changes
sudo systemctl restart unattended-upgrades

Save and close the file with the following key combination Ctrl+O and then Ctrl+X

  1. Make the script executable:
chmod +x auto-update-setup.sh
  1. Execute the script:
sudo ./auto-update-setup.sh

Previous Post Next Post

Add a comment