Setup Unattended Upgrades on Debian 9 Stretch
In this article
Setting up unattended upgrades on Debian 9 Stretch ensures that your system automatically downloads and installs security updates without requiring manual intervention. This process is critical for maintaining the security posture of a server, as it minimizes the window of vulnerability between the release of a security patch and its application to your machine. By automating this workflow, you eliminate the risk of forgetting to run apt-get upgrade and ensure that critical kernel patches and library updates are applied consistently across your infrastructure.
What You'll Need
- A server or workstation running Debian 9 (Stretch).
- Root or sudo privileges to modify system configuration files and install packages.
- An active internet connection to reach the Debian security and main repositories.
- A basic understanding of the Linux command line and the nano or vi text editors.
Step-by-Step
- Update Your Package Index: Before installing new software, ensure your local package index is up to date. Open your terminal and run
sudo apt-get update. This ensures you are downloading the most recent version of the unattended-upgrades package available in the Stretch repositories. - Install the Unattended-Upgrades Package: Execute the command
sudo apt-get install unattended-upgrades. This package provides the scripts and configuration framework necessary to automate the apt process. During the installation, the system may install dependencies such as apt-listchanges, which helps track what is changing in the updates. - Enable the Automation Trigger: Installing the package does not automatically activate the service. You must run the interactive configuration tool by typing
sudo dpkg-reconfigure -plow unattended-upgrades. When the prompt appears asking if you would like to automatically download and install stable updates, select "Yes" and press Enter. This action creates the file/etc/apt/apt.conf.d/20auto-upgrades, which tells the system how often to check for updates. - Configure the Update Sources: To fine-tune which updates are installed, you need to edit the main configuration file. Open it using a text editor:
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades. Look for theUnattended-Upgrade::Origins-Patternsection. By default, Debian 9 is configured to allow updates from the security repository. Ensure the line"${distro_id}:${distro_codename}-security";is uncommented (no double slashes at the start). If you wish to include general updates (non-security), you can uncomment the line for"${distro_id}:${distro_codename}";, though this is generally discouraged for production servers to maintain maximum stability. - Set Up Automatic Reboots: Some security updates, particularly kernel updates, require a reboot to take effect. To automate this, scroll down in the
50unattended-upgradesfile to findUnattended-Upgrade::Automatic-Reboot. Change the value from"false"to"true". To prevent the server from rebooting during business hours, findUnattended-Upgrade::Automatic-Reboot-Timeand set it to a specific time, such as"04:00"for 4 AM. - Configure Email Notifications: It is important to know when an update has failed or when a reboot has occurred. In the same configuration file, locate
Unattended-Upgrade::Mail. Enter your email address inside the quotes, for example:"admin@example.com". Note that for this to work, your system must have a working Mail Transfer Agent (MTA) like Postfix or Exim installed and configured to send outgoing mail. - Clean Up Old Packages: Over time, automatic updates will leave behind old kernels and obsolete dependencies that consume disk space. In the
50unattended-upgradesfile, findUnattended-Upgrade::Remove-Unused-Dependenciesand set it to"true". This ensures thatapt-get autoremoveis triggered automatically after updates. - Verify the Configuration: To ensure there are no syntax errors in your configuration files, you can perform a dry run. Run the command
sudo unattended-upgrade --dry-run --debug. This will simulate the update process and print the logs to your terminal. Check the output for any "Error" or "Warning" messages. If the dry run completes without critical failures, your setup is correct.
Common Mistakes
One of the most frequent errors is failing to configure a working MTA. Many users enter their email address in the configuration file but forget that Debian does not come with a pre-configured mail server. Without a tool like Postfix, the system will attempt to send an email and fail silently, leaving the administrator unaware of critical update failures. Always test your mail functionality using the mail command before relying on it for system alerts.
Another common mistake is the accidental enabling of the "updates" repository alongside the "security" repository on a highly sensitive production server. While security updates are designed to be low-risk, general updates can occasionally introduce new features or change configuration defaults that might break a custom application. Stick to security-only updates if stability is your primary concern.
Lastly, users often forget to check the available disk space in /boot. When unattended upgrades install new kernels, the old ones remain until cleaned. If the Remove-Unused-Dependencies option is not enabled, the /boot partition can fill up, which may cause future updates to fail or even prevent the system from booting correctly.
Tips
To enhance your monitoring, consider integrating the unattended-upgrades logs into a centralized logging system. The logs are located at /var/log/unattended-upgrades/unattended-upgrades.log. By monitoring this file, you can create alerts for when a specific package fails to update repeatedly.
If you have a cluster of Debian 9 servers, do not set them all to reboot at the exact same time (e.g., 04:00). Stagger the Automatic-Reboot-Time across your servers (e.g., 04:00, 04:15, 04:30) to ensure that your service availability remains high and you do not experience a total outage during the reboot window.
For those who want even more control, you can use the Unattended-Upgrade::Package-Blacklist section in the configuration file. This allows you to specify packages that should never be updated automatically. This is useful for database engines or proprietary software where a version jump might require a manual migration script before the update can be applied.
Troubleshooting
If you suspect that unattended upgrades are not running, the first step is to check the system logs. Use the command grep "unattended-upgrades" /var/log/syslog to see when the service last triggered. If you see no entries, check if the systemd timer or cron job is active. In Debian 9, the process is typically handled by a systemd timer; check its status with systemctl status apt-daily-upgrade.timer.
If the logs indicate that updates are being downloaded but not installed, check for "dpkg lock" errors. This happens when another process (like a manual apt-get install) is running at the same time as the unattended upgrade. The system will skip the update to avoid corruption. If this happens frequently, check for other automated scripts that might be locking the apt database.
In cases where the system fails to reboot despite the configuration, verify that the cron service is running and that the /etc/cron.d/apt-compat file exists. Some minimal Debian installations may lack the necessary cron components to trigger the reboot signal sent by the unattended-upgrades script.
Frequently Asked Questions
Does this replace manual updates entirely?
No. Unattended upgrades are designed for security patches. You should still perform manual updates periodically to handle major version jumps and to review the changelogs of non-security software you use.
Will this break my custom configuration files?
Generally, no. Apt's default behavior is to prompt the user when a configuration file changes. Since unattended upgrades are non-interactive, they typically keep your existing configuration files and save the new version as a .dpkg-dist file.
Can I use this on a Debian 9 VPS with limited RAM?
Yes, but be cautious. The update process consumes CPU and RAM. If your VPS is extremely resource-constrained, ensure you schedule the updates and reboots during your lowest traffic periods to avoid performance spikes.
How do I know if a reboot is pending?
You can check for the existence of the file /var/run/reboot-required. If this file exists, a package installation has flagged that the system needs a restart to apply changes.
Is it safe to automate updates for all packages?
It is safer to limit automation to security updates. Automating all updates increases the risk of a software regression affecting your production environment without your knowledge.