A server nobody patches is a server waiting to get owned. If you rent a VPS, you are the one responsible for applying security updates. Your provider secures the host node; the packages inside your VM are on you. The fix for “I forgot to run apt upgrade” is unattended-upgrades, a small Debian/Ubuntu tool that installs security patches for you on a schedule, and can reboot the machine when a patch actually requires it.
This guide walks through the whole setup on Ubuntu 22.04, 24.04, and Debian 11/12. It takes about ten minutes, needs no extra software beyond what ships with the distro, and at the end your server will patch itself every night and email you a report of what changed.
What unattended-upgrades Actually Does
Two files control the behavior. /etc/apt/apt.conf.d/20auto-upgrades sets the schedule: how often the package lists refresh and whether upgrades run automatically. /etc/apt/apt.conf.d/50unattended-upgrades decides what gets upgraded: security fixes only, or everything.
The key distinction is “security only” versus “all updates.” On a production server, the sane default is security-only. Regular package updates can pull in new features, changed config defaults, or new dependencies. Security patches are backported by the distro maintainers specifically to fix vulnerabilities without changing behavior. That is the set you want installed automatically while you sleep.
Step 1: Install unattended-upgrades
On Ubuntu it is usually already installed. On Debian it is not. Either way, run:
sudo apt update
sudo apt install -y unattended-upgrades
The installer runs dpkg-reconfigure and asks whether to enable automatic stable updates. Answer yes, or run it explicitly later:
sudo dpkg-reconfigure -plow unattended-upgrades
This just writes a minimal 20auto-upgrades file. We will overwrite it with our own values in step 3.
Step 2: Configure Security-Only Updates in 50unattended-upgrades
Open /etc/apt/apt.conf.d/50unattended-upgrades. The important block is Unattended-Upgrade::Allowed-Origins. For a standard security-only setup on Ubuntu, make it look like this:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
"${distro_id}ESMApps:${distro_codename}-apps-security";
"${distro_id}ESM:${distro_codename}-infra-security";
};
On Debian 12 the equivalent origins look like this (Debian names its security archive differently):
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
"${distro_id}:${distro_codename}-updates";
};
A few lines further down control whether packages on hold are skipped and whether the machine reboots when needed. The defaults are fine, but these two are worth setting explicitly:
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
These clean up old kernels and orphaned packages so /boot never fills up and kills the next upgrade. That last one is a classic VPS failure mode: a 512 MB /boot partition choked with four old kernels, and the security update that needed space to install itself.
Step 3: Set the Schedule in 20auto-upgrades
Now write /etc/apt/apt.conf.d/20auto-upgrades:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
Each line is a number of days. This configuration refreshes the package lists daily, downloads the upgradeable packages daily, runs the actual upgrade daily, and cleans the download cache weekly. If you prefer a weekly cadence, change the "1" values to "7", but daily is the right call for security fixes, which sometimes land within hours of a CVE disclosure.
Step 4: Handle Reboots the Safe Way
Some security updates (a kernel patch, a glibc fix) only take effect after a reboot. unattended-upgrades can do that automatically, but rebooting a server at 6 AM while your site is busy is how you wake up to angry users. Set a maintenance window in 50unattended-upgrades:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";
This reboots the machine at 03:30 server time, only when a reboot is actually required. If nothing pending needs a restart, nothing happens.
Two honest caveats. First, 03:30 means 03:30 in the server’s timezone, so check timedatectl and make sure it matches the quiet hours of your actual audience. Second, if your VPS runs something stateful, like a game server, a long-running queue worker, or an un-checkpointed database write pattern, an automatic reboot will interrupt it. In that case set Automatic-Reboot "false" and instead get notified that a reboot is pending (see the next section), then reboot by hand when traffic is low.
Step 5: Get Notified When Something Changes
Automatic updates without any reporting are a black box. Point the report at your email in 50unattended-upgrades:
Unattended-Upgrade::Mail "[email protected]";
The mailer needs a working MTA. The lightest option on a VPS is msmtp or ssmtp configured to relay through your provider’s SMTP or a transactional service. Full Postfix is overkill if the server exists only to send you these reports. Alternatively, leave Mail unset and read the log instead:
grep "Packages that will be upgraded" /var/log/unattended-upgrades/unattended-upgrades.log | tail -5
Step 6: Verify It Actually Works
Do not trust the configuration until you have seen it run. First, a dry run that simulates an upgrade without changing anything:
sudo unattended-upgrades --dry-run --debug
The output lists which origins match and which packages would be upgraded. If it says no packages match, that is normal on a freshly patched system. The mechanism is what you are testing, not whether updates exist.
Second, check the systemd timer that triggers the daily run:
systemctl status apt-daily-upgrade.timer
It should show active (waiting) with a next-trigger time. If the timer is missing or failed, the most common cause is a broken 20auto-upgrades. Run apt-config dump | grep Periodic to see what APT actually parsed.
What It Will Not Do for You
unattended-upgrades is deliberately conservative. Know the boundaries:
- No release upgrades. It will not move you from Ubuntu 22.04 to 24.04 or Debian 11 to 12. That stays a manual, planned operation.
- No upgrades that need your input. If a package’s maintainer scripts ask a question (a debconf prompt), the upgrade is skipped rather than answered blindly. Check the log for skipped packages occasionally.
- No application-level patching. A WordPress plugin, a Docker image, a Node app deployed from git: none of that is in APT’s world. Cover those with their own update routines.
- Reboot-required services still restart. Even without a full reboot, services like nginx or PHP-FPM get restarted when their packages update. Brief, but not zero.
Troubleshooting: The Three Problems You Will Hit
Updates run but nothing upgrades. Almost always a wrong origin in 50unattended-upgrades. Run the dry run with --debug and look for “Checking” lines. They show exactly which origins were tested against each package’s origin. Copy the block from this guide for your distro instead of hand-writing origins.
Disk fills up with old kernels. You skipped the Remove-Unused-Kernel-Packages line, or /boot is a separate tiny partition. Add the cleanup lines from step 2, then run sudo apt autoremove --purge once by hand to clear the backlog.
The server rebooted at the wrong time. The reboot time follows server local time, and many cheap VPS images ship set to UTC while your users are in Jakarta. Set the timezone with sudo timedatectl set-timezone Asia/Jakarta (or your region) before relying on Automatic-Reboot-Time.
FAQ
Is it safe to enable automatic updates on a production server?
Security-only automatic updates are the default recommendation from both Debian and Ubuntu security teams. The patches are backported to avoid behavior changes. The real risk is running unpatched: most VPS compromises exploit CVEs that had fixes available for weeks.
Should I enable updates for all packages, not just security?
On a personal or staging VPS, sure. Full automatic upgrades are fine and keep everything current. On anything serving real users, stick to security-only and update the rest manually on a schedule, so a changed default in some package does not surprise you at 3 AM.
How do I know a reboot is pending?
Check for the flag file: ls /var/run/reboot-required. If it exists, a package has requested a restart. With Automatic-Reboot "true" the machine reboots itself at your configured time; with it set to false, reboot manually when convenient.
Set this up once, right after provisioning, and it becomes part of your standard server checklist alongside SSH keys and a firewall. Ten minutes now saves you from the far more expensive exercise of cleaning up a compromised box later.