Skip to content

Ubuntu 22.04 误装 24.04 软件包后的系统修复

场景:在 Ubuntu 22.04 (jammy) 系统中,误安装了部分 Ubuntu 24.04 (noble) 的软件包(主要是 systemd / dbus / python3.12 / libicu74 等),导致系统依赖断裂、dpkg 无法配置、dbus 与 systemd 异常。

目标:在 不重装系统 的前提下,通过 LiveCD + chroot 的方式,回退并修复到 22.04 官方可用状态

一、总体修复思路(结论先行)

  1. 必须在 LiveCD 环境下操作,避免使用已损坏系统自身的 systemd / dbus

  2. chroot 回原系统,使用 22.04 仓库版本的 deb 包进行“人工回滚”

  3. 原则:

    • 安装 22.04 对应版本
    • 顺序从 底层库 → systemd/dbus → Python → 高层工具
    • 清理 24.04 的包(iU / 半安装状态)
  4. 全过程允许 --force-all,但仅限救援场景

二、进入救援环境(LiveCD + chroot)

bash
# 原系统根分区假设已挂载到 /mnt
mount --bind /dev  /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys  /mnt/sys
mount --bind /run  /mnt/run

chroot /mnt /bin/bash

三、准备修复所需的 deb 包

建议:在 LiveCD 中提前 apt download,避免在 chroot 里网络异常

1. 批量下载依赖包(22.04 版本)

bash
cd /mnt/livecd-rootfs/debs

for pkg in \
  bsdutils dbus-bin dbus-daemon dbus-session-bus-common dbus-system-bus-common dbus \
  dmsetup gir1.2-girepository-2.0 gir1.2-glib-2.0 libapparmor1 libargon2-1 \
  libblkid1 libbsd0 libcryptsetup12 libdbus-1-3 libdevmapper1.02.1 libexpat1 \
  libfdisk1 libgirepository-1.0-1 libglib2.0-0 libglib2.0-data libjson-c5 \
  libkmod2 libmount1 libpython3-stdlib libsmartcols1 libsystemd-shared \
  libsystemd0 libudev1 libuuid1 libxml2 media-types mount netbase \
  networkd-dispatcher ntpsec ntp python3-dbus python3-gi python3-minimal \
  python3-ntp python3 shared-mime-info systemd-dev systemd-resolved systemd \
  tzdata util-linux xdg-user-dirs; do
  apt download "$pkg"
done

2. 明确指定 dbus 22.04 版本(关键)

bash
apt download \
  dbus=1.12.20-2ubuntu4.1 \
  dbus-bin=1.12.20-2ubuntu4.1 \
  dbus-daemon=1.12.20-2ubuntu4.1 \
  dbus-session-bus-common=1.12.20-2ubuntu4.1 \
  dbus-system-bus-common=1.12.20-2ubuntu4.1 \
  libdbus-1-3=1.12.20-2ubuntu4.1 \
  dbus-user-session=1.12.20-2ubuntu4.1

四、修复执行顺序(核心部分)

顺序非常重要

1. 底层库(filesystem / mount / crypto)

bash
dpkg -i --force-all \
  libbsd0_0.11.5-1_amd64.deb \
  libblkid1_2.37.2-4ubuntu3.4_amd64.deb \
  libuuid1_2.37.2-4ubuntu3.4_amd64.deb \
  libmount1_2.37.2-4ubuntu3.4_amd64.deb \
  libsmartcols1_2.37.2-4ubuntu3.4_amd64.deb \
  libfdisk1_2.37.2-4ubuntu3.4_amd64.deb \
  libkmod2_29-1ubuntu1_amd64.deb \
  libcryptsetup12_2:2.4.3-1ubuntu1.3_amd64.deb \
  libexpat1_2.4.7-1ubuntu0.6_amd64.deb \
  libxml2_2.9.13+dfsg-1ubuntu0.10_amd64.deb \
  libjson-c5_0.15-3~ubuntu1.22.04.2_amd64.deb \
  libargon2-1_0~20171227-0.3_amd64.deb \
  libapparmor1_3.0.4-2ubuntu2.5_amd64.deb

2. systemd / glib 核心库(22.04)

bash
dpkg -i --force-all \
  libglib2.0-0_2.72.4-0ubuntu2.7_amd64.deb \
  libglib2.0-data_2.72.4-0ubuntu2.7_all.deb \
  libsystemd0_249.11-0ubuntu3.17_amd64.deb \
  libudev1_249.11-0ubuntu3.17_amd64.deb \
  libdevmapper1.02.1_2:1.02.175-2.1ubuntu5_amd64.deb \
  libsystemd-shared_249.11-0ubuntu3.17_amd64.deb \
  libgirepository-1.0-1_1.72.0-1_amd64.deb \
  gir1.2-glib-2.0_1.72.0-1_amd64.deb

3. 基础系统工具

bash
dpkg -i --force-all \
  bsdutils_1:2.37.2-4ubuntu3.4_amd64.deb \
  mount_2.37.2-4ubuntu3.4_amd64.deb \
  util-linux_2.37.2-4ubuntu3.4_amd64.deb \
  dmsetup_2:1.02.175-2.1ubuntu5_amd64.deb \
  tzdata_2025b-0ubuntu0.22.04.1_all.deb \
  netbase_6.3_all.deb \
  networkd-dispatcher_2.1-2ubuntu0.22.04.2_all.deb

4. systemd 服务本体

bash
dpkg -i --force-all \
  systemd_249.11-0ubuntu3.17_amd64.deb \
  systemd-resolved_249.11-0ubuntu3.17_amd64.deb

5. Python(必须回退到 3.10

bash
dpkg -i --force-all \
  python3-minimal_3.10.6-1~22.04.1_amd64.deb \
  python3_3.10.6-1~22.04.1_amd64.deb \
  python3-ntp_1.2.1+dfsg1-4_amd64.deb \
  python3-dbus_1.2.18-3build1_amd64.deb \
  python3-gi_3.42.1-0ubuntu1_amd64.deb

6. NTP / 时间服务

bash
dpkg -i --force-all \
  ntp_1:4.2.8p15+dfsg-1ubuntu2_amd64.deb \
  ntpsec_1.2.1+dfsg1-4_amd64.deb \
  sntp_1:4.2.8p15+dfsg-1ubuntu2_amd64.deb

7. 高层通用数据

bash
dpkg -i --force-all \
  media-types_7.0.0_all.deb \
  shared-mime-info_2.1-2_amd64.deb \
  xdg-user-dirs_0.17-2ubuntu4_amd64.deb

五、清理 24.04 遗留包(关键动作)

1. 查看半安装状态(iU)

bash
dpkg -l | grep ^iU

典型问题包(来自 24.04):

  • libicu74
  • python3.12 / libpython3.12-*
  • libsystemd-shared (255)

2. 强制移除

bash
dpkg --remove --force-remove-reinstreq --force-depends \
  libpython3.12-stdlib \
  python3.12 \
  libpython3.12-minimal \
  libicu74

或一次性:

bash
dpkg -l | awk '/^iU/ {print $2}' | xargs dpkg --remove --force-remove-reinstreq --force-depends

六、dbus 专项修复(最容易翻车的点)

1. 停止 dbus(如果存在)

bash
systemctl stop dbus.socket dbus.service 2>/dev/null || true

2. 移除错误版本(1.14.x / noble)

bash
dpkg --remove --force-remove-reinstreq --force-depends \
  dbus dbus-daemon dbus-bin libdbus-1-3

3. 安装 22.04 正确版本

bash
cd /root/dbus-packages-20260117/

dpkg -i --force-all \
  libdbus-1-3_1.12.20-2ubuntu4.1_amd64.deb \
  dbus_1.12.20-2ubuntu4.1_amd64.deb \
  dbus-user-session_1.12.20-2ubuntu4.1_amd64.deb \
  dbus-bin_1.12.20-2ubuntu4.1_amd64.deb

[ -f dbus-daemon_1.12.20*.deb ] && dpkg -i --force-all dbus-daemon_1.12.20*.deb

七、收尾检查

bash
dpkg --configure -a
apt -f install

lsb_release -a
python3 --version   # 必须是 3.10.x
systemctl --version # 必须是 249

八、经验教训(必须记住)

  • Ubuntu 不支持跨大版本混装核心包(systemd / python / libc / dbus)
  • apt install ./xxx.deb ≠ 安全
  • 一旦 systemd / dbus / python3 崩,在线修复基本不可能
  • LiveCD + chroot 是唯一可靠方案
  • --force-all 只在“救砖”时使用,不能作为日常手段

本次系统最终恢复到可正常 boot、可联网、可 apt update 状态