Automatic Updates in Debian/Ubuntu
Name: Kirill Reshetnikov Contacts: kiryxa20692@gmail.com About: System engineer, passionate about process automation, microservices, and optimizing high-load systems. Links: Achievements:
Greetings!

In this article, I’ll explain how to configure automatic updates in Debian/Ubuntu using the unattended-upgrades package. We’ll cover two approaches: manual configuration in the terminal and tasks for an Ansible playbook. We’ll also look at how to restrict automatic installation to security updates only. The example uses a virtual machine running Debian 13.

Introduction

In a production environment, keeping the system up to date and promptly installing updates that address known vulnerabilities is critical. In practice, however, this is not always possible because of constraints imposed by installed software. For example, applications running on a server may be incompatible with newer versions of system components because they are no longer maintained or have strict dependency requirements.

In these circumstances, a compromise is to configure automatic installation of security updates, which minimize changes affecting user-facing software and applications while reducing the system’s overall exposure to vulnerabilities.

However, it is important to understand the limitations of this approach:

This approach should therefore be considered a temporary risk-reduction measure, not a complete replacement for regularly updating the entire software stack.

Software used in this article:

SoftwareVersion
Debian13.4

Configuring automatic updates in Linux

Option 1: manual configuration in the server terminal

You can enable automatic updates with a few commands, but first you need to install the appropriate packages.

In this example, we’ll use an account with sudo access, so the command is:

BASH
sudo apt install -y unattended-upgrades apt-listchanges
Click to expand and view more

After installing the required packages, reconfigure automatic updates:

BASH
sudo dpkg-reconfigure --priority=low unattended-upgrades
Click to expand and view more

In the dialog that appears, enable automatic updates by selecting Yes (Да in the Russian-language screenshot below).

You will then find two new files in the apt package manager configuration directory:

BASH
# Enables daily checks for updates
/etc/apt/apt.conf.d/20auto-upgrades

# Configures automatic update installation
/etc/apt/apt.conf.d/50unattended-upgrades
Click to expand and view more

By default, updates are configured to come from all channels except those that have not been tested:

Output
╭─(12:58:49)-(0)-(llm-b01-p)-[~]
╰─[root]# grep -vE '^//' /etc/apt/apt.conf.d/50unattended-upgrades
grep -vE '^//' /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Origins-Pattern {
        // Codename based matching:
        // This will follow the migration of a release through different
        // archives (e.g. from testing to stable and later oldstable).
        // Software will be the latest available for the named release,
        // but the Debian release itself will not be automatically upgraded.
        "origin=Debian,codename=${distro_codename},label=Debian";
        "origin=Debian,codename=${distro_codename},label=Debian-Security";
        "origin=Debian,codename=${distro_codename}-security,label=Debian-Security";

        // Archive or Suite based matching:
        // Note that this will silently match a different release after
        // migration to the specified archive (e.g. testing becomes the
        // new stable).
};

Unattended-Upgrade::Package-Blacklist {
    // The following matches all packages starting with linux-

    // Use $ to explicitely define the end of a package name. Without
    // the $, "libc6" would match all of them.

    // Special characters need escaping

    // The following matches packages like xen-system-amd64, xen-utils-4.1,
    // xenstore-utils and libxenstore3.0

    // For more information about Python regular expressions, see
    // https://docs.python.org/3/howto/regex.html
};
Click to expand and view more

For typical use cases where packages need to be kept up to date, this configuration is sufficient and meets basic system security and maintenance requirements.

However, if the server runs outdated software or applications that depend on specific dependency versions and are incompatible with updated libraries, this update policy may cause failures or break some functionality.

In these scenarios, it makes sense to restrict automatic installation to updates from the security repository. This reduces the risk of introducing changes that could affect applications and user-facing software.

To do this, remove the unrelated sources from the Unattended-Upgrade::Origins-Pattern block in /etc/apt/apt.conf.d/50unattended-upgrades, leaving only entries that match the security repository.

A minimal configuration looks like this:

Output
Unattended-Upgrade::Origins-Pattern {
    "origin=Debian,codename=${distro_codename}-security,label=Debian-Security";
};
Click to expand and view more

Keep in mind that even installing security patches alone does not completely eliminate the risk of breaking software that depends on specific dependency versions. Vulnerability fixes may affect libraries and components used by applications.

For this reason, it is recommended to:

Option 2: configuring updates in an Ansible playbook

The first approach, manual configuration, is suitable for small infrastructures without centralized automation and for scenarios that require fine-tuning, where automatically installing all updates could break older software.

In larger infrastructures, virtual machines and servers are usually configured automatically using tools such as Terraform, OpenTofu, SaltStack, and Ansible. The previous approach may therefore be unsuitable if, for example, we want to deploy and configure a group of virtual machines at once.

In this scenario, it makes more sense to add the appropriate tasks to the main configuration playbook.

Here is an example of initial server setup tasks that install all updates available at the time of execution and enable automatic updates:

YAML
  tasks:
    - name: Update the system
      ansible.builtin.apt:
        update_cache: true
        upgrade: dist
        autoremove: true

    - name: Install packages for automatic updates
      ansible.builtin.apt:
        name:
          - unattended-upgrades
          - apt-listchanges
        state: present

    - name: Enable unattended-upgrades
      ansible.builtin.copy:
        dest: /etc/apt/apt.conf.d/20auto-upgrades
        content: |
          APT::Periodic::Update-Package-Lists "1";
          APT::Periodic::Unattended-Upgrade "1";
        owner: root
        group: root
        mode: '0644'

    - name: Configure security updates
      ansible.builtin.copy:
        dest: /etc/apt/apt.conf.d/50unattended-upgrades
        content: |
          Unattended-Upgrade::Origins-Pattern {
                  "origin=Debian,codename=${distro_codename}-security,label=Debian-Security";
                  "origin=Ubuntu,archive=${distro_codename}-security";
          };

          Unattended-Upgrade::Remove-Unused-Dependencies "true";
        owner: root
        group: root
        mode: '0644'

    - name: Ensure the unattended-upgrades service is enabled and running
      ansible.builtin.systemd:
        name: unattended-upgrades
        enabled: true
        state: started
Click to expand and view more

The Origins-Pattern block contains two lines: the first is for Debian, and the second is for Ubuntu. The line that does not apply to the system simply matches nothing and causes no interference, so the same playbook works on both distributions.

The Unattended-Upgrade::Remove-Unused-Dependencies "true" setting enables automatic removal of unused dependencies.

Automatic reboots are disabled by default in this setup. If you want the server to reboot without administrator intervention after updates that require it (such as kernel updates), add these two settings to the content field of the “Configure security updates” task:

Output
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";
Click to expand and view more

Conclusion

Automatic updates are an essential practice for maintaining system security. In typical operating scenarios, the default settings are sufficient to keep packages up to date without manual intervention.

When a server runs outdated software or applications that depend on specific dependency versions, it is better to restrict automatic updates to security patches and update everything else manually as needed. This approach reduces the risk of breaking sensitive software, but does not eliminate it completely or address every potential vulnerability.

Even in this mode, additional oversight is necessary: reviewing logs, tracking changes, and testing updates whenever possible. In the long term, this approach should be considered temporary, with a plan to modernize or replace the outdated software if possible.

Copyright Notice

Author: Kirill Reshetnikov

Link: https://r4ven.me/en/automation/avtomaticheskaya-ustanovka-obnovleniy-v-debian-ubuntu/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
↵
ESC
⌘K Shortcut