Automating OS Patch Windows: unattended-upgrades on Linux and PSWindowsUpdate on Windows

Unpatched hosts remain the most reliable way into a network, and manual patching never keeps pace. The fix is not a better reminder in someone’s calendar — it is a patch window that runs itself, reboots inside a bounded slot, and tells you afterwards what it did.

When a supervisor or an auditor asks how you keep operating systems current, “the team applies updates when they can” is not an answer that survives scrutiny. What survives is a documented, scheduled mechanism with an exclusion list, a controlled reboot, and a report. This piece builds that on both platforms: Debian and Ubuntu with unattended-upgrades, and Windows with the PSWindowsUpdate module driven from a scheduled job. The design goal is the same on each — patch automatically, reboot only when required and only inside the window, and never let an automatic update take down the one service that must not restart unannounced.

The two things that make automated patching safe

Automated patching earns its keep only if two controls are in place. The first is scope: apply security updates, not every available update. Feature and version upgrades belong in a change-managed window where someone is watching, not in an overnight job. The second is reboot discipline: a kernel or libc update needs a reboot to take effect, but that reboot has to happen when you say so, not the moment the package lands. Get those two right and the rest is plumbing. Get them wrong and you have automated your own outages.

Free · 4 minutes

Would you survive contact with a determined attacker — or an auditor?

Fourteen questions on access, patching, detection, and recovery — the basics that prevent most real incidents, and the ones most often assumed rather than verified. Banded finding on screen, full sheet by email.

There is a third, quieter control: an exclusion list. Databases, container runtimes and kernels are the usual candidates — packages you want to move deliberately, under observation, with a rollback plan. Both tools let you pin those out of the automatic path, and both let the rest flow.

Debian and Ubuntu: unattended-upgrades scoped to security

Install the package with apt install unattended-upgrades. The behaviour lives in two files. /etc/apt/apt.conf.d/50unattended-upgrades decides what gets upgraded and how reboots are handled; /etc/apt/apt.conf.d/20auto-upgrades switches the periodic run on. Here is a configuration scoped to security origins only, with a package blacklist and a reboot pinned to 03:30.

// /etc/apt/apt.conf.d/50unattended-upgrades — security only, gated reboot
Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}-security";
    "${distro_id}ESMApps:${distro_codename}-apps-security";
    "${distro_id}ESM:${distro_codename}-infra-security";
};

// Never auto-touch these — they move in a manual change window
Unattended-Upgrade::Package-Blacklist {
    "postgresql-.*";
    "docker-ce.*";
    "linux-image-.*";
};

// Reboot only when a package requires it, and only at 03:30
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";

// Report what happened, on change and on error
Unattended-Upgrade::Mail "ops@example.com";
Unattended-Upgrade::MailReport "on-change";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";

The Allowed-Origins block is doing the real work: it restricts the run to the security pockets, so a routine feature update never rides along. The blacklist keeps your database, your container runtime and your kernel images out of the automatic path. Automatic-Reboot-WithUsers "false" means a machine with an interactive session logged in will hold rather than kick someone off — set it to true for headless fleet hosts where nobody should be logged in anyway.

Enable the periodic run in the second file:

// /etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Before you trust it, dry-run it: unattended-upgrade --dry-run --debug prints exactly which packages it would take and which it would hold, without changing anything. Run that on a representative host and read the output — it is the cheapest way to catch a blacklist that does not match what you thought it did.

The run itself is triggered by apt-daily-upgrade.timer. On a fleet you do not want every host waking at the same instant and hammering the mirror, so override the timer with a drop-in and add jitter. If you have moved your other scheduled work off cron, this will feel familiar — the same reasons I set out in using systemd timers for a backup job that logs, retries and survives reboots apply here.

# systemctl edit apt-daily-upgrade.timer
[Timer]
# clear the vendor schedule, then set our own
OnCalendar=
OnCalendar=*-*-* 03:00
RandomizedDelaySec=1800
Persistent=true

The empty OnCalendar= line clears the packaged value before the new one is set; RandomizedDelaySec=1800 staggers the fleet across a half-hour so the reboots at 03:30 do not all land together.

Windows: PSWindowsUpdate on a scheduled job

Windows has no native equivalent that gives you scoping, exclusions and reporting in one place, but the community module PSWindowsUpdate does. Install it once, then drive it from a script. The following installs security and critical updates, excludes drivers and one known-bad KB, and writes a log.

# Install-PatchWindow.ps1 — run under SYSTEM from a scheduled task
Install-Module PSWindowsUpdate -Scope AllUsers -Force -ErrorAction SilentlyContinue
Import-Module PSWindowsUpdate

$stamp = Get-Date -Format 'yyyyMMdd-HHmm'
$log   = "C:\ProgramData\PatchWindow\install-$stamp.log"
New-Item -ItemType Directory -Path (Split-Path $log) -Force | Out-Null

# Show what is pending, then install — drivers and one KB held back
Get-WindowsUpdate -MicrosoftUpdate -NotCategory 'Drivers' `
    -NotKBArticleID 'KB5040442' | Out-File -Append $log

Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot `
    -NotCategory 'Drivers' -NotKBArticleID 'KB5040442' |
    Out-File -Append $log

-AcceptAll answers the licence and confirmation prompts, -AutoReboot restarts if an update needs it, and -NotCategory plus -NotKBArticleID are your exclusion list. The module talks to the Windows Update Agent COM API, so test it under the PowerShell edition you intend to run — it is most predictable under Windows PowerShell 5.1, and works under PowerShell 7.4 on current builds; confirm on your image before you trust it unattended.

Register the job as a scheduled task in code rather than clicking through the GUI, for the same reasons I set out in defining scheduled tasks as PowerShell code — the definition is reviewable, versionable and reproducible across the fleet. Run it as SYSTEM so it has the rights to install and reboot without an interactive session.

$action = New-ScheduledTaskAction -Execute 'pwsh.exe' `
    -Argument '-NonInteractive -NoProfile -File C:\Scripts\Install-PatchWindow.ps1'
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3:00am
$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' `
    -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName 'Patch Window' -Action $action `
    -Trigger $trigger -Principal $principal -Force

Keeping the whole patch definition in PowerShell sits naturally alongside managing the rest of the host the same way — the argument for configuring the Windows Defender firewall from PowerShell rather than the GUI is the argument for this: code you can review beats clicks nobody witnessed.

Reporting, staggering and the evidence trail

Patching that happens silently is only half the control. You need to know it ran and what it touched. On Linux, the Mail and MailReport "on-change" settings send a summary whenever something is applied, and /var/log/unattended-upgrades/ holds the detail. On Windows, Get-WUHistory returns the installed-update record for a host, which you can collect centrally:

Get-WUHistory -Last 20 |
    Select-Object Date, Title, Result |
    Export-Csv -Path 'C:\ProgramData\PatchWindow\history.csv' -NoTypeInformation

Across a fleet, stagger the windows deliberately. Do not patch every host at 03:00 — a bad update then takes the whole estate at once. Ring your fleet: a small canary group on the earliest slot, the bulk an hour later, the crown-jewel hosts last, after someone has confirmed the canaries came back healthy. The Linux RandomizedDelaySec and the Windows trigger time are where you encode that.

Done properly, you finish with the same posture on both platforms: security updates applied on a schedule, feature changes held for a human, dangerous packages excluded by name, reboots confined to a window, and a report that answers the auditor’s question before it is asked. The point is not that patching is now invisible. The point is that it is now provable — and the day someone asks you to prove it is the day you find out whether you built it or just meant to.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Free interactive tool

Interactive deadline calculator

Check which regulations apply to you and when

Regulation across the EU, UK, US and Asia-Pacific has moved considerably in the past eighteen months, and several headline dates have shifted more than once. Twelve questions, about three minutes.

Results are shown on screen — no email required. A dated summary is available to download, and can be sent on if that's more useful. What we do with your answers.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.

Full Governance by Sixteen Pillars

Govern your business. Prove your compliance.

A board assurance cockpit for EU-regulated financial firms — tamper-evident, hash-chained proof of governance across DORA, GDPR, NIS2, ISO 27001, the EU AI Act and MiCA. In development.

See what's coming