How to Use Ubuntu unattended-upgrades for Automated Security Patching

The Patch Management Crisis

In a large Linux infrastructure, maintaining strict security compliance is an operational nightmare. When a critical vulnerability like “Heartbleed” or “Shellshock” is announced, attackers begin scanning the internet and exploiting unpatched servers within hours.

Relying on a human administrator to manually SSH into 150 different Ubuntu servers every week to run sudo apt update && sudo apt upgrade is fundamentally flawed. It is slow, highly prone to human error, and guarantees that servers will be vulnerable during weekends or holidays.

To eliminate this vulnerability window, Ubuntu provides the unattended-upgrades package. When properly configured, this service runs silently in the background via a systemd timer, automatically downloading and installing critical security patches the exact moment Canonical publishes them, ensuring your fleet remains continuously fortified without human intervention.

Step 1: Installing and Enabling the Package

On modern Ubuntu Server editions, the unattended-upgrades package is usually installed by default, but its execution is often disabled or misconfigured.

First, ensure the package is installed:

sudo apt update
sudo apt install unattended-upgrades apt-listchanges -y

Next, you must explicitly enable the automated execution. You can do this by running the interactive configuration wizard:

sudo dpkg-reconfigure -plow unattended-upgrades

A pink terminal interface will appear asking: “Automatically download and install stable updates?” Select Yes.

This creates or modifies the /etc/apt/apt.conf.d/20auto-upgrades file, ensuring that the package lists are updated daily (APT::Periodic::Update-Package-Lists "1";) and the unattended upgrades script runs daily (APT::Periodic::Unattended-Upgrade "1";).

Step 2: Configuring the Allowed Repositories

The core configuration file dictates exactly what gets automatically updated. You do not want a major version upgrade of your PostgreSQL database to occur automatically at 3:00 AM, as it might break application schemas. You only want security patches.

Open the primary configuration file:

sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

Look at the Unattended-Upgrade::Allowed-Origins section. By default, Ubuntu explicitly restricts automated upgrades to the “security” repository:

Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}";
    "${distro_id}:${distro_codename}-security";
    // Extended Security Maintenance; doesn't necessarily exist for
    // every release and this system may not have it installed, but if
    // available, the policy for updates is such that unattended-upgrades
    // should also install from here by default.
    "${distro_id}ESMApps:${distro_codename}-apps-security";
    "${distro_id}ESM:${distro_codename}-infra-security";
};

Ensure that lines like "${distro_id}:${distro_codename}-updates"; remain commented out (prefixed with //), otherwise standard bug-fix updates will also install automatically, which increases the risk of unexpected software behavior.

Step 3: Blacklisting Specific Packages

If you have a hyper-sensitive application, such as a custom compiled web server or a fragile database cluster (e.g., Elasticsearch), you can explicitly forbid unattended-upgrades from touching those specific packages, even if a security patch exists.

Scroll down in the same 50unattended-upgrades file to the Package-Blacklist section.

Unattended-Upgrade::Package-Blacklist {
    "elasticsearch";
    "nginx";
    "libc6";
};

These packages will be ignored by the automation, forcing the administrator to patch them manually during a controlled maintenance window.

Step 4: Managing Automatic Reboots

The most controversial aspect of automated patching involves kernel updates. When a Linux kernel receives a security patch, the patch is installed to the hard drive, but the server is still running the old, vulnerable kernel in RAM. The server must reboot to apply the new kernel.

By default, unattended-upgrades will never reboot the server. It will simply create a file named /var/run/reboot-required to notify the administrator.

If you have a highly resilient cluster (e.g., 5 load-balanced web servers) and you want them to automatically reboot themselves to apply kernel patches, you must enable the reboot feature.

Scroll down to the reboot section and modify these two lines:

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";

This instructs the server to reboot autonomously, but only at exactly 3:00 AM, minimizing user disruption.

Step 5: Verifying the Logs

Because this process runs completely invisibly, you must occasionally audit the logs to ensure your servers are actually patching themselves.

The system logs all activity to a dedicated directory:

cat /var/log/unattended-upgrades/unattended-upgrades.log

If you want to manually trigger the script right now (to test your configuration or immediately apply a zero-day patch without waiting for the daily timer), you can run it in dry-run mode, accompanied by the debug flag:

sudo unattended-upgrade -d --dry-run

Conclusion

In the modern threat landscape, manual patch management is a liability. By strictly configuring the unattended-upgrades package to target only the Canonical security repositories, utilizing package blacklists, and strategically defining automatic reboot windows, Linux administrators can achieve continuous, zero-day security compliance without sacrificing the stability of their production applications.

RELATED POSTS

  • How to Use Ubuntu multipathd for Redundant SAN Storage Connections
  • How to Install and Configure the Postfix Mail Server on Ubuntu
  • How to Configure Linux PAM (Pluggable Authentication Modules) for Two-Factor Authentication
  • How to Configure a UFW Firewall to Block Specific IP Ranges in Ubuntu
  • How to Install and Configure the Proxmox Backup Server (PBS) Client on Debian
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.