跳转至

29 - 系统迁移与升级(Debian 11 → 12 → 13)

前提条件

  • 当前系统为 Debian 11(Bullseye)或 Debian 12(Bookworm),具有 root 权限
  • 所有关键服务数据已完整备份(数据库、Web 文件、/etc/home
  • 虚拟机环境:VMware Workstation Pro(升级前必须创建快照
  • 网络稳定,apt 源已配置为国内镜像
  • 无正在进行的 dpkg / apt 事务(dpkg --audit 输出为空)

全局参数

主机名 linuxdc,IP 192.168.1.100/24,SSH 端口 2222,运维用户 ops

生产环境升级原则

生产服务器应优先使用蓝绿部署(新版本机器并行运行,验证后切流量),而非就地升级。就地升级仅适合实验环境或在明确维护窗口内操作的小型系统。就地升级前必须完成:快照 / 全量备份 → 升级前全量测试 → 制定回滚计划。

升级路径说明

升级路径 状态 推荐程度
Debian 11 (Bullseye) → Debian 12 (Bookworm) 稳定,官方支持 生产可用
Debian 12 (Bookworm) → Debian 13 (Trixie) 截至 2026 年 6 月仍为 testing 仅限测试环境
跨版本升级(如 11 → 13) 不支持,风险极高 禁止

详细步骤

1. 升级前全面准备(所有版本通用)

# 1. 创建 VMware 快照(关机状态更安全)
#    VM → Snapshot → Take Snapshot → 命名为 "pre-upgrade-YYYYMMDD"

# 2. 备份关键目录
mkdir -p /backup/pre-upgrade
tar -czf /backup/pre-upgrade/etc-$(date +%Y%m%d).tar.gz /etc
rsync -aq /var/www/ /backup/pre-upgrade/www/
mysqldump -u root -p --all-databases --single-transaction \
    > /backup/pre-upgrade/all-databases-$(date +%Y%m%d).sql

# 3. 确认当前版本
lsb_release -a
cat /etc/debian_version

# 4. 将当前系统升级到最新 patch
apt update && apt full-upgrade -y && apt autoremove -y

# 5. 检查是否需要重启(内核更新等)
[ -f /var/run/reboot-required ] && echo "需要重启" || echo "无需重启"

# 6. 清理磁盘空间
apt clean
journalctl --vacuum-time=30d
df -h /    # 确认根分区有至少 5 GB 可用空间

# 7. 检查 dpkg 健康状态
dpkg --audit    # 无输出表示正常

检查点

dpkg --audit 无输出;/var/run/reboot-required 不存在(或已重启);根分区可用空间 ≥ 5 GB;备份文件可正常读取。

2. Debian 11 (Bullseye) → Debian 12 (Bookworm)

修改 apt 源(推荐完整替换,避免 sed 遗漏):

# 备份原始 sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bak-$(date +%F)
cp -r /etc/apt/sources.list.d/ /etc/apt/sources.list.d.bak-$(date +%F)/

# 替换主 sources.list
tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
EOF

# 将 sources.list.d 下所有文件中的 bullseye 替换为 bookworm
# 注意:第三方源(如 Docker、NodeSource)需手动确认兼容性
for f in /etc/apt/sources.list.d/*.list; do
    sed -i.bak 's/bullseye/bookworm/g' "$f"
done

第三方源需单独处理

/etc/apt/sources.list.d/ 中的第三方源(Docker、NodeSource、HashiCorp 等)在替换 codename 前,需先确认该第三方软件是否提供 bookworm 版本。若不确定,先注释该源,升级完成后再逐一恢复。

执行升级(两阶段):

# 第一阶段:更新包列表
apt update

# 第二阶段:执行升级(可能需要较长时间,建议在 screen 或 tmux 中运行)
apt full-upgrade -y

# 清理废弃包
apt autoremove -y
apt clean

重启并验证:

reboot
# 重启后验证
cat /etc/debian_version    # 应显示 12.x
lsb_release -a
uname -r                   # 确认新内核已加载
systemctl --failed         # 确认无服务启动失败

检查点

lsb_release -a 显示 Codename: bookwormsystemctl --failed 无条目,关键业务服务(Nginx、MariaDB 等)正常运行。

3. Debian 12 (Bookworm) → Debian 13 (Trixie)

当前状态(2026 年 6 月)

Debian 13 (Trixie) 截至本文档编写时仍处于 testing 阶段,尚未正式发布为 stable。testing 分支包版本更新频繁,可能包含未充分测试的软件,存在破坏性变更风险。严禁在生产环境执行此升级,仅在专用测试虚拟机上进行。Trixie 正式发布后,此限制解除。

流程与 11 → 12 完全相同,仅修改 codename:

cp /etc/apt/sources.list /etc/apt/sources.list.bak-$(date +%F)

tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security trixie-security main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie-updates main contrib non-free non-free-firmware
EOF

apt update
apt full-upgrade -y
apt autoremove -y
reboot

Trixie 升级后需重点验证的变更点:

组件 Bookworm 版本 Trixie 预期版本 注意事项
Linux 内核 6.1 LTS 6.11+ 驱动兼容性,尤其是 DKMS 模块
Python 3.11 3.12 检查 Python 脚本兼容性
PHP 8.2 8.3 WordPress / Nextcloud 等 PHP 应用
OpenSSL 3.0 3.3+ TLS 配置、证书格式
/usr-merge 部分完成 全部完成 /bin/usr/bin 符号链接,脚本硬编码路径需检查

4. 回滚与故障恢复

场景一:升级过程中断(网络中断、超时等)

# 尝试恢复 dpkg 中断的事务
dpkg --configure -a
apt install -f
apt full-upgrade -y

场景二:升级后服务无法启动

# 检查失败服务的详细日志
systemctl status <服务名>
journalctl -u <服务名> -xe --no-pager

# 检查 AppArmor 是否阻断
journalctl | grep "apparmor.*DENIED" | tail -20

场景三:系统完全不可用,从快照恢复

通过 VMware 控制台 → Snapshot Manager → 选择升级前快照 → Revert。

场景四:无快照,从 live USB 恢复

# 从 live USB 启动,挂载根分区
mount /dev/sda1 /mnt
chroot /mnt

# 将 sources.list 改回 bookworm
vim /etc/apt/sources.list

apt update
apt full-upgrade -y

5. 升级后清理

# 清除已删除包的残留配置文件
dpkg -l | grep '^rc' | awk '{print $2}' | xargs apt purge -y

# 更新 GRUB
update-grub

# 重新加载 systemd 单元(处理单元文件变更)
systemctl daemon-reload

# 最终全系统状态检查
systemctl --failed
df -h
free -h

实践任务

  1. 在测试虚拟机上完整执行 Debian 11 → 12 升级,记录每步骤的输出和耗时
  2. 升级完成后检查:内核版本、关键服务状态、systemctl --failed 输出
  3. 编写升级预检脚本:检查当前版本、可用空间、dpkg --audit、是否需要重启
  4. 模拟 sources.list.d 中有第三方源的情况,实践手动处理第三方源兼容性的流程

自测问题

1. 为什么升级 Debian 时只需一次 apt full-upgrade 而不是两次?

原文建议"两次"是过时的早期做法。现代 APT 版本(Debian 10+)能在单次 apt full-upgrade 中正确解析所有依赖关系和包替换逻辑。唯一仍需注意的是:若第一次 full-upgrade 后有新包被安装并带来额外依赖,再次运行 apt full-upgrade 确保所有依赖均已满足——但这只是确保性操作,而非必须。重要的是升级前先做 apt full-upgrade 确保当前系统处于最新 patch 状态。

2. Debian 13 (Trixie) 当前是什么阶段?生产环境是否推荐直接升级?

截至 2026 年 6 月,Trixie 仍处于 testing 阶段,尚未发布为 stable。testing 分支无完整的安全支持(安全更新延迟),软件包版本变动频繁,可能包含回归问题。生产环境不推荐升级到 testing 分支。Trixie 正式发布为 stable 后(预计 2025 年),升级路径才变为官方支持的稳定升级。

3. /etc/apt/sources.listnon-free-firmware 的作用是什么?

Debian 12 (Bookworm) 起将部分非自由固件(网卡、无线网卡、GPU 驱动等的闭源固件 blob)从 non-free 单独拆分为 non-free-firmware 组件。若不包含此组件,某些硬件(尤其是 Wi-Fi 网卡、Intel 网卡)可能在安装或升级后无法正常工作。在服务器环境(VMware 虚拟机)中通常不受影响,但物理服务器上的网卡固件可能需要此组件。

4. 如果升级过程中出现依赖冲突,最快速的排查方式是什么?

# 1. 查看具体冲突信息
apt full-upgrade 2>&1 | grep -A5 "has broken"

# 2. 尝试自动修复
apt install -f

# 3. 找出冲突包并单独处理
apt-cache show <package> | grep -E "Depends|Conflicts|Replaces"

# 4. 临时跳过问题包(仅当确认安全时)
apt full-upgrade --ignore-hold

# 5. 查看 dpkg 日志了解历史操作
tail -100 /var/log/dpkg.log

总结

Debian 就地升级路径清晰,核心流程是:备份 + 快照 → 替换 sources.list → apt full-upgrade → 重启验证。关键在于升级前的充分准备(备份、快照、空间检查)和升级后的系统验证(服务状态、日志审查)。Debian 13 正式发布前,生产环境应继续运行 Debian 12(全面支持至 2027 年 5 月)。