Set up automatic security updates on Ubuntu Debian VPS with unattended-upgrades

Set Up Automatic Security Updates on VPS with unattended-upgrades

Updated: October 29, 2026 · 8 min read · Ubuntu / Debian

An unpatched VPS is one of the highest-risk configurations you can run. Kernel and OpenSSH vulnerabilities are often exploited within 24–72 hours of disclosure. unattended-upgrades is the Debian/Ubuntu package that automatically downloads and installs security patches every night — no SSH session required.

This guide covers installation, configuring auto reboot after kernel updates, email notifications, and debugging common failures.

Why Automatic Security Patches Matter

Most admins intend to run apt upgrade regularly, but busy schedules mean servers sit weeks or months behind. unattended-upgrades solves this by touching only the security repository — it will not upgrade major versions that might break application dependencies.

Update TypeAuto Updated?Reason
Security patches✅ YesMinimizes attack window
Bug fixesOptionalEnable via config if desired
Major upgrades❌ NoRisk of dependency breakage

Install unattended-upgrades

Ubuntu 20.04+ ships with it pre-installed, but confirm and reinstall cleanly:

# Install the package and apt-listchanges
sudo apt update
sudo apt install -y unattended-upgrades apt-listchanges

# Enable automatic updates interactively
sudo dpkg-reconfigure --priority=low unattended-upgrades

The dpkg-reconfigure prompt asks if you want automatic updates — select Yes.

Main Configuration: /etc/apt/apt.conf.d/50unattended-upgrades

This file controls which repositories receive automatic updates. Ubuntu's defaults already enable the security repository:

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

Confirm these lines are not commented out:

Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}";
    "${distro_id}:${distro_codename}-security";
    // Enable backports if needed (not recommended for production):
    // "${distro_id}ESMApps:${distro_codename}-apps-security";
    // "${distro_id}ESM:${distro_codename}-infra-security";
};

Additional behavioral settings in the same file:

// Remove unused dependencies after upgrades
Unattended-Upgrade::Remove-Unused-Dependencies "true";

// Clean up old kernels (saves /boot space)
Unattended-Upgrade::Remove-New-Unused-Dependencies "true";

// Verbose logging (set to true for debugging)
Unattended-Upgrade::Verbose "false";

Configure Auto Reboot After Kernel Updates

A kernel security patch only takes effect after a reboot. Configure automatic reboots at 3 AM when traffic is lowest:

# In /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";
Tip: Automatic-Reboot-WithUsers "false" prevents a reboot while active SSH sessions are connected — it reduces the risk of abruptly cutting off someone's terminal.

Email Notifications

Send an email when packages are upgraded or an error occurs (requires mailx or postfix):

# In /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::MailReport "on-change";
// on-change = only when packages were updated
// always    = every run
// only-on-error = only on failure

Configure Run Frequency: 20auto-upgrades

The file /etc/apt/apt.conf.d/20auto-upgrades controls how often apt timers run (in days):

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";

A value of "1" means daily — correct for security updates.

Needrestart — Detect Services Using Old Libraries

needrestart identifies services still loaded with pre-patch library versions that need a restart to apply the fix:

sudo apt install -y needrestart

# List services that need restarting
sudo needrestart -r list

# Auto-restart services without prompting (for unattended servers)
sudo sed -i "s/#\$nrconf{restart} = 'i';/\$nrconf{restart} = 'a';/" /etc/needrestart/needrestart.conf

Verify unattended-upgrades Is Working Correctly

# Check apt timer status
systemctl status apt-daily.timer apt-daily-upgrade.timer

# Dry run — shows what would be upgraded without installing
sudo unattended-upgrades --dry-run --debug 2>&1 | head -40

# View recent upgrade log
sudo tail -30 /var/log/unattended-upgrades/unattended-upgrades.log

# View packages upgraded in the last run
sudo tail -20 /var/log/unattended-upgrades/unattended-upgrades-dpkg.log

# Check if a reboot is pending
ls /var/run/reboot-required 2>/dev/null && echo "REBOOT REQUIRED" || echo "No reboot needed"
Tip: If --dry-run shows "No packages found that can be upgraded unattended", the system is up-to-date — or double-check that your Allowed-Origins matches your distro's codename exactly.

Troubleshooting Common Issues

Error: "apt-daily.service failed to start"

Another apt process left a stale lock file. Clear it:

sudo systemctl stop apt-daily.service apt-daily-upgrade.service
sudo rm -f /var/lib/apt/lists/lock /var/cache/apt/archives/lock /var/lib/dpkg/lock*
sudo dpkg --configure -a
sudo apt update

Error: "E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a'"

A previous upgrade was interrupted, leaving the package database in an inconsistent state:

sudo dpkg --configure -a
sudo apt install -f
sudo apt update && sudo apt upgrade -y

Error: Kernel not being upgraded automatically

On some Ubuntu versions the kernel package origin may need an explicit entry in 50unattended-upgrades:

# Add to Allowed-Origins
"${distro_id}:${distro_codename}-updates";

# Verify kernel package origin
apt-cache policy linux-image-$(uname -r)

Error: unattended-upgrades runs but produces no log output

Verbose logging may be disabled, or the apt timer hasn't reached its run interval yet:

# Force an immediate run with verbose output
sudo unattended-upgrades -v

# Check timer schedule
systemctl list-timers | grep apt

Error: Email notifications not being sent

A mail agent must be installed before the Mail setting takes effect:

# Install ssmtp as a lightweight mail relay
sudo apt install -y ssmtp mailutils
sudo nano /etc/ssmtp/ssmtp.conf
# Set: mailhub, AuthUser, AuthPass for your SMTP provider

# Test
echo "Test from VPS" | mail -s "unattended-upgrades test" [email protected]

Frequently Asked Questions

Will unattended-upgrades update every installed package?

No — by default it only upgrades packages from the *-security origin listed in Allowed-Origins. General package upgrades (e.g. nginx 1.24 → 1.26) still require manual apt upgrade or adding additional origins to the config.

If unattended-upgrades reboots the server, will my services start automatically?

Yes — as long as your systemd services are configured with Restart=always or WantedBy=multi-user.target, they will start automatically after reboot. Test with a manual reboot before enabling auto reboot in production.

Should I enable unattended-upgrades on a production server?

Yes, for security patches. The risk of being exploited through an unpatched vulnerability is far higher than the rare chance that a security patch breaks something. For critical production systems, set Automatic-Reboot-Time to your lowest-traffic window and configure uptime monitoring to verify the server comes back after a reboot.