如何修复 Ubuntu 18.04 中损坏的 apt,而无需从头开始重新安装

如何修复 Ubuntu 18.04 中损坏的 apt,而无需从头开始重新安装

我有一台运行 Ubuntu 18.04.6 LTS 的旧服务器,我怀疑有人试图安装或升级到错误的 Ubuntu 20.04 软件包,导致 apt 损坏和所谓的“Frankensystem”。

我想修复此问题,但又不想重新安装整个系统,尤其是服务器位于远程位置。是否可以逐个修复损坏的软件包?如果可以,我该怎么做?

以下是我遇到的错误:

运行时sudo apt update

$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Fetched 222 kB in 2s (118 kB/s)
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ImportError: libapt-pkg.so.6.0: cannot open shared object file: No such file or directory
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

运行时sudo apt --fix-broken install

$ sudo apt --fix-broken install
Reading package lists... Done
Building dependency tree... Done
Correcting dependencies... failed.
The following packages have unmet dependencies:
 dirmngr : Depends: init-system-helpers (>= 1.52) but 1.51 is installed
 gir1.2-gtk-3.0 : Depends: gir1.2-gdkpixbuf-2.0 (>= 2.40.0) but 2.36.11-2 is installed
                  Depends: gir1.2-pango-1.0 (>= 1.41.0) but 1.40.14-1ubuntu0.1 is installed
                  Depends: libgtk-3-0 (>= 3.24.7) but 3.22.30-1ubuntu4 is installed
 gnupg : Depends: gpgv (>= 2.2.19-3ubuntu2.2) but 2.2.4-1ubuntu1.6 is installed
 gnupg-utils : Depends: libreadline8 (>= 6.0) but it is not installed
 gpg : Depends: libreadline8 (>= 6.0) but it is not installed
 gpg-agent : Depends: init-system-helpers (>= 1.52) but 1.51 is installed
 gpgconf : Depends: libreadline8 (>= 6.0) but it is not installed
 gpgsm : Depends: libreadline8 (>= 6.0) but it is not installed
 hplip : Depends: hplip-data (= 3.20.3+dfsg0-2) but 3.17.10+

当我检查时/usr/lib/x86_64-linux-gnu发现libapt-pkg.so.5.0存在,但是没有libapt-pkg.so.6.0

另外,/usr/lib/python3/dist-packages我发现apt_pkg.cpython-38-x86_64-linux-gnu.so这很奇怪,因为python3版本是3.6.9

关于备份,我找到了这些文件

/var/backups$ ls -l
total 7276
-rw-r--r-- 1 root root    153600 Apr 10 00:08 alternatives.tar.0
-rw-r--r-- 1 root root      9783 Jun 24  2022 alternatives.tar.1.gz
-rw-r--r-- 1 root root      9808 Nov 27  2021 alternatives.tar.2.gz
-rw-r--r-- 1 root root      9421 Mar 27  2021 alternatives.tar.3.gz
-rw-r--r-- 1 root root      6161 Mar 18  2021 alternatives.tar.4.gz
-rw-r--r-- 1 root root      6090 Jan 25  2021 alternatives.tar.5.gz
-rw-r--r-- 1 root root      6061 Nov 28  2020 alternatives.tar.6.gz
-rw-r--r-- 1 root root    154853 Apr  9 23:02 apt.extended_states.0
-rw-r--r-- 1 root root     12962 Apr  9 21:44 apt.extended_states.1.gz
-rw-r--r-- 1 root root     12828 Jul 26  2022 apt.extended_states.2.gz
-rw-r--r-- 1 root root     12821 Jul  4  2022 apt.extended_states.3.gz
-rw-r--r-- 1 root root     12806 Jun 23  2022 apt.extended_states.4.gz
-rw-r--r-- 1 root root     13304 Jun 17  2022 apt.extended_states.5.gz
-rw-r--r-- 1 root root     13269 Nov 26  2021 apt.extended_states.6.gz
-rw-r--r-- 1 root root      4649 Jun 23  2022 dpkg.diversions.0
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.1.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.2.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.3.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.4.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.5.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.6.gz
-rw-r--r-- 1 root root       348 Mar 18  2021 dpkg.statoverride.0
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.1.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.2.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.3.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.4.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.5.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.6.gz
-rw-r--r-- 1 root root   2529012 Jul  3 16:58 dpkg.status.0
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.1.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.2.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.3.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.4.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.5.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.6.gz
-rw------- 1 root root      1336 Apr  9 22:33 group.bak
-rw------- 1 root shadow    1107 Apr  9 22:33 gshadow.bak
-rw------- 1 root root      3518 Apr  9 22:33 passwd.bak
-rw-r--r-- 1 root smmsp    64178 Mar 18  2021 sendmail.cf.bak
-rw-r--r-- 1 root smmsp     4058 Mar 18  2021 sendmail.mc.bak
-rw------- 1 root shadow    2479 Apr  9 22:33 shadow.bak
-rw-r--r-- 1 root smmsp    44618 Mar 18  2021 submit.cf.bak
-rw-r--r-- 1 root smmsp     2375 Mar 18  2021 submit.mc.bak

或者我可以使用这些文件中的任何一个将系统恢复到正常工作状态吗?

相关内容