
# Ubuntu自动更新：用unattended-upgrades自动安装安全更新

**Ubuntu自动更新由unattended-upgrades软件包完成：它每天自动下载并安装一次安全更新。** Ubuntu Server默认已启用它，但仍建议核实：`/etc/apt/apt.conf.d/20auto-upgrades`中的两项设置都应为`"1"`，并且`sudo unattended-upgrade -v --dry-run`应列出安全更新源。这样，OpenSSL、OpenSSH或内核的补丁会自动安装到服务器上，无需您登录。

本文介绍如何开启自动更新、重启、排除软件包、查看日志、处理“could not get lock”报错，以及如何关闭。命令依据24.04 LTS的[Ubuntu Server官方文档](https://ubuntu.com/server/docs/how-to/software/automatic-updates/)，并附Debian 12部分。



## 检查自动更新是否已开启

```bash
apt list --installed 2>/dev/null | grep unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgrades
```

预期输出：

```
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
```

`"1"`表示每天一次。实际工作由systemd定时器完成：

```bash
systemctl list-timers 'apt-daily*'
```

`apt-daily.timer`负责刷新软件包列表，`apt-daily-upgrade.timer`负责安装更新。

## 如何在Ubuntu 24.04上开启unattended-upgrades

如果缺少软件包或`20auto-upgrades`文件：

```bash
sudo apt update
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades   # answer Yes; writes 20auto-upgrades
```

在不做任何修改的情况下预览将要安装的内容：

```bash
sudo unattended-upgrade -v --dry-run
```

## 更新哪些内容：Allowed-Origins

更新源配置在`/etc/apt/apt.conf.d/50unattended-upgrades`中。Ubuntu默认只安装安全更新：

```
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";
//  "${distro_id}:${distro_codename}-updates";
};
```

`-updates`保持注释状态。这对服务器来说是合理的默认值：安全修复自动安装，常规更新由您决定何时进行，方法见[如何更新Ubuntu服务器](https://0x.cloud/zh/kb/servers/ubuntu-gengxin-xitong)。

**常见陷阱：第三方软件源不在范围内。** 来自`download.docker.com`的Docker、来自NodeSource的Node.js以及PPA都不在`Allowed-Origins`中，unattended-upgrades会跳过它们。请手动更新，或者把它们的Origin加入列表（`Origin`字段可在`/var/lib/apt/lists/`下该软件源的`InRelease`文件中找到）。

## 重启、清理和排除软件包

把您的修改放在一个单独的文件中，让它在默认配置之后加载，这样软件包升级时就不会询问配置文件已被修改：

```bash
sudo tee /etc/apt/apt.conf.d/52unattended-upgrades-local > /dev/null <<'EOF'
// reboot at 04:00 server time if a kernel update requires it
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
// remove dependencies that are no longer needed
Unattended-Upgrade::Remove-Unused-Dependencies "true";
// never touch these packages
Unattended-Upgrade::Package-Blacklist {
    "postgresql-";
};
EOF
```

| 选项 | 默认值 | 何时修改 |
|---|---|---|
| `Automatic-Reboot` | `false` | 如果夜间停机一分钟可以接受（机器人、n8n），就开启 |
| `Automatic-Reboot-Time` | `now` | 按服务器时区选择一个空闲时段 |
| `Automatic-Reboot-WithUsers` | `true` | 设为`false`，有人登录时就跳过重启 |
| `Package-Blacklist` | 空 | 数据库或您手动升级的任何软件 |

再次运行`--dry-run`确认。如果不开启自动重启，内核更新后请留意`/var/run/reboot-required`文件。

## unattended-upgrades日志

```bash
sudo tail -n 50 /var/log/unattended-upgrades/unattended-upgrades.log
sudo less /var/log/unattended-upgrades/unattended-upgrades-dpkg.log
```

第一个文件列出已安装的软件包和错误；第二个文件包含完整的dpkg输出。

## 卡住了：Could not get lock

新服务器刚启动时，`apt install`经常报错`Could not get lock /var/lib/dpkg/lock-frontend`。这是unattended-upgrades正在安装第一批补丁。检查一下，再等几分钟：

```bash
ps aux | grep -i unattended
```

不要删除锁文件，也不要在安装中途结束进程，否则dpkg可能处于配置了一半的状态。如果确实卡住超过30分钟，请查看上面的日志。

## 如何关闭Ubuntu自动更新

```bash
sudo dpkg-reconfigure unattended-upgrades   # answer No
```

或者把`20auto-upgrades`中的两个值都改为`"0"`。要彻底停止定时器：`sudo systemctl disable --now apt-daily.timer apt-daily-upgrade.timer`。只有在您按计划手动打补丁时才这样做。

## Debian 12

最小化安装的Debian可能没有这个软件包。用`apt list --installed | grep unattended`检查，需要时安装：

```bash
sudo apt install unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades
```

Debian在同一个`50unattended-upgrades`文件中使用`Unattended-Upgrade::Origins-Pattern`。默认安装安全更新（`label=Debian-Security`）和稳定版小版本修复（`label=Debian`，例如从12.12到12.13），`-updates`保持注释状态。请注意：自2026年7月11日起，Debian 12的安全修复由Debian LTS团队提供（持续到2028年6月30日）；它们仍通过同一个`bookworm-security`仓库发布，所以无需做任何修改。

## 自动更新加上安全网

自动更新能堵住已知漏洞，但不能替代UFW防火墙、仅限密钥登录的SSH以及尽量少的开放端口。

如果您想新建一台服务器来练习，s1-1套餐（1 vCPU、1GB内存、15GB NVMe）$4.49/月完全够用：unattended-upgrades几乎不占资源。按小时计费时，测试一小时约$0.030750，开通费$0。删除后，未使用的金额退回账户余额。套餐见[价格](https://0x.cloud/zh/pricing)页面。

在大规模手动升级之前，先创建一个[快照](https://0x.cloud/zh/kb/servers/vps-kuaizhao)（0x.cloud上$0.99/月），几分钟内即可回滚。或者让AI智能体帮您配置好一切：


```text
连接到我的0x.cloud服务器（使用MCP的run_command工具或通过SSH）。检查unattended-upgrades是否已安装，以及/etc/apt/apt.conf.d/20auto-upgrades的内容；如果未开启，就开启它。创建/etc/apt/apt.conf.d/52unattended-upgrades-local，设置在服务器时间04:00自动重启，运行unattended-upgrade -v --dry-run并把结果给我看。
```



[创建服务器: s1-1, $4.49/月](https://0x.cloud/cabinet/create?plan=s1-1)


## 常见问题

**Ubuntu默认开启了unattended-upgrades吗？**
Ubuntu Server默认已安装该软件包，每天安装一次安全更新。请确认/etc/apt/apt.conf.d/20auto-upgrades中的两行都设为“1”。

**unattended-upgrades会重启服务器吗？**
默认不会：Automatic-Reboot为false。更新需要重启时，会出现/var/run/reboot-required文件。您可以开启在指定时间自动重启。

**如何关闭Ubuntu自动更新？**
把/etc/apt/apt.conf.d/20auto-upgrades中的两个值都改为“0”，或者运行sudo dpkg-reconfigure unattended-upgrades并选择No。只有在您按计划手动打补丁时才这样做。

**更新把服务器搞坏了怎么办？**
从快照回滚（0x.cloud上$0.99/月），或者以$0重装系统。
